Android: How to make horizontal progress bar using Interpolator?

Lucky Luke picture Lucky Luke · May 27, 2015 · Viewed 8.7k times · Source

I have a progress bar view like this:

<ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:max="100"/>

It lasts 3 second, so how to use interpolator to make update smoothly?

ObjectAnimator animation = ObjectAnimator.ofInt(what_is_in_here?); 
animation.setDuration(3000); //  second
animation.setInterpolator(new DecelerateInterpolator());
animation.start();

I really appreciate your help. Thank you very much in advance.

Answer

Lucky Luke picture Lucky Luke · May 27, 2015

I found out the solution:

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

Here is a detailed explanation:

create an animation object:

ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0);
  • progressBar: reference to the ProgressBar in the layout;
  • "progress": the name of the property to be animated;
  • 100: starting point of the animation;
  • 0: ending point of the animation.

and set an interpolation:

animation.setInterpolator(new DecelerateInterpolator());

It is possible to use different interpolators for our animation, like for example:

  • LinearInterpolator: where the rate of change is constant.
  • DecelerateInterpolator: where the rate of change starts out quickly and and then decelerates.

  • AccelerateInterpolator: where the rate of change starts out slowly and and then accelerates.

  • OvershootInterpolator: where the change flings forward and overshoots the last value then comes back.

  • For other interpolators check the interface android.view.animation.Interpolator.