Android Animation--Property animation

Source: Internet
Author: User

I. Overview

Android added new features after API11, mainly through the object's property dynamic change to achieve animation effect, and thus greatly expanded the role of objects, and because directly to the properties of the transformation, animation is more rich. Mainly Valueanimator, Objectaninmator, Animatorset and other concepts.

Classification and use of attribute animation

1, objectanimator inherit from Valueanimator, use example as follows

Objectanimator.offloat (Button, "translationy", +). Setduration (+). Start ();

Pass in the Action object, the property name, the change value, and set the relevant parameters, and finally call start to begin the animation.

2, Valueanimator can change any one of the properties, but there is no object, only the change in the value of the property, the use of which can be added to listen. There are two main listening interfaces for property animation, Animateupdatelistener and Animatelistener. One of the former is frame-wise monitoring, the latter is listening to different four states, similar to the View animation monitoring.

When you use this object, you can get the animator property value by frame in Animateupdatelistener's listening method, and animate the object by animating it dynamically. The code is as follows:

Valueanimator Coloranim = Objectanimator.ofint ( This, "BackgroundColor", 0xffff8080,0xff8080ff); Coloranim.addupdatelistener (NewValueanimator.animatorupdatelistener () {@Override Public voidonanimationupdate (valueanimator animation) {intColorValue = (int) Animation.getanimatedvalue ();            Button.setbackgroundcolor (ColorValue);        }        }); Coloranim.setduration (3000); Coloranim.setevaluator (Newargbevaluator ());        Coloranim.setrepeatcount (Valueanimator.infinite);        Coloranim.setrepeatmode (Valueanimator.reverse); Coloranim.start ();

This allows you to animate any property, and there are two different ways to animate any property.

Where Getanimatedvalue is getting the current progress value, which is related to the total value passed in when the object was created, you can also use the Evaluator (estimator) to calculate the desired property value, the code is as follows

                float fraction = animation.getanimatedfraction ();                 = intevaluator.evaluate (fraction,start,end);                Target.requestlayout ();

By obtaining getanimatedfraction to obtain fraction, and then using Intevaluator object to pass in fraction, start (attribute initial value), End (the property value) to calculate the current property values, You can also use a custom evaluator to calculate the property values for a more flexible animation effect.

Requestlayout is called when the view determines that it is not already suitable for an existing zone, to re-complete the measure and layout procedures to refresh the location.

3, Animatorset Animation collection, as the name implies, can set a number of animation effects, the use is very simple, as follows

New Animatorset ();        Set.playtogether (/* animate effect and Object */),                Objectanimator.offloat (/* animate effects and Objects */),                objectanimator.ofint ( /* animating effects and objects */ ));        Set.setduration (). Start ();

In addition, attribute animations can be defined by XML. A brief description of the tags and attributes used.

Property animations are defined in the Res/animator directory.

Label <set> Properties Ordering (together/squentially) The animation's play order in the collection.

Tags <objectAnimator> properties propertyname property name, Durantion duration, Valuefrom, Valueto, startoffset (int) delay time, RepeatCount Repeat Count, Repeatmode (repeat/reverse) Repeat mode, ValueType (inttype/floattype) color properties are not specified.

Tags <animator> similar to <objectAnimator>, no PropertyName property.

Use property animations with the following code after the definition is complete

    Animatorset set = (Animatorset) animatorinflater.loadanimator (this, r.anim.animation_test);        Set.settarget (Mbutton);        Set.start ();

In real-world development, property animations are typically used in code because the XML definition does not get the initial value of the property.

Third, Interpolator Interpolator and estimator evaluator

In property animations, the change in attributes over time is determined by both the Interpolator and the estimator.

1. Interpolator is similar to the view animation. In the property animation, Interpolator returns the ratio of the change of the property, the ratio of the change in time to the parameter. If the time elapses 50%, returns the ratio of the change in the attribute at this point.

2, Evaluator internal evaluate method accepts three parameters, fraction, start, end, according to the three calculations to get the attribute value, wherein the fraction is the change in the proportion of the attribute value.

Custom encoders implement Interpolator or Timeinterpolator, and custom estimators implement Typeevaluator, and if you animate other types except int, float, and color, you must customize the type estimation algorithm.

Iv. animating any attribute

Property animations require that an animated object have a get and set method for the property, where the Get method is used to get the starting value of the property, and the set method is used to assign the property dynamically during animation execution. Therefore, property animation (Objectanimator) cannot be used directly when view cannot provide either method or the provided method cannot set the desired property (such as the SetWidth method of the button).

There are three ways to solve the above problems

1. Add the Get and set methods for the object, which is not possible for the view provided by the system.

2, wrapping the original object with a class, indirectly provides the get and set method, the steps are as follows

    //wrapper class, providing custom get and set methods for view    Private Static classviewwrapper{PrivateView Target; Viewwrapper (View target) { This. target =Target; }         Public intgetwidth () {returntarget.getlayoutparams (). width; }         Public voidSetWidth (intwidth) {Target.getlayoutparams (). Width=width;        Target.requestlayout (); }    }

3, the use of Valueanimator, the above has been introduced.

V. How property animations work

VI. matters of caution

1, Oom problem

2. Memory leaks

An animation of an infinite loop in a property animation needs to close the animation in a timely manner when the activity exits, or it will cause a memory leak due to activity not being released.

3. Compatibility issues

4. Do not use PX

5. Animation element interaction

After API11, the effect of the property animation is to change the view's position, and the view only changes the effect, so the event response can be problematic.

6, the problem of view animation

Because the view animation is an image of the view, there are times when the view cannot be hidden after the animation is finished, so just call clearanimation to clear the animation.

7. Hardware acceleration

For smooth system, animation effect is recommended to turn on hardware acceleration.

Android Animation--Property animation

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.