我试图为我的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();
}
发布于 2016-07-06 17:19:57
如果每次将进度值更改为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();
}
发布于 2015-06-11 04:40:45
因为您使用的是ofInt
,所以只能在全整数上移动。换句话说,如果你有一个宽度为1000的进度条,进度从0到100,因为你是以整数步速移动的,你会数到1,2,3,4,它可以转换为10px,20px,30px和40px。这就解释了你所看到的锯齿状。
要纠正这一点,您有几个选项。第一个是将你的整数从0增加到someBigInt
,这会给动画师提供更多的数字。
ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 10000, 0);
另一种选择是使用ofFloat
,它与ofInt
做同样的事情,但使用浮点而不是整数。
ObjectAnimator progressAnimator = ObjectAnimator.ofFloat(mProgressBar, "progress", 100.0, 0.0);
发布于 2017-10-12 21:08:04
XML
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:indeterminate="false"
android:progress="0"
android:max="100"/>
JAVA
@BindView(R.id.progress_bar) ProgressBar progressBar;
ObjectAnimator.ofInt(progressBar, "progress", 79).start();
79
对于此示例,
https://stackoverflow.com/questions/30766755
复制相似问题