Android Properties Animation (ii)

Source: Internet
Author: User

Previous article (Link: http://blog.csdn.net/jerehedu/article/details/45305683), we have a simple understanding of property animation, and the actual use of Objectanimator, Animatorset finished a simple example, this time continue to learn the rest of the content.

First, let's look at how the animation is calculated. From the official documentation we can see the following picture:

The diagram depicts the main classes that work with each other during animation, where the Valueanimator object can record the animation time trajectory. The valueanimator contains a timeinterpo;ator, which is used to describe the time interpolation of an animation, and contains a typeevaluator that specifies how the animation property values are computed.

After you create a valueanimator and start the animation, Valueanimator calculates a score of 0 to 1 during the animation run, which is used to represent the percentage of the entire animation run. When Valueanimator calculates the good one run score, the current set of Timeinterpolator is called to calculate the interpolation fraction, and when the interpolation score is calculated, Valueanimator calls Typeevaluator to calculate the value of the attribute based on the interpolated fraction.

The Valueanmiator here is the parent of the objectanimator we used in the previous chapter, and it is important to note that Valueanimator does not manipulate an object or property directly, which means that if we want to use Valueanimator to complete the animation, You must add a listener for Valueanimator:

                Valueanimator animator = Valueanimator.offloat (0.0f, 360f);                Animator.addupdatelistener (New Valueanimator.animatorupdatelistener () {                    @Override public                    Void Onanimationupdate (Valueanimator animation) {                        //Current animation progress                        float Curvalue = (float) animation.getanimatedvalue ();                        Imageview.setrotationx (Curvalue);                    }                });                Animator.setduration (4000);                Animator.setinterpolator (New Decelerateinterpolator ());                Animator.start ();

  :

When Valueanimator Ofint, offloat can not satisfy the demand is, we may use the Ofobject method, this time needs to inherit typeevaluaot<t> custom.

In addition to Animatorupdatelistener, the property animation framework provides us with a Animator.animatorlistener listener:

Android provides a default implementation of this listener animatorlisteneradapter, we only need to inherit this class, the reference code is as follows:

Animator.addlistener (New Animatorlisteneradapter () {                    @Override public                    void Onanimationend (animator Animation) {                        propertyvaluesholder Holderx =propertyvaluesholder.offloat ("Translationx", 0f,260f);                        Propertyvaluesholder holdery=propertyvaluesholder.offloat ("Translationy", 0f,260f);                        Objectanimator.ofpropertyvaluesholder (Imageview,holderx,holdery)                                . Setduration (4000)                                . Start ();                    }                );

Properties that are commonly used in property animations are:

Translationx, translatioiny: Coordinate values relative to the left and top of the layout container

Rotation, RotationX, RotationY: Rotation properties

Scalx, Scaly: Scaling Properties

Pivotx, Pivoty: Sets the center point of rotation, scaling

X, y: coordinates

Alpha: Transparency

As with view animations, we can also use XML documents to write property animations, in addition to using Java code to complete animations. Note that the XML file's directory is no longer anim, but to create a new animator folder under Res for storage.

  X The ml code is as follows:

<set xmlns:android= "http://schemas.android.com/apk/res/android"    android:ordering= "sequentially" >    <set android:ordering= "Together" >        <objectanimator android:propertyname= "Translationx"            android: valuefrom= "0"            android:valueto= "260"            android:duration= "" "Android:valuetype=" Floattype "/>        <objectanimator android:propertyname= "translationy"            android:valuefrom= "0"            android:valueto= "260"            android:valuetype= "Floattype"            android:duration= "/>    </set>    <" Objectanimator        android:propertyname= "Alpha"        android:valuefrom= "1"        android:valueto= "0"        android:duration= "/></set>"

Use in Java:

Animatorset Animatorset = (animatorset) animatorinflater.loadanimator (this,r.animator.myanimator);                Animatorset.settarget (ImageView);                Animatorset.start ();

Students who want to learn more, you can click to download the source code, see more detailed content!

Android Properties Animation (ii)

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.