Basically, the property animation changes object’s properties value over a specified duration.
Using property animation, you can define following characteristics of the animation:
Duration: By default, the animation duration is 300ms.
Repeat count and behaviour: You can specify the number of animation repeat and whether the animation playback in reverse.
Animation interpolation: You can define how the values for property are calculated over the time
Animators set: You can group different animator and animators set to execute them together, in sequence or after some delay.
Frame refresh delay: By default, the frames will be refreshed after 10ms.
B - Compare Property Animation and View Animation
View Animation
Property Animation
Only apply to View
Be able to apply to any object
Only animate some properties of View like scaling and rotation
Can animate any properties of object
Only modify where the View was drawn, not the actual View itself. For example, if you animate a View to move across the screen, the View was drawn correctly but the actual View which you can interact with is still at the old location.
Modify where the object was drawn and the actual object itself
Easy to set up and write code
Require more effort to implement
C - Use ValueAnimator
The ValueAnimator class lets you animate values of some type for the duration of an animation by specifying a set of int, float, or color values to animate through.
You obtain a ValueAnimator by calling one of its factory methods: ofInt(), ofFloat(), or ofObject().
As you can see, in order to make real animation effect to the object, we also need to define the listener for ValueAnimator.
In that listener, you can obtain the calculated value for that specific frame and apply changes to your object.
D - Use ObjectAnimator
The ObjectAnimator is a subclass of the ValueAnimator and it allows you to specify the property name of the target object.
Using ObjectAnimator, you don’t need to implement the listener implicitly because the property’s value will be updated automatically.
To create a correct ObjectAnimator, you have to define:
Target object: The object you want to animate its property.
Animated property: The property you want to animate. This property must have a setter with format set<property name>. If your property doesn’t have a setter, you can:
Add a setter for it
Or use a wrapper that has right to change its values and apply ObjectAnimator to that wrapper. Make sure the wrapper class have valid setter.
Or use ValueAnimator instead
Values: This define the property value range during animation duration. If you only define 1 value, it will be the end value. In this case, the start value will be the default value when you call the getter. Because of that, your object must have the getter with format get<property name>.
Example:
E - Use AnimatorSet
In case you want to group some animator together to animate it at the same time or in sequence, you can use AnimatorSet to achieve that.
F - Use Interpolator
An Interpolator define the calculation of animation value over the animating time. Interpolator allows animation has a non-linear motion like acceleration and deceleration.
Android system provides some default interpolator like:
AccelerateInterpolator: The rate of change starts out slowly and and then accelerates
DecelerateInterpolator: The rate of change starts out quickly and and then decelerates
AccelerateDecelerateInterpolator: The rate of change starts and ends slowly but accelerates through the middle
AnticipateInterpolator: The change starts backward then flings forward
AnticipateOvershootInterpolator: The change starts backward then flings forward and overshoots the target value and finally goes back to the final value
If you don’t like any default interpolator, you can create your own by implement TimeInterpolator interface.
G - Declare Animation in XML resources
Instead of implementing the animation programmatically like above, you can declare the animation details in XML resources. This way allows you to reuse the animation and modify the animation sequence easily.
The animation XML resources are stored in res/animator/ folder.
Syntax details:
Example:
H - Demo application and Source code
Please find the complete source code of this tutorial at Github: