Android Animation specific explanation four create animations

Source: Internet
Author: User

UseValueanimatorto animate

A collection of values that specify some type of int, float, or color. valueanimator enables you to animate these types of values. You need to get a valueanimator object by calling a factory method of Valueanimator . For example:ofint (), offloat (), or ofobject (). Like what:

Valueanimator animation = Valueanimator.offloat (0f, 1f); Animation.setduration (+); Animation.start ();     

In this code, when the start () method runs, the value of the animation that is between 0 and 1 in the MS is started.

You can also animate by specifying a type of your own definition as follows:

Valueanimator animation = Valueanimator.ofobject (new Mytypeevaluator (), Startpropertyvalue, Endpropertyvalue); Animation.setduration (+); Animation.start ();

In this code, when the start () method runs, valueanimator calculates the value of the animation between Startpropertyvalue and Endpropertyvalue in Ms. Use the calculation logic provided by Mytypeevaluator.

However, in the code snippet above, there is actually no effect on the object. Because Valueanimator does not manipulate objects or properties directly. You should change the object yourself according to the calculated value yourself. You can define a listener to handle the important events of Valueanimator during the animation process, such as updating frames.

When the listeners were implemented. You will be able to get the animated value by calling Getanimatedvalue () when the frame is refreshed.


UseObjectanimatorto animate

Objectanimator is a subclass of Valueanimator and incorporates the computational power of the time engine and Valueanimator to animate the properties of an object, which is specified by name.

This makes it easier to animate an object, because you no longer need to implement Valueanimator.animatorupdatelistener, because the properties you want to animate are self-updating.

Instantiating objectanimator and instantiating valueanimator are very similar, but you also need to specify the names of the properties of objects and objects:

Objectanimator anim = objectanimator.offloat (foo, "Alpha", 0f, 1f); Anim.setduration (+); Anim.start ();

To enable Objectanimator to update the properties correctly. You have to do it as follows:

· The property of the object you want to animate must have a setter function, like this:set<propertyname> (). Because objectanimator actively updates properties during an animation, the target property must be manipulated through this setter method. For example, if the name of the property is Foo, you must have a Setfoo () method. Assuming this method does not exist, you have three options:

· Suppose you can, add yourself to the Setter method (nonsense).

· Encapsulate the class of this object with another class, so you have rewrite power. The setter that joins the properties of the original class in the encapsulation class.

· Use valueanimatorinstead.

· Suppose that in one of the factory methods of Objectanimator , you just specify a values ... The number of references. This parameter is considered the end value of the animation.

Therefore, the object property of your animation must have a getter method for getting the start value. The Getter method must have a get<propertyname> ()-like format.

Like what. Assuming that the attribute name is foo, you need to have a Getfoo () method.

· The getter (if required) of the properties you want to animate and the setter method must handle the same type of data as the start and end values that you specify for objectanimator . For example, suppose you create a objectanimatorsuch as the following, you must have the Targetobject.setpropname (float) method and the Targetobject.getpropname (float) method:

Objectanimator.offloat (TargetObject, "propname", 1f)

· differs from the object and property of the animation. You may need to call the view's invalidate () method to force the screen to redraw itself with the new animated data. You should do so in the onanimationupdate () callback. Like what. Animating the Color property of a Drawable object causes the screen to be updated only when the object redraws itself. View's full property setters, such as Setalpha () and Settranslationx (), will invalidate the view, so you don't have to invalidate the view when calling these methods.


UseAnimatorsetCombining multiple animations

In very many cases, you may want to play an animation that depends on the start time or end time of other animations. The Android system allows you to bind multiple animations into a animatorset , so you can specify whether to play at the same time. or play them separately. or a delay before playing. You can also nest each animatorset object with each other.

The following demo sample code is taken from the sample bouncing Balls (small change). It plays the following animated objects in this manner:

1. Play Bounceanim.

2. Play SquashAnim1, SQUASHANIM2, STRETCHANIM1, and stretchAnim2 at the same time.

3. Play Bouncebackanim.

4. Play Fadeanim.

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 ();

Previous: Android animation specific explanation three animation API overview

Next: Android Animation specific explanation five layout, interpolation and evaluators

Android Animation specific explanation four create animations

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.