Android Animation in detail four create animations

Source: Internet
Author: User

· UseValueanimatorto animate

Valueanimator allows you to animate values of these types by specifying a collection of values such as int, float , or color . You need to get a valueanimator object by calling a factory method of Valueanimator , such asOfint (), offloat (), or ofobject (). For example:

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

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

You can also animate by specifying a custom type in the following way:

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

In this code, when the start () method executes, Valueanimator begins to calculate the value of the animation between Startpropertyvalue and Endpropertyvalue in Ms. Use the calculation logic provided by Mytypeevaluator.

However, in the preceding code fragment, there is actually no effect on the object, because valueanimator does not manipulate objects or properties directly. You should modify the object yourself according to the calculated values. You can define a listener to handle valueanimator important events during the animation process, such as updating frames. When the listener is implemented, you can get the animated value by calling Getanimatedvalue () when the frame is refreshed.

· animating with objectanimator

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.animatorupdatelistenerbecause the properties of the animation are automatically updated.

Instantiating Objectanimator is similar to instantiating Valueanimator , but you also need to specify the name of the object and the properties of the object:

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

To enable Objectanimator to update the properties correctly, you must do the following:

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

· If you can, add your own setter method (nonsense).

· Encapsulate the class of this object with another class, so you have rewrite power. In the encapsulation class, add the setter that operates the properties of the original class.

· Use valueanimatorinstead.

· If you are in a factory method in objectanimator , you specify only one of the values ... parameter, 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. For example, if the property name is foo, you need to have a getfoo () method.

· The getter (if necessary) 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 specified for objectanimator . For example, if you create the following Objectanimator, you must have the Targetobject.setpropname (float) method and Targetobject.getpropname (float) method:

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

· Depending on 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. For example, animating the Color property of a Drawable object causes the screen to be updated only when the object redraws itself. All the property setters of the view, such as Setalpha () and Settranslationx (), will invalidate the view, so you do not need to invalidate the view when calling these methods.

· UseAnimatorsetCombining multiple animations

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

The following sample code is taken from the example bouncing Balls (small Change the next). It plays the following animated objects in such a way:

1. Play Bounceanim.

2. Simultaneously play squashAnim1, SQUASHANIM2, STRETCHANIM1, and stretchAnim2.

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 detailed three animation API overview

Android Animation in detail 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.