我正在尝试为我的 ProgressBar
实现流畅的动画,但是当我增加时间(30 秒)时,动画不再流畅。
以 5 秒为例:
以 30 秒为例:
我的进步背景:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:top="1dp" />
<solid android:color="#10444444" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" />
<solid android:color="#20444444" />
</shape>
</item>
<item>
<shape>
<padding android:top="1dp" />
<solid android:color="#30444444" />
</shape>
</item>
<item android:id="@android:id/background">
<shape>
<solid android:color="@color/black_thirty" />
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<solid android:color="#3500D0" />
</shape>
</clip>
</item>
</layer-list>
我的进度布局:
<ProgressBar
android:id="@+id/pb_loading"
android:layout_width="match_parent"
android:layout_height="8dp"
android:indeterminate="false"
android:layout_centerInParent="true"
android:progress="100"
android:progressDrawable="@drawable/my_progress_bar" />
我的动画方法:
private void startAnimation(){
ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.pb_loading);
ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 100, 0);
progressAnimator.setDuration(30000);
progressAnimator.setInterpolator(new LinearInterpolator());
progressAnimator.start();
}
最佳答案
如果您每次将进度值更改 1(例如从 45 到 46),您将看不到动画。您最好将进度更改 100 点(或其他点),为此您只需将最大值乘以 100,并将每个进度值也乘以 100。例如:
private void setProgressMax(ProgressBar pb, int max) {
pb.setMax(max * 100);
}
private void setProgressAnimate(ProgressBar pb, int progressTo)
{
ObjectAnimator animation = ObjectAnimator.ofInt(pb, "progress", pb.getProgress(), progressTo * 100);
animation.setDuration(500);
animator.setAutoCancel(true);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
关于android - 平滑进度条动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30766755/