First, Introduction
Second, the Code
1.xml
(1) Activity_main.xml
1<?xml version= "1.0" encoding= "Utf-8"?>2<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "vertical" android:layout_width= "fill_parent"4android:layout_height= "Fill_parent" >5 6<button android:id= "@+id/scalebuttonid" android:layout_width= "Fill_parent"7android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true"8android:text= "Test scale animation effect"/>9 Ten<button android:id= "@+id/rotatebuttonid" android:layout_width= "Fill_parent" Oneandroid:layout_height= "Wrap_content" android:layout_above= "@id/scalebuttonid" Aandroid:text= "Test rotate animation effect"/> - -<button android:id= "@+id/alphabuttonid" android:layout_width= "Fill_parent" theandroid:layout_height= "Wrap_content" android:layout_above= "@id/rotatebuttonid" -android:text= "Test alpha animation effect"/> - -<button android:id= "@+id/translatebuttonid" +Android:layout_width= "Fill_parent" android:layout_height= "Wrap_content" -Android:layout_above= "@id/alphabuttonid" android:text= "Test translate animation effects"/> + A<linearlayout android:orientation= "Vertical" atAndroid:layout_width= "Fill_parent" android:layout_height= "Fill_parent" > - -<imageview android:id= "@+id/imageviewid" -Android:layout_width= "Wrap_content" android:layout_height= "100DP" -Android:layout_centerinparent= "true" android:layout_margintop= "100dip" -android:src= "@drawable/android"/> in</LinearLayout> -</RelativeLayout>
2.java
(1) Mainactivity.java
1 PackageCom.animation1;2 3 Importandroid.app.Activity;4 ImportAndroid.os.Bundle;5 ImportAndroid.view.View;6 ImportAndroid.view.View.OnClickListener;7 Importandroid.view.animation.AlphaAnimation;8 Importandroid.view.animation.Animation;9 ImportAndroid.view.animation.AnimationSet;Ten Importandroid.view.animation.RotateAnimation; One Importandroid.view.animation.ScaleAnimation; A Importandroid.view.animation.TranslateAnimation; - ImportAndroid.widget.Button; - ImportAndroid.widget.ImageView; the - Public classMainactivityextendsActivity { - - PrivateImageView ImageView =NULL; + PrivateButton Rotatebutton, Scalebutton, Alphabutton, Translatebutton =NULL; - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); - -ImageView =(ImageView) Findviewbyid (R.ID.IMAGEVIEWID); - -Rotatebutton =(Button) Findviewbyid (R.id.rotatebuttonid); -Scalebutton =(Button) Findviewbyid (R.id.scalebuttonid); inAlphabutton =(Button) Findviewbyid (R.id.alphabuttonid); -Translatebutton =(Button) Findviewbyid (R.id.translatebuttonid); to +Rotatebutton.setonclicklistener (NewRotatebuttonlistener ()); -Scalebutton.setonclicklistener (NewScalebuttonlistener ()); theAlphabutton.setonclicklistener (NewAlphabuttonlistener ()); *Translatebutton.setonclicklistener (NewTranslatebuttonlistener ()); $ Panax Notoginseng } - the Private classRotatebuttonlistenerImplementsOnclicklistener { + @Override A Public voidOnClick (View v) { theAnimationset Animationset =NewAnimationset (true); +Rotateanimation rotateanimation =NewRotateanimation (0, 360, - animationset.relative_to_parent, 1f, $ animation.relative_to_parent, 0f); $Rotateanimation.setduration (5000); - animationset.addanimation (rotateanimation); - imageview.startanimation (animationset); the } - }Wuyi the Private classScalebuttonlistenerImplementsOnclicklistener { - @Override Wu Public voidOnClick (View v) { -Animationset Animationset =NewAnimationset (true); AboutScaleanimation scaleanimation =NewScaleanimation (1, 0.1f, 1, 0.1f, $Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f); - animationset.addanimation (scaleanimation); -Animationset.setstartoffset (1000); -Animationset.setfillafter (true); AAnimationset.setfillbefore (false); +Animationset.setduration (2000); the imageview.startanimation (animationset); - } $ } the the Private classAlphabuttonlistenerImplementsOnclicklistener { the @Override the Public voidOnClick (View v) { -Animationset Animationset =NewAnimationset (true); inAlphaanimation alphaanimation =NewAlphaanimation (1, 0); theAlphaanimation.setduration (1000); the animationset.addanimation (alphaanimation); About imageview.startanimation (animationset); the } the } the + Private classTranslatebuttonlistenerImplementsOnclicklistener { - @Override the Public voidOnClick (View v) {BayiAnimationset Animationset =NewAnimationset (true); theTranslateanimation translateanimation =NewTranslateanimation ( the animation.relative_to_self, 0f, -Animation.relative_to_self, 0.5f, - animation.relative_to_self, 0f, theAnimation.relative_to_self, 1.0f); theTranslateanimation.setduration (1000); the animationset.addanimation (translateanimation); the imageview.startanimation (translateanimation); - } the } the}
Android_mars Study notes _s02_007_animation the first way to use: code