Smooth Progress Bar Animation
Solution 1:
If you change progress value each time by 1 (for example from 45 to 46) you won't see the animation. You'd better change progress by 100 points (or maybe other), for this you just need to multiply your max value by 100 and each progress value to 100 too. For example:
privatevoidsetProgressMax(ProgressBar pb, int max) {
pb.setMax(max * 100);
}
privatevoidsetProgressAnimate(ProgressBar pb, int progressTo)
{
ObjectAnimatoranimation= ObjectAnimator.ofInt(pb, "progress", pb.getProgress(), progressTo * 100);
animation.setDuration(500);
animator.setAutoCancel(true);
animation.setInterpolator(newDecelerateInterpolator());
animation.start();
}
Solution 2:
Because you are using ofInt
you can only move at full integers. In other words, if you have a progress bar with a width of 1000 and a progress of 0 to 100 since you are moving at an integer pace you count 1, 2, 3, 4 which translates to 10px, 20px, 30px and 40px. Which explains the jaggedness you are seeing.
To correct this you have a few options. The first is to up your integers from 0 to someBigInt
This will give the animator more numbers to work with.
ObjectAnimatorprogressAnimator= ObjectAnimator.ofInt(mProgressBar, "progress", 10000, 0);
The other option is to use ofFloat
which does the same thing as ofInt
but uses floating points instead of integers.
ObjectAnimatorprogressAnimator= ObjectAnimator.ofFloat(mProgressBar, "progress", 100.0, 0.0);
Solution 3:
If you have Android N and above, you can use :
progressBar.setProgress(newProgress, true)
Docs:
Sets the current progress to the specified value, optionally animating the visual position between the current and target values.
Animation does not affect the result of getProgress(), which will return the target value immediately after this method is called.
https://developer.android.com/reference/android/widget/ProgressBar.html#setProgress(int,%20boolean)
Solution 4:
XML
<ProgressBar
android:id="@+id/progress_bar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="4dp"
android:indeterminate="false"
android:progress="0"
android:max="100"/>
JAVA
@BindView(R.id.progress_bar) ProgressBar progressBar;
ObjectAnimator.ofInt(progressBar, "progress", 79).start();
79
- any number between 0 and 100 for this example
Solution 5:
Just set
android:max="1000"
and do
ObjectAnimator progressAnimator = ObjectAnimator.ofInt(mProgressBar, "progress", 1000, 0);
in this case you will animate on 1/1000 by each step which in 10 time smoothly when default 100 percent scale. and it looks much better
Post a Comment for "Smooth Progress Bar Animation"