Android Animation 3: Property Animation (I)

Source: Internet
Author: User

Android Animation 3: Property Animation (I)

To complete this Android Animation series, I previously wrote View Animation and Drawable Animation. Next I will explain the last of the three animations, Property Animation, which is also the most powerful part of Android Animation, it is also a relatively complex part.

Difference between Property Animation and Value Animation

Property Animation is translated as Property Animation and introduced from Android3.0. Compared with View Animation, developers are recommended to use Property Animation. The following describes the differences between View Animation and Property Animation:

1. View Animation can only modify visible components on the interface, while Property Animation can also modify components that are not visible on the interface, such as modifying a float value. (This is strange when I saw it at the beginning, but it does. I will explain how to apply it to interface animation later)

2. When View Animation changes a component through the Animation effect, it is actually drawn in another place on the screen, and the response position of the component remains unchanged, for example, a button on the left of the screen, you can use View Animation to move to the right. Although you can see the button on the screen to the right, you still need to click the original position on the left to respond.

3. Proerty Animation can modify attributes of View controls that cannot be modified by View Animation, such as the background color.

Next we will mainly explain the usage of Property Animation, including ValueAnimator, ObjectAnimator, and AnimatorSet.

 

ValueAnimator
First look at the following code
ValueAnimator animation = ValueAnimator. ofFloat (0f, 1f); animation. setDuration (1000); animation. start (); animation. addUpdateListener (new AnimatorUpdateListener () {@ Overridepublic void onAnimationUpdate (ValueAnimator animation) {System. out. println (onAnimationUpdate = + animation. getAnimatedValue (); // you can update the UI here }});

From this example, we can see that it is helpful to let a floating point number change the Property Animator? You only need to use addUpdateListener, and then use getAnimatedValue () to obtain the update value in the onAnimationUpdate listener. Then you can use this value on the UI and refresh the interface. In this way, the floating point Property Animator is associated with the interface animation effect.
The above onAnimationUpdate is related to the animation update cycle, which is called once every cycle. The default period is 10 ms, but it depends on how busy the system is.
ValueAnimator can act on both floating-point and integer types.
ObjectAnimator
Let's take a look at ObjectAnimator. t it makes the ValueAnimator subclass. ValueAnimator needs to listen to onAnimationUpdate to apply the changed value to a UI attribute. The difference between ObjectAnimation is that it can automatically apply the changed value to a UI attribute. For example, the following example.
ObjectAnimator anim = ObjectAnimator.ofFloat(foo, alpha, 0f, 1f);anim.setDuration(1000);anim.start()

The preceding four parameters of ObjectAnimator. ofFloat are the target object, attributes, start value, and target value. The property to be applied must have a setter method in the target object. With ObjectAnimator, the updated value is automatically applied to the attributes declared in the constructor.
When using ObjectAnimator, pay attention to the following points:
1. If you want to use ObjectAnimator to update an attribute of an object, you need a setter method for this attribute, such as the above "alpha", which is in the class to which the foo object belongs, the setAlpha method is required. The system uses this method to apply the latest change value to the UI attribute. When ObjectAnimator is used to change an attribute without the setter method of this attribute, you can do this:
1) if you have the permission to modify the code, you can directly add a setter method for this property. 2) use a packaging class and add a setter method for this property in this class, then, the method applies the received value to this attribute. 3), use ValueAnimator.
2. If only the target value is provided when constructing ObjectAnimator, but there is no starting value, you need to provide the setter method for the attribute and the getter method.

Let's look at the following small example and use ObjectAnimator to change the font color.
ObjectAnimator colorAnim = ObjectAnimator.ofInt(tv, textColor,0xff008271, Color.RED);colorAnim.setDuration(4000);colorAnim.setEvaluator(new ArgbEvaluator());colorAnim.setRepeatCount(100);colorAnim.start();


Note two problems. The first one is that the color value must be 0xff008271, rather than a six-digit hexadecimal color value. The second is that TypeEvaluator needs to be set here, otherwise the color change will flash. From this point, we can find that the role of TypeEvaluator is to calculate the value of each gradient of the animation and control the gradient effect. ArgbEvaluator is provided by the system to handle color value changes. The following describes how to customize TypeEvaluator.

AnimatorSet
You can use AnimatorSet to enable multiple animations to be executed simultaneously or in the previous or later order. You can also specify that an animation will be executed after a certain period of time. Let's take a look at the examples provided on the official website.
AnimatorSet bouncer = new AnimatorSet();bouncer.play(bounceAnim).before(squashAnim1);bouncer.play(squashAnim1).with(squashAnim2);bouncer.play(squashAnim1).with(stretchAnim1);bouncer.play(squashAnim1).with(stretchAnim2);bouncer.play(bounceBackAnim).after(stretchAnim2);ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, alpha, 1f, 0f);fadeAnim.setDuration(250);AnimatorSet animatorSet = new AnimatorSet();animatorSet.play(bouncer).before(fadeAnim);animatorSet.start();


The following three methods are used to show the sequence of the animation: with, before, and after. with indicates that the animation is executed simultaneously, and before and after indicates the relationship between the animation and the animation. From the code above, it is easy to find that the AnimatorSet can contain multiple ObjectAnimator and the AnimatorSet.

Animation Listeners
The Listener of an animation is used to perform operations at key moments of the animation, such as start, end, refresh, and repeat of each frame. The animation mainly includes Listener, Animator. AnimatorListener, and ValueAnimator. AnimatorUpdateListener.
Animator. AnimatorListener

The following callback method is provided: onAnimationStart ()
OnAnimationEnd ()
OnAnimationRepeat ()
OnAnimationCancel () calls back this method when the animation is canceled. It also calls back onAnimationEnd (). This Listener provides so many unimplemented methods. If you only need to implement one of the methods, however, you only need to inherit the AnimatorListener to implement all the methods. To avoid this problem, you can choose to inherit the AnimatorListenerAdapter, which provides an empty implementation for the above methods and inherits the class, you only need to implement the method you want to use.
ValueAnimator. AnimatorUpdateListener

The onAnimationUpdate () callback method is provided. This method is executed once every frame of the animation and is mainly used in ValueAnimator.

Principles of TypeEvaluator
The animation is updated at each frame, and the animation is divided into several frames and how many changes should be made at each frame. The two problems are handed over to TimeInterpolator and TypeEvaluator respectively, and TimeInterpolator calculates the time at which each frame is updated, then, the TypeEvaluator is used to calculate the number of changes that need to be made to the attributes of the animation on each frame.

 

Related Article

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.