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.