Android Animation three: Property Animation (top)

Source: Internet
Author: User

To complete this Android animation series, previously wrote the View Animation and drawable Animation, followed by the last of three kinds of animation, property Animation, which is the most powerful part of Android animation, It is also a relatively complex part.

The difference between property animation and value animation

Property Animation translated as attribute animation, introduced from the Android3.0, compared with the view Animation, the official more recommend the developer to use Properties Animation. The differences between view animation and property animation are explained below:

1,view animation can only modify the components visible on the interface, and the property animation can modify some components that are not visible on the interface, such as modifying a value of type float. (I was surprised to see that at first, but that's exactly what it was, and it's going to explain how to apply it to the interface animation)

2,view Animation When you change a component by animating it, it's just another place on the screen, and the response position of the component is still unchanged, like a button on the left side of the screen, moving to the right through the View animation, although the button on the screen is on the right side, But you still need to click on the left side of the original position, the button will respond.

3,proerty animation can modify the properties of view controls that many view animation cannot modify, such as the background color.

Next, we 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 (+); Animation.start (); Animation.addupdatelistener (New Animatorupdatelistener () {@Overridepublic void Onanimationupdate (valueanimator Animation) {System.out.println ("onanimationupdate =" + Animation.getanimatedvalue ());//can update the UI here}});

From this example to see, let a floating-point number of property animator change, what role? You just need to use addupdatelistener and then listen to the onanimationupdate, use Getanimatedvalue () to get the updated value, and then you use that value in the UI and refresh the interface. In this way, the floating-point property animator and the interface animation effect is connected together.
The onanimationupdate above is related to the animation update cycle and is called once per cycle. The default period is 10ms, but in fact it depends on how busy the system is.
Valueanimator can also be used for integer types, except for floating-point types.
Objectanimator
Take a look at objectanimator,t it makes Valueanimator subclasses, Valueanimator need us to listen onanimationupdate to apply the changed values to a UI property. The difference with objectanimation is that it automatically applies the changed values to a UI property. Let's say the following example.
Objectanimator anim = objectanimator.offloat (foo, "Alpha", 0f, 1f); Anim.setduration (+); Anim.start ()

The above objectanimator.offloat four parameters, respectively, are the function of the object, as well as the function of the property, the starting value, the target value. Where the properties of the action must have a setter method in the Action object. With Objectanimator, update values are automatically applied to the properties declared in the construction method.
The following points need to be noted when using Objectanimator:
1, when you want to use Objectanimator to update a property of an object, need to have a setter method of the property, such as "alpha" above, then the Foo object belongs to the class, need to have Setalpha method, The system uses this method to apply the most recent change values to the UI properties. When you use Objectanimator to change a property, and there is no setter method for that property, you can do this:
1), if you have permission to modify the code, add a setter Method 2 directly for the property, use a wrapper class, add a setter method to the property in the class, and then apply the received value to the property in the method. 3), use Valueanimator.
2, if the target value is provided only when the Objectanimator is constructed, and there is no starting value, you will need to provide a getter method in addition to the setter method for the property.

Take a look at the following small example, using Objectanimator to change the font color
Objectanimator Coloranim = objectanimator.ofint (TV, "TextColor", 0xff008271, color.red); coloranim.setduration (4000); Coloranim.setevaluator (New Argbevaluator ()); Coloranim.setrepeatcount (+); Coloranim.start ();


Note Two questions, the first one is that the color value must be written as 0xff008271, and cannot be written as a 6-bit hexadecimal color value. The second is that you need to set the typeevaluator here, otherwise the color change will be flashing. As you can see from here, Typeevaluator's function is to calculate the value of each gradient of the animation, to control the gradient, while the Argbevaluator is the system provides to handle the change in color values. We'll explain how to customize the Typeevaluator later.

Animatorset
With Animatorset, you can have multiple animations at the same time, either in a sequential order, or you can specify that an animation is delayed after a certain period of time. Take a look at the example given by 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 ();


About the pre-and post-order of the animation, the main With,before,after three methods, with, indicating simultaneous, before and after to represent the relationship. It is easy to see from the code above that Animatorset can contain multiple objectanimator and can contain animatorset.

Animation Listeners
The listener of animation is mainly to do some action at some key moments of animation, such as Start, end, refresh of each frame, repetition and so on. The animation mainly has a listener,animator.animatorlistener and Valueanimator.animatorupdatelistener
Animator.animatorlistener

The following callback methods are mainly provided
    • Onanimationstart ()
    • Onanimationend ()
    • Onanimationrepeat ()
    • Onanimationcancel () will callback the method when the animation is canceled, and will also callback Onanimationend ()
The listener provides so many methods that are not implemented, and if you only need to implement one of these methods, you have to implement all the methods as long as you inherit Animatorlistener. To avoid this problem, you can choose to inherit Animatorlisteneradapter, which provides an empty implementation for each of the above methods, inheriting the class, you only need to implement the method you want to use.
Valueanimator.animatorupdatelistener

The Onanimationupdate () callback method is mainly provided, which executes once in each frame of the animation and is mainly used for Valueanimator

The principle of Typeevaluator
The animation is each frame update, and the animation is divided into how many frames, each frame should make how many changes, the two problems are given to Timeinterpolator and typeevaluator,timeinterpolator to calculate each frame at what time, Then use Typeevaluator to calculate how many changes you need to make to the animation's properties at each frame.

Because the property animation too many knowledge points, this blog first written here, the next blog to explain other knowledge.

Android Animation three: Property Animation (UP)

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.