The
Instantiating an
ObjectAnimator is a subclass of the ValueAnimator (discussed in the previous section) and combines the timing engine and value computation of ValueAnimator
with the ability to animate a named property of a target object. This
makes animating any object much easier, as you no longer need to
implement the ValueAnimator.AnimatorUpdateListener, because the animated property updates automatically.Instantiating an
ObjectAnimator is similar to a ValueAnimator, but you also specify the object and the name of that object's property (as a String) along with the values to animate between:ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "alpha", 0f, 1f); anim.setDuration(1000); anim.start();To have the
ObjectAnimator update properties correctly, you must do the following:- The object property that you are animating must have a setter function (in camel case) in the form of
set<propertyName>(). Because theObjectAnimatorautomatically updates the property during animation, it must be able to access the property with this setter method. For example, if the property name isfoo, you need to have asetFoo()method. If this setter method does not exist, you have three options:- Add the setter method to the class if you have the rights to do so.
- Use a wrapper class that you have rights to change and have that wrapper receive the value with a valid setter method and forward it to the original object.
- Use
ValueAnimatorinstead.
- If you specify only one value for the
values...parameter in one of theObjectAnimatorfactory methods, it is assumed to be the ending value of the animation. Therefore, the object property that you are animating must have a getter function that is used to obtain the starting value of the animation. The getter function must be in the form ofget<propertyName>(). For example, if the property name isfoo, you need to have agetFoo()method. - The
getter (if needed) and setter methods of the property that you are
animating must operate on the same type as the starting and ending
values that you specify to
ObjectAnimator. For example, you must havetargetObject.setPropName(float)andtargetObject.getPropName(float)if you construct the followingObjectAnimator:ObjectAnimator.ofFloat(targetObject, "propName", 1f)
- Depending on what property or object you are animating, you might need to call the
invalidate()method on a View to force the screen to redraw itself with the updated animated values. You do this in theonAnimationUpdate()callback. For example, animating the color property of a Drawable object only cause updates to the screen when that object redraws itself. All of the property setters on View, such assetAlpha()andsetTranslationX()invalidate the View properly, so you do not need to invalidate the View when calling these methods with new values. For more information on listeners, see the section about Animation Listeners.
Content is superb Android Online Training
ReplyDelete