使用 textview 和进度条创建框架布局:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progress_bar"
android:progressDrawable="@drawable/progress_bar_states"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="false"
style="?android:attr/progressBarStyleHorizontal" />
<TextView
android:id="@+id/text_view_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter"
android:textColor="@android:color/white"
android:padding="6dp"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center"/>
</FrameLayout>
您需要创建一个progressDrawable 文件。
文件 res/drawable/progress_bar_states.xml 声明了不同状态的颜色:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<gradient
android:startColor="#777777"
android:centerColor="#333333"
android:centerY="0.75"
android:endColor="#222222"
android:angle="270" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<gradient
android:startColor="#234"
android:centerColor="#234"
android:centerY="0.75"
android:endColor="#a24"
android:angle="270" />
</shape>
</clip>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#999999"
android:centerColor="#777777"
android:centerY="0.75"
android:endColor="#555555"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
然后,创建按钮/进度条的逻辑:
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);
final ObjectAnimator objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 100).setDuration(2000);
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int progress = (int) valueAnimator.getAnimatedValue();
progressBar.setProgress(progress);
}
});
TextView btn = (TextView) findViewById(R.id.text_view_button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
objectAnimator.start();
}
});
基本上,在单击 Textview 后,ObjectAnimator 会增加 ProgressBar 2 秒,直到完成。
如果你想加快进度调用这个方法:
private void completeFast(final ProgressBar progressBar) {
final ObjectAnimator objectAnimator = ObjectAnimator.ofInt(progressBar, "progress",
progressBar.getProgress(), progressBar.getMax());
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int progress = (int) valueAnimator.getAnimatedValue();
progressBar.setProgress(progress);
}
});
}
会在0.3s内完成进度
也许你想改变 res/drawable/progress_bar_states.xml 的颜色。不过差不多就这些了=]
结果: