There are two main animation modes in Android, one is tweened animation (gradient animation), including 4 types of animation, alpha (transparency), scale (size scaling), translate (position transform), rotate (graphics rotation), The second is frame by frame (frame animation), which is to play the set animation by frame
Let's look at the implementation of the first animation
The first step is to define the animated XML file and create a new myanim.xml under Res/anim.
<?XML version= "1.0" encoding= "Utf-8"?><Setxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--Transparency Transform | Start Transparency to 10%| Target transparency is 100% | Time is 2 seconds - <AlphaAndroid:fromalpha= "0.1"Android:toalpha= "1.0"android:duration= "$" /> <!--Zoom Animation | Start x and y both for 0| when the animation ends, zoom to 140%|pivotx and Pivoty set the position of the animation relative to itself |fillafter indicates whether the playback is applied after it finishes - < ScaleAndroid:interpolator= "@android: Anim/accelerate_decelerate_interpolator"Android:fromxscale= "0.0"Android:toxscale= "1.4"Android:fromyscale= "0.0"Android:toyscale= "1.4"Android:pivotx= "50%"Android:pivoty= "50%"Android:fillafter= "false"android:duration= " the" /> <!--Position Transform | Start x and y are 30 | end X is 0,y to 50, which means move to the left | time is 3 seconds - <TranslateAndroid:fromxdelta= "+"Android:toxdelta= "0"Android:fromydelta= "+"Android:toydelta= " the"android:duration= " the" /> <!--Rotate Animation | Starting at 0 degrees, ending angle is 350, animation is 50% relative to its position, time is 3 seconds - <RotateAndroid:interpolator= "@android: Anim/accelerate_decelerate_interpolator"android:fromdegrees= "0"android:todegrees= "+350"Android:pivotx= "50%"Android:pivoty= "50%"android:duration= " the" /></Set>
Then add a new picture and button to the Activity_main.xml to start the animation
<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/relativelayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.ssln.animation.MainActivity" > <ImageViewAndroid:id= "@+id/myimageview"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:layout_centervertical= "true"Android:layout_centerhorizontal= "true"android:layout_gravity= "Center_horizontal"android:src= "@drawable/img" /> <ButtonAndroid:id= "@+id/butstart"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_alignleft= "@+id/myimageview"Android:layout_alignparentbottom= "true"Android:text= "Start animation" /></Relativelayout>
Finally, add the code in the Mainactivity.java
Packagecom.ssln.animation; ImportAndroid.os.Bundle;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;Importandroid.view.animation.Animation;Importandroid.view.animation.AnimationUtils;ImportAndroid.widget.Button;ImportAndroid.widget.ImageView; Public classMainactivityextendsactionbaractivity {Animation myanim; //AnimationImageView myimg;//ImageButton mybtn;//Start button@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Loading AnimationsMyanim=animationutils.loadanimation ( This, R.anim.myanim); //Find a picturemyimg=(ImageView) Findviewbyid (R.id.myimageview); //Check the button.mybtn=(Button) Findviewbyid (R.id.butstart); Mybtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {//Start Animationmyimg.startanimation (Myanim); } }); }}
The display works as follows, the transparency in the rotation increases, the picture is enlarged and the position is shifted
Android Custom Animations