Android: 如何使用插值器制作水平进度条?

14

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

<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();

我非常感谢您的帮助。非常感谢您的预先帮助。

1个回答

36

我找到了解决方案:

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:layout中ProgressBar的引用;
  • "progress":要进行动画处理的属性名称;
  • 100:动画的起始点;
  • 0:动画的结束点。

并设置插值:

animation.setInterpolator(new DecelerateInterpolator());

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

  • LinearInterpolator:其中变化速率是恒定的。
  • DecelerateInterpolator:其中变化速率开始很快,然后逐渐减速。
  • AccelerateInterpolator:其中变化速率开始缓慢,然后加速。
  • OvershootInterpolator:它会将变化向前抛出并超过最后一个值,然后回来。
  • 要查看其他插值器,请检查界面android.view.animation.Interpolator.

非常好的答案!简洁明了易懂。谢谢! - Eugene H
需要帮助,“progress”是已定义的属性名称还是自定义的?如果已经定义,预定义的列表是什么? - N J

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接