我在我的应用程序中使用 ProgressBar,我在 AsyncTask
的 onProgressUpdate 中进行了更新。到目前为止,一切都很好。
我想做的是为进度更新设置动画,这样它不仅“跳转”到该值,而且可以平滑地移动到该值。
我尝试这样做运行以下代码:
this.runOnUiThread(new Runnable() {
@Override
public void run() {
while (progressBar.getProgress() < progress) {
progressBar.incrementProgressBy(1);
progressBar.invalidate();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
问题在于进度条在完成其最终值(进度变量)之前不会更新其状态。两者之间的所有状态都不会显示在屏幕上。 调用 progressBar.invalidate() 也没有帮助。
有什么想法吗?谢谢!
最佳答案
我为此使用了 android Animation:
public class ProgressBarAnimation extends Animation{
private ProgressBar progressBar;
private float from;
private float to;
public ProgressBarAnimation(ProgressBar progressBar, float from, float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
progressBar.setProgress((int) value);
}
}
然后这样调用它:
ProgressBarAnimation anim = new ProgressBarAnimation(progress, from, to);
anim.setDuration(1000);
progress.startAnimation(anim);
注意:如果 from 和 to 值太低而无法产生流畅的动画,只需将它们乘以 100 左右。如果你这样做了,不要忘记乘以 setMax(..)。
关于android - Android 中的动画进度条更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8035682/