我有一个这样的进度条视图:
<ProgressBar android:id="@+id/progress_bar" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100"/>
它持续3秒,那么如何使用插补器进行平滑更新?
ObjectAnimator animation = ObjectAnimator.ofInt(what_is_in_here?); animation.setDuration(3000); // second animation.setInterpolator(new DecelerateInterpolator()); animation.start();
我非常感谢你的帮助.非常感谢你提前.
我找到了解决方案:
progressBar = (ProgressBar) findViewById(R.id.progress_bar); ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0); animation.setDuration(3500); // 3.5 second animation.setInterpolator(new DecelerateInterpolator()); animation.start();
这是一个详细的解释:
创建一个动画对象:
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0);
> progressBar:布局中对ProgressBar的引用;
>“progress”:要动画的属性的名称;
> 100:动画的起点;
> 0:动画的结束点.
并设置插值:
animation.setInterpolator(new DecelerateInterpolator());
可以为我们的动画使用不同的插值器,例如:
> LinearInterpolator:变化率恒定的地方.
> DecelerateInterpolator:变化率快速开始然后减速.
> AccelerateInterpolator:变化率开始缓慢然后加速.
> OvershootInterpolator:变化向前晃动并超过最后一个值然后返回.
>对于其他插补器,请检查接口android.view.animation.Interpolator.