There are two categories of Animation (animation): Tween animation (Tween) and frame animation (frame)
Motion tween mainly has the following kinds:
Rotation (rotateanimation)
Translation (Translateanimation)
Stretching (scaleanimation)
Transparency (alphaanimation)
Way to implement:
1. The corresponding animation object of the instance
2. Load the animated file in the resource
Properties of animations
Duration: Duration of animation
Filiafter: Keeps the state at the end of true, and returns the original state for false
RepeatCount: Number of repetitions (excluding first time)
Startoffset: Time to start the animation
Repeatmode:1 to start again, 2 to play back backwards from the last state
Frame animation:
Implementation methods
1. Example Animationdrawable
2. In drawable, create a new XML file of type Animation-list, and then load the file
The code is as follows
Package com.example.animaction;
Import Android.os.Bundle;
Import android.view.animation.AlphaAnimation;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationSet;
Import Android.view.animation.AnimationUtils;
Import Android.view.animation.CycleInterpolator;
Import android.view.animation.RotateAnimation;
Import android.view.animation.ScaleAnimation;
Import android.view.animation.TranslateAnimation;
Import Android.widget.TextView;
Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import android.graphics.drawable.AnimationDrawable;
Import android.graphics.drawable.Drawable; @SuppressLint ("Newapi") public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinst
Ancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
TextView TV = (TextView) Findviewbyid (r.id.tv); /** * Transparency Animation//Method One://parameter 1 is initial transparency, parameter 2 is end transparency//alphaanimation alphaanimation = new AlphaanimatiOn (1,//0.5f);//1 can be automatically converted to float,0.5 the default is double value alphaanimation alphaanimation = new Alphaanimation (1, 0);
Set execution time, default to 0 alphaanimation.setduration (3000);
Repetition times Alphaanimation.setrepeatcount (3);
Repeat mode, Animation.restart: Start from scratch, animation.reverse: Reverse Alphaanimation.setrepeatmode (animation.reverse);
Sets the state at the end, true to the end state, and false back to the original state alphaanimation.setfillafter (true);
Method Two://load the animated file in the resource Animation AlphaAnimation1 = Animationutils.loadanimation (this, r.anim.alpha_anim);
Tv.startanimation (AlphaAnimation1); /** * Translation Animation/* * @parm * fromxtype--> type of starting x coordinates, three types: animation.absolute,relative_to_parent, * relative _to_self * * @parm fromxvalue--> If the type is absolute, the value is absolute, unit px * relative_to_parent, relative to the parent control's position, the value is float ( -1~1) Relati
Ve_to_self: Relative to the position of its own control * * @parm the position of the x-coordinate at the end of the Toxtype--> * * @parm toxvalue--> at the end of the x-coordinate value the other four parameters are the same as above * * The other constructor defaults to the absolute type//represents the position of the parent control 0.5 from the position relative to its control 0.5 translateanimation traNslateanimation = new Translateanimation (animation.relative_to_self, 0.5f, Animation.relative_to_parent, 0.5f, 0,
0, 0, 0);
Translateanimation.setduration (3000);
Tv.startanimation (translateanimation); /** * Zoom Animation///* x direction: FromX,tox--> Scaling from 0.5 (will directly become half of the original, no animation effect) into 1.5 y direction: FromY, ToY * The following four parameters determine the center point of the zoom. 4 parameter types are absolute with respect to their own control 6 parameter types//If the type is relative to the parent control, it is equivalent to pulling the control up to the center of the parent control scaleanimation scaleanimation = new Scaleanimation (0,
1.0f, 0, 1.0f, animation.relative_to_parent, 0.5f, Animation.relative_to_parent, 0.5f);
Scaleanimation.setduration (3000);
Tv.startanimation (scaleanimation); /** * Rotary Animation/* * fromdegrees--> starting angle,todegrees--> End angle Four parameters to determine the rotation of the center point * * * rotateanimation Rotateanim
ation = new Rotateanimation (0, 180, animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
Rotateanimation.setduration (5000); Loop playback, the number of parameter loops, will be in the setduration time cycle, cycle for 0~180,0~-180 counted once cycleinterpolator cycleinterpolator = New Cycleinterpolator (20);
Rotateanimation.setinterpolator (Cycleinterpolator);
Tv.startanimation (rotateanimation);
/** * Animation Set *//Method One: Animation set = Animationutils.loadanimation (this, r.anim.set);
Start is executed immediately, and the set may not execute tv.startanimation (set) immediately;
If the argument is true, the animation performs the same transmission as the set set by the transmission, false to perform the respective transmission animationset Set2 = new Animationset (false);
Set2.addanimation (rotateanimation);
Scaleanimation.setstartoffset (10000);
Set2.addanimation (scaleanimation);
/** * Frame Animation *///First//animationdrawable indirect inheritance drawable animationdrawable ad = new animationdrawable ();
drawable frame1 = Getresources (). getdrawable (R.drawable.ic_launcher);
Ad.addframe (frame1, 1000/* display time * *);
The animation executes only once Ad.setoneshot (true);
Tv.setbackground (AD);
Ad.start ()//Start the animation ad.stop (), and then start from the beginning of the second tv.setbackgroundresource (r.drawable.list);
Animationdrawable ad1 = (animationdrawable) tv.getbackground ();
Ad1.start ();
@Override public void Finish () { Super.finish (); When startactivity or finish, set the action effect of the page switch overridependingtransition (/* Enter the page animation */r.anim.alpha_anim, * leave the page animation/R.
Anim.set);
}
}
Xmlz
<?xml version= "1.0" encoding= "Utf-8"?> <alpha xmlns:android=
"http://schemas.android.com/apk/res/" Android "
android:fromalpha=" 1 "
android:toalpha=" 0.5 "
android:duration=" 3000 "
android: Repeatmode= "Restart"
android:repeatcount= "3" >
</alpha>
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "Http://schemas.android.com/apk/res/android" > <alpha android:duration= "3000" android:fromalpha= "0" android:toalpha= "1"/> & lt;! --can only set relative own scaling--> <scale android:duration= "3000" android:fromxscale= "0" ANDROID:FROMYSC ale= "0" android:pivotx= "50%" android:pivoty= "50%" android:toxscale= "1" android:toyscale= "1
"/> <rotate android:duration=" 3000 "android:pivotx=" 50% "android:pivoty=" "50%"
android:fromdegrees= "0" android:todegrees= "360"/> <translate android:duration= "3000" Android:fromxdelta= "0" android:toxdelta= "100%"/> <!--android:startoffset= "3000": Three seconds before execution
; <alpha android:duration= "3000" android:fromalpha= "1" android:startoffset= "3000" Android : toalpha= "0"/> </set>