Android Animation One

Source: Internet
Author: User

Two forms of Android animation

Layout file

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Android:paddingbottom= "@dimen/activity_vertical_margin"Tools:context=". Mainactivity ">    <ImageViewAndroid:layout_width= "40DP"Android:layout_height= "40DP"Android:id= "@+id/imageview"Android:onclick= "click"android:src= "@android:d rawable/ic_lock_idle_alarm" />    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Move"Android:onclick= "Move"Android:id= "@+id/button"Android:layout_alignparentbottom= "true"Android:layout_centerhorizontal= "true"Android:layout_marginbottom= "102DP" /></Relativelayout>

Code files:

 Packagecom.androidbase.zjn.androidbase;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;Importandroid.view.animation.TranslateAnimation;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View view) {Toast.maketext ( This, "clicked", Toast.length_long). Show (); }     Public voidMove (view view) {Translateanimation animation=NewTranslateanimation (0,200,0,0); Animation.setduration (1000);//exercise for 1 secondsAnimation.setfillafter (true);//the end result of the animation stays in its original positionImageView ImageView =(ImageView) Findviewbyid (R.id.imageview); Imageview.startanimation (animation);//ImageView Binding Animations    }}

Effect of using animator:

ImageView ImageView =(ImageView) Findviewbyid (R.id.imageview);        Objectanimator.offloat (ImageView,"Translationx", 0,200f). Setduration (+).start ();        Objectanimator.offloat (ImageView,"Translationy", 0,200f). Setduration (+).start ();        Objectanimator.offloat (ImageView,"Rotation", 0,360f). Setduration (+). Start ();

Use a few together to understand:

 ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);        Propertyvaluesholder P1  = propertyvaluesholder.offloat ("Rotation", 0,360f); Propertyvaluesholder P2  = propertyvaluesholder.offloat ("Translationx", 0,200f        ); Propertyvaluesholder P3  = propertyvaluesholder.offloat ("Translationy", 0,200f        ); Objectanimator.ofpropertyvaluesholder (IMAGEVIEW,P1,P2,P3). Setduration ( +). Start (); 
    // Show several animations together        ImageView ImageView =(ImageView) Findviewbyid (R.id.imageview);        Objectanimator animator1=objectanimator.offloat (ImageView, "Translationx", 0, 200F);        Objectanimator Animator2=objectanimator.offloat (ImageView, "Translationy", 0, 200F);        Objectanimator Animator3=objectanimator.offloat (ImageView, "rotation", 0, 360F);         New Animatorset ();        Set.playtogether (ANIMATOR1,ANIMATOR2,ANIMATOR3);        Set.setduration (+);        Set.start ();

The complete code:

 Packagecom.androidbase.zjn.androidbase;ImportAndroid.animation.AnimatorSet;ImportAndroid.animation.ObjectAnimator;ImportAndroid.animation.PropertyValuesHolder;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;Importandroid.view.animation.TranslateAnimation;ImportAndroid.widget.ImageView;ImportAndroid.widget.Toast; Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }     Public voidClick (View view) {Toast.maketext ( This, "clicked", Toast.length_long). Show (); }     Public voidMove (view view) {//Translateanimation is just a simple move.//translateanimation animation = new Translateanimation (0,200,0,0);//animation.setduration (+);//exercise for 1 seconds//Animation.setfillafter (true);//the end result of the animation stays in its original position//ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);//imageview.startanimation (animation);//ImageView Binding Animations//ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);//objectanimator.offloat (ImageView, "Translationx", 0,200f). Setduration (+). Start ();//objectanimator.offloat (ImageView, "Translationy", 0,200f). Setduration (+). Start ();//objectanimator.offloat (ImageView, "rotation", 0,360f). Setduration (+). Start ();//The above optimization//ImageView ImageView = (ImageView) Findviewbyid (R.id.imageview);//propertyvaluesholder p1 = propertyvaluesholder.offloat ("Rotation", 0,360f);//propertyvaluesholder P2 = propertyvaluesholder.offloat ("Translationx", 0,200f);//Propertyvaluesholder p3 = propertyvaluesholder.offloat ("Translationy", 0,200f);//Objectanimator.ofpropertyvaluesholder (IMAGEVIEW,P1,P2,P3). Setduration (+). Start (); //Show several animations togetherImageView ImageView =(ImageView) Findviewbyid (R.id.imageview); Objectanimator Animator1=objectanimator.offloat (ImageView, "Translationx", 0, 200F); Objectanimator Animator2=objectanimator.offloat (ImageView, "Translationy", 0, 200F); Objectanimator Animator3=objectanimator.offloat (ImageView, "rotation", 0, 360F); Animatorset Set=NewAnimatorset (); //Set.playtogether (ANIMATOR1,ANIMATOR2,ANIMATOR3); //set.playsequentially (ANIMATOR1,ANIMATOR2,ANIMATOR3);//Animate in sequenceSet.play (Animator1). with (Animator2). before (Animator3);//Animate 1 and animate 21 blocks, let's animate after 3Set.setduration (1000);    Set.start (); }}

Android Animation One

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.