Android Property Animation --- Property Animation (4)

Source: Internet
Author: User

Use ValueAnimator to create an animation

The ValueAnimator class specifies certain types of animation values during animation playback by setting int, float, or color values during the animation process. Obtain a ValueAnimator object through a factory method of the ValueAnimator class: ofInt (), ofFloat (), ofObject (). For example:

ValueAnimator animation = ValueAnimator. ofFloat (0f, 1f );

Animation. setDuration (1000 );

Animation. start ();

In this Code, when the start () method starts to execute, the ValueAnimator object starts to calculate the animation value between 0 and 1 during the animation period of 1000 milliseconds.

You can also use the following method to specify the Custom Animation type:

ValueAnimator animation = ValueAnimator. ofObject (new MyTypeEvaluator (), startPropertyValue, endPropertyValue );

Animation. setDuration (1000 );

Animation. start ();

In this Code, when the start () method starts execution, the ValueAnimator object uses the logic provided by the MyTypeEvaluator object during the animation period of 1000 milliseconds between startPropertyValue and endPropertyValue, start to calculate the animation value.

However, in the previous code snippet, there is no actual impact on an object, because the ValueAnimator object does not directly perform operations on the object or attribute. The biggest reason for doing so is to use these calculated values to modify the objects that want the animation effect. You can define event listeners in the ValueAnimator class to process important events during animation execution, such as frame update. When the listener is executed, it can call the getAnimateValue () method to obtain the refresh calculation value of the specified frame. For more information about the listener, see "animation listener ".

Use ObjectAnimator to create an animation

The ObjectAnimator class is a subclass of the ValueAnimator class. It combines the computing values of the time series engine and the ValueAnimator object so that the name attributes of the target object can be animated. This makes it easier for any object to have an animation effect. For example, you do not need to implement the ValueAnimator. AnimatorUpdateListener interface because the attributes of the animation are automatically updated.

Instantiating an ObjectAnimator object is similar to instantiating a ValueAnimator object. However, it also needs to specify the name of the animation object and the object animation attribute (using a string) together with parameters during the animation:

ObjectAnimator anim = ObjectAnimator. ofFloat (foo, "alpha", 0f, 1f );

Anim. setDuration (1000 );

Anim. start ();

To correctly update the attributes of the ObjectAnimator object, do the following:

1. The animation effect attribute must have a set <propertyName> formatting method. During the animation processing, the ObjectAnimator object automatically updates the corresponding animation effect attribute, so it must use this setter method to access the corresponding attribute. For example, if the attribute name is foo, you need a setFoo () method. If the setFoo method does not exist, you have three options:

A. If you have the right to do so, you need to add the setter method in this class;

B. Use a package class that you have the right to change, and this package can receive animation values with a valid setting method, and also forward the value to the initial object.

C. Use the ValueAnimator class instead.

2. If you specify a values... This parameter is assumed as the animation end value. Therefore, the animation effect attribute of the object must have a method to obtain the starting value of the animation. The get method must be in the get <propertyName> () format. For example, if the property is foo, there must be a getFoo method.

3. You must specify the start and end values for the ObjectAnimator object of the same type as the animation attribute acquisition (if needed) and setting method. For example, if you construct an ObjectAnimator object like the following, you must have the targetObejct. setPropName (float) and targetObject. getPropName (float) methods:

ObjectAnimator. ofFloat (targetObject, "propName", 1f) 4. based on the animation effects on attributes or objects, you may need to call the invalidate () method on the View object to force the screen to repaint itself when updating the animation effect. Do this in the onAnimationUpdate () callback method. For example, the animation effect of the color attribute of a drawing object is updated to the screen only when the formation redraws itself. All attribute settingers on the View object, such as setAlpha () and setTranslationX (), will invalidate the View object correctly. Therefore, when you call these methods to set new values, you do not need to invalidate the event. For more information about the listener, see "animation listener ".

By FireOfStar
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.