Animate ProgressBar update in Android
I used android Animation for this:
public class ProgressBarAnimation extends Animation{ private ProgressBar progressBar; private float from; private float to; public ProgressBarAnimation(ProgressBar progressBar, float from, float to) { super(); this.progressBar = progressBar; this.from = from; this.to = to; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); float value = from + (to - from) * interpolatedTime; progressBar.setProgress((int) value); }}
and call it like so:
ProgressBarAnimation anim = new ProgressBarAnimation(progress, from, to);anim.setDuration(1000);progress.startAnimation(anim);
Note: if from and to value are too low to produce smooth animation just multiply them by a 100 or so. If you do so, don't forget to multiply setMax(..) as well.
I use an ObjectAnimator
private ProgressBar progreso;private ObjectAnimator progressAnimator;progreso = (ProgressBar)findViewById(R.id.progressbar1);progressAnimator = ObjectAnimator.ofInt(progreso, "progress", 0,1);progressAnimator.setDuration(7000);progressAnimator.start();
The simplest way, using ObjectAnimator (both Java and Kotlin):
ObjectAnimator.ofInt(progressBar, "progress", progressValue) .setDuration(300) .start();
where progressValue
is integer within range 0-100 (the upper limit is set to 100 by default but you can change it with Progressbar#setMax() method)
You can also change the way how values are changing by setting different interpolator with setInterpolator() method. Here is visualization of different interpolators: https://www.youtube.com/watch?v=6UL7PXdJ6-E