Android.animation.ValueAnimator
lastmod 2018-08-17created 2014-02-279 edits
The main timing engine for property animation that also computes the values for the property to be animated. It has all of the core functionality that calculates animation values and contains the timing details of each animation, information about whether an animation repeats, listeners that receive update events, and the ability to set custom types to evaluate. There are two pieces to animating properties: calculating the animated values and setting those values on the object and property that is being animated. ValueAnimator does not carry out the second piece, so you must listen for updates to values calculated by the ValueAnimator and modify the objects that you want to animate with your own logic. See the section about Animating with ValueAnimator for more information.
Compared to the previous Animation approach, Animation can be applied to anything, including Objects and not just Views.
- TimeInterpolator
- TypeEvaluator
- duration
- startPropertyValue
- endPropertyValue
How to use ValueAnimator
It can be used as follows.
ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
animation.setDuration(1000);
animation.start()
// or (for Object)
ValueAnimator animation = ValueAnimator.ofObject(new MyTypeEvaluator(), startPropertyValue, endPropertyValue);
animation.setDuration(1000);
animation.start();
ValueAnimator does not apply any actual change directly to the Object. To do that, you must attach an AnimationListener, get the computed value through getAnimatedValue, and apply the change.
To use ObjectAnimator, the following must be observed.
- A setter method for the Property Name must exist, following camel case notation. (if foo is the setter, setFoo() )
- Option 1 : Add a setter
- Option 2 : Create a Wrapper class and add a setter.
- Option 3 : Use ValueAnimator instead.
- This is handled using the onAnimationUpdate() callback.
- For cases such as changing a View's RGB value, there is no need to do a separate invalidation in onAnimaionUpdate. This is because such Drawables already have setAlpha and getAlpha methods prepared, and the property would already have been specified by that name, such as alpha.
Favorite site
References
-
Xicnt.blog.me_-_Android_Property_Animation.pdf ↩