Simple Anatomy of Android properties animation

Source: Internet
Author: User

Run:

Look at the layout file first, Activity_main.xml:

1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3Xmlns:tools= "Http://schemas.android.com/tools"4Android:layout_width= "Match_parent"5android:layout_height= "Match_parent"6tools:context= "Com.example.administrator.animatordemo.MainActivity" >7 8<ImageView9Android:id= "@+id/iv_icon"TenAndroid:layout_width= "Wrap_content" Oneandroid:layout_height= "Wrap_content" Aandroid:layout_marginleft= "10DP" -android:layout_margintop= "10DP" -Android:scaletype= "Centercrop" theandroid:src= "@mipmap/ic_launcher_round"/> -  -<LinearLayout -Android:layout_width= "Wrap_content" +android:layout_height= "Wrap_content" -Android:layout_alignparentbottom= "true" +Android:layout_centerhorizontal= "true" Aandroid:orientation= "Vertical" > at  -<Button -Android:id= "@+id/btn_click1" -Android:layout_width= "Wrap_content" -android:layout_height= "Wrap_content" -Android:layout_centerinparent= "true" inandroid:layout_gravity= "Center" -android:text= "StartAnimator1"/> to  +<Button -Android:id= "@+id/btn_click2" theAndroid:layout_width= "Wrap_content" *android:layout_height= "Wrap_content" $Android:layout_centerinparent= "true"Panax Notoginsengandroid:layout_gravity= "Center" -android:text= "StartAnimator2"/> the  +<Button AAndroid:id= "@+id/btn_click3" theAndroid:layout_width= "Wrap_content" +android:layout_height= "Wrap_content" -Android:layout_centerinparent= "true" $android:layout_gravity= "Center" $android:text= "StartAnimator3"/> -  -</LinearLayout> the  -</RelativeLayout>

Then look at the Java code, Mainactivity.java:

1  PackageCom.example.administrator.animatordemo;2 3 ImportAndroid.animation.AnimatorSet;4 ImportAndroid.animation.ObjectAnimator;5 ImportAndroid.animation.PropertyValuesHolder;6 ImportAndroid.os.Bundle;7 Importandroid.support.v7.app.AppCompatActivity;8 ImportAndroid.view.View;9 ImportAndroid.widget.Button;Ten ImportAndroid.widget.ImageView; One  A  Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener { -  -     PrivateButton Btn_click1; the     PrivateButton Btn_click2; -     PrivateButton Btn_click3; -     PrivateImageView Iv_icon; -  + @Override -     protected voidonCreate (Bundle savedinstancestate) { +         Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); at Initview (); -     } -  -     Private voidInitview () { -Iv_icon =(ImageView) Findviewbyid (R.id.iv_icon); -Btn_click1 =(Button) Findviewbyid (R.ID.BTN_CLICK1); inBtn_click1.setonclicklistener ( This); -Btn_click2 =(Button) Findviewbyid (R.ID.BTN_CLICK2); toBtn_click2.setonclicklistener ( This); +Btn_click3 =(Button) Findviewbyid (R.ID.BTN_CLICK3); -Btn_click3.setonclicklistener ( This); the     } *  $ @OverridePanax Notoginseng      Public voidOnClick (View v) { -         Switch(V.getid ()) { the              CaseR.id.btn_click1: +StartAnimator1 ();//in this case, three animations will be executed simultaneously A                  Break; the              CaseR.id.btn_click2: +StartAnimator2 ();//The effect is consistent with StartAnimator1 (), and if compared with the first, this method is recommended -                  Break; $              CaseR.id.btn_click3: $ StartAnimator3 (); -                  Break; -         } the     } - Wuyi     /** the * Rotate Animation: Rotation - * Horizontal animation: Translationx Wu * Vertical animation: Translationy -      */ About     Private voidStartAnimator1 () { $Objectanimator.offloat (Iv_icon, "rotation", 0F, 200F). Setduration (1000). Start (); -Objectanimator.offloat (Iv_icon, "Translationx", 0F, 200F). Setduration (1000). Start (); -Objectanimator.offloat (Iv_icon, "Translationy", 0F, 200F). Setduration (1000). Start (); -     } A  +     Private voidStartAnimator2 () { thePropertyvaluesholder P1 = propertyvaluesholder.offloat ("Rotation", 0F, 200F); -Propertyvaluesholder P2 = propertyvaluesholder.offloat ("Translationx", 0F, 200F); $Propertyvaluesholder p3 = propertyvaluesholder.offloat ("Translationy", 0F, 200F); theObjectanimator.ofpropertyvaluesholder (Iv_icon, p1, p2, p3). Setduration (+). Start ();//The first parameter is the control object, and the latter argument is a variable-length array the     } the  the     Private voidStartAnimator3 () { -Objectanimator Animator1 = objectanimator.offloat (Iv_icon, "rotation", 0F, 200F); inObjectanimator Animator2 = objectanimator.offloat (Iv_icon, "Translationx", 0F, 200F); theObjectanimator Animator3 = objectanimator.offloat (Iv_icon, "Translationy", 0F, 200F); theAnimatorset set =NewAnimatorset (); About //Set.playtogether (Animator1, Animator2, Animator3);//three animations executed simultaneously theSet.playsequentially (Animator1, Animator2, Animator3);//three animations executed sequentially theSet.setduration (1000); the Set.start (); +     } -}

To illustrate, in StartAnimator3 () This method, Animatorset collection in addition to Playtogether and playsequentially two methods, there are play methods. For example, it can be used like this:

Set.paly (Animator2). with (Animator3), Set.play (Animator1). After (Animator2);

With this effect, the horizontal and vertical animations are performed at the same time, and then the rotation animation is performed. With a few methods such as (), after (), and even the Before () method, we can do this in a detailed sequential processing of a collection of properties.

Is it a powerful feature? This way, is also the property animation inside the most used one way.

Simple Anatomy of Android properties 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.