Here we will be creating a custom progress bar with linear animation of progress.
We need to create a progress bar in activity_main.xml layout file with a progressDrawable which sets the background color and progress color of ProgressBar widget.
Now, we have to set maximum value of progress:
Suppose, if you set max to 100 and your phone has 1400 pixels between 1 and 100 point.
Then when you set progress value 1 to 2, it will cover 14 pixels which will not be shown smooth to user.
So, that’s why we choose max to 500, so that when you cover progress 1 to progress 2 , it will cover 2.8 pixels(1400/500), which will be smooth for users.
Basically above function will give animation, but it will feel very slow, because here we are updating progress value by 1, 5 times in 1 sec, i.e. by updating progress value to 5 in 1 sec.
We should not calculate intermediate progress values(by what progress values to be incremented in 1 sec). We should use LinerInterpolator to calculate intermediate values of progress to update in a sec.
Now, we will use ObjectAnimator to set LinearInterpolator to provide smooth animation by setting linearInterpolator on ObjectAnimator instance.
This subclass of
ValueAnimatorprovides support for animating properties on target objects. The constructors of this class take parameters to define the target object that will be animated as well as the name of the property that will be animated. Appropriate set/get functions are then determined internally and the animation will call these functions as necessary to animate the property.
|target||The object whose property is to be animated. This object should have a public method on it called |
|propertyName||The name of the property being animated.|
|values||A set of values that the animation will animate between over time.|
So, here is MainActivity.java code: