Android:如何使用Interpolator制作水平进度条?

Luc*_*uke 10 android linear-interpolation

我有一个这样的进度条视图:

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"/>
Run Code Online (Sandbox Code Playgroud)

它持续3秒,那么如何使用插值器进行平滑更新?

ObjectAnimator animation = ObjectAnimator.ofInt(what_is_in_here?); 
animation.setDuration(3000); //  second
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
Run Code Online (Sandbox Code Playgroud)

我非常感谢你的帮助.非常感谢你提前.

Luc*_*uke 29

我找到了解决方案:

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();
Run Code Online (Sandbox Code Playgroud)

这是一个详细的解释:

创建一个动画对象:

ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0);
Run Code Online (Sandbox Code Playgroud)
  • progressBar:在布局中引用ProgressBar;
  • "进度":要动画的属性的名称;
  • 100:动画的起点;
  • 0:动画的结束点.

并设置插值:

animation.setInterpolator(new DecelerateInterpolator());
Run Code Online (Sandbox Code Playgroud)

可以为我们的动画使用不同的插值器,例如:

  • LinearInterpolator:变化率不变的地方.
  • DecelerateInterpolator:变化率快速开始然后减速.

  • AccelerateInterpolator:变化率开始缓慢然后加速.

  • OvershootInterpolator:变化向前晃动并超过最后一个值然后返回.

  • 对于其他插值器,请检查界面android.view.animation.Interpolator.