1.Animation Animation type
Android animation consists of four types:
in XML
| Alph |
Gradient Transparency Animation effect |
| Scale |
Gradient Dimension Stretch animation effect |
| Translate |
Move animation effect on picture transition position |
| Rotate |
Picture Transfer rotation animation effect |
In Javacode
| Alphaanimation |
Gradient Transparency Animation effect |
| Scaleanimation |
Gradient Dimension Stretch animation effect |
| Translateanimation |
Move animation effect on picture transition position |
| Rotateanimation |
Picture Transfer rotation animation effect |
2.Android Animation mode
Animation There are two main modes of animation:
One is tweened animation (gradient animation)
| in XML |
Javacode |
| Alpha |
Alphaanimation |
| Scale |
Scaleanimation |
One is frame by frame (picture conversion animation)
| in XML |
Javacode |
| Translate |
Translateanimation |
| Rotate |
Rotateanimation |
3. How to define animations in an XML file
The steps are as follows:
① New Android Project
② creating a new Anim folder in the Res directory
③ Create a new my_anim.xml in the Anim directory (note the file name lowercase)
④ Add animation code to My_anim.xml
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <alpha/> <scale/> <translate/> <rotate/></set>
4.Android Animation parsing --xml
4.1 Alpha Gradient Transparency animation effect
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <alpha android:duration= "" android:fromalpha= "0.0" android:toalpha= "1.0"/> <!-- Transparency controls the animation effect Alpha float value: The Fromalpha property is the Transparency Toalpha property at the beginning of the animation when the transparency description is at The end of the animation : 0.0 indicates full transparency 1.0 represents a full opacity above value takes a numeric long integer value of float data type between 0.0-1.0 : duration property for animation duration Description: the time is in milliseconds --></set>
4.2 Scale gradient resizing animation effect
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <scale android:duration= "" android:fillafter= "false" android:fromxscale= "0.0" a ndroid:fromyscale= "0.0" android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" Android:pivo tx= "50%" android:pivoty= "50%" android:toxscale= "1.4" android:toyscale= "1.4"/></set><!-- Size Stretch animation effect Scale property: interpolator Specifies an animated insert during my trial, there are three types of animation inserts found when using resources in Android.res.anim: Accelerate_decelerate_interpolator acceleration-deceleration animation Insert accelerate_interpolator acceleration-Animation Insert Decelerate_i Nterpolator Deceleration-Animation insert other specific animation effects floating-point values: The Fromxscale property is the scaling dimension on the x-coordinate at the start of the animation The Toxscale property is the scaling dimension on the x-coordinate at the end of the animation Fromyscale property is the scaling dimension on the y-coordinate at the start of the animation Toyscale property is the Y-seat at the end of the animation Description of scaling dimensions on the label: The above four attribute values of 0.0 mean that shrinking to no 1.0 means that the normal no scaling value is less than 1. 0 means that a shrink value greater than 1.0 means that the magnification Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is animated relative to the object's Y Start position of coordinates description: The above two attribute values from 0%-100% value 50% is the midpoint position on the x or Y coordinate of the object. Long Integer value: Duration property is the animation duration description: The time is in milliseconds Boolean value: Fillafter property When set to True, the animation is converted to the animated knot After bundle is applied--
4.3 Translate picture conversion position move animation effect
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <translate android:duration= "Android:fromxdelta=" " android:fromydelta=" 30 " android:toxdelta= " -80" android:toydelta= "/>" <!-- Translate position transfer animation effect integer value: The Fromxdelta property is the position of the x-coordinate at the start of the animation Toxdelta property is the position on the x-coordinate at the end of the animation Fromydelta property is the position on the y-coordinate when the animation starts Toydelta property is the position of the y-coordinate at the end of the animation Note: fromxtype toxtype fromytype toytype is not specified, the default is to use their own relative reference Long integer value: The Duration property is an animation duration Description: The time is in milliseconds --></set>
4.4 Rotate Picture Transfer rotation animation effect
<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" > <rotate android:duration= "android:fromdegrees=" 0 "android:interpolator=" @android: ANI M/accelerate_decelerate_interpolator "android:pivotx=" 50% "android:pivoty=" 50% "android:todegrees=" +3 "/> <!--rotate rotation animation effect properties: interpolator Specifies an animated insert during my experiment, using resources in Android.res.anim to discover There are three types of animation inserts: Accelerate_decelerate_interpolator acceleration-deceleration animation Insert Accelerate_interpolator Acceleration-Animation Insert Decelerate_interpolator deceleration-Animation insert other animation effects that belong to a specific Floating-point value: The Fromdegrees property is the angle of the object at the start of the animation Todegrees property is at the end of the animation when the object rotates at an angle greater than 360 degrees Description: When the angle is negative--indicates counterclockwise rotation when the angle is positive--Indicates clockwise rotation (Negative from--to positive: clockwise rotation) (Negative from--to negative: counterclockwise rotation) (Positive from--to positive: clockwise rotation) (Positive from--to negative number: counterclockwise rotation) The Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is the beginning of the animation relative to the object's y-coordinate Note: The above two attribute values from the 0%-100% value 50% is the object's X or y-direction coordinates on the midpoint position Long value: Durat The Ion property is the animation duration description: The time is in milliseconds--></set>
5. How to use the animation effect in XML
public static Animation Loadanimation (context context, int id)//The first parameter context for the program contexts //second parameter ID for a reference to an animated XML file// Example: myanimation= animationutils.loadanimation (This,r.anim.my_anim);//static method using Animationutils class Loadanimation () To load the animated XML file in the XML
6. How to use the animation effect in XML
Defining an animation instance object in code private Animation myanimation_alpha;private Animation myanimation_scale;private Animation myanimation_ Translate;private Animation myanimation_rotate; Initializes an instance object Myanimation_alpha=new alphaanimation (0.1f, 1.0f) according to their respective construction methods; Myanimation_scale =new scaleanimation (0.0f, 1.4f, 0.0f, 1.4f, animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f); myanimation_translate=new Translateanimation (30.0f, -80.0f, 30.0f, 300.0f), Myanimation_rotate=new rotateanimation (0.0f, +350.0f, Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);
7.Android Animation parsing--javacode
7.1 alphaanimation
①alphaanimation Class object definitions
Private Alphaanimation Myanimation_alpha
②alphaanimation Class object constructs
The first parameter fromalpha the animation at the beginning of the transparency//second parameter Toalpha for the end of the animation transparency Alphaanimation (float fromalpha, float toalpha)//Description: 0.0 is fully transparent, 1.0 means completely opaque myanimation_alpha=new alphaanimation (0.1f, 1.0f);
③ Setting Animation duration
Set time duration is 5000 milliseconds myanimation_alpha.setduration (5000);
7.2 scaleanimation
①scaleanimation Class object definitions
Private Alphaanimation Myanimation_alpha;
②scaleanimation Class object constructs
Scaleanimation (float FromX, float toX, float fromY, float toY, int pivotxtype, float pivotxvalue, int pivotytype, Floa T pivotyvalue)////The first parameter fromx the scaling dimension on the x-coordinate at the start of the animation //The second parameter tox the scaling dimension on the x-coordinate at the end of the animation //The third parameter fromy the scaling dimension on the y-coordinate at the start of the animation // The fourth parameter, toy, is the scaling dimension on the y-coordinate at the end of the animation /* Description: The above four attribute values 0.0 means that shrinking to no 1.0 means that a normal no scaling value of less than 1.0 means that the shrinkage value is greater than 1.0 for the magnification */// The fifth parameter Pivotxtype for animation at the x-axis relative to the object position type //sixth parameter pivotxvalue the start position of the animation relative to the object's x-coordinate//seventh parameter Pivotxtype for the animation on the y-axis relative to the object position type / /Eighth parameter pivotyvalue the start position of the animation relative to the object's y-coordinate myanimation_scale =new scaleanimation (0.0f, 1.4f, 0.0f, 1.4f, Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
③ Setting Animation duration
Set time duration is 700 milliseconds myanimation_scale.setduration (700);
7.3 Translateanimation
①translateanimation Class object definitions
Private Alphaanimation Myanimation_alpha;
②translateanimation Class object constructs
The first parameter fromxdelta the move position on the x-coordinate at the start of the animation //The second parameter toxdelta the position of the movement at the end of the animation at the x-coordinate ///The third parameter Fromydelta the movement position on the y-coordinate at the start of the animation
③ Setting Animation duration
Set time duration is 2000 milliseconds myanimation_translate.setduration (2000);
7.4 rotateanimation
①rotateanimation Class object definitions
Private Alphaanimation Myanimation_alpha;
②rotateanimation Class object constructs
Rotateanimation (float fromdegrees, float todegrees,int pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue) ///The first parameter fromdegrees the rotation angle at the start of the animation //The second parameter todegrees the angle to which the animation is rotated ///The third parameter Pivotxtype for the animation on the x-axis relative to the object position type // The fourth parameter pivotxvalue the start position of the animation relative to the object's X coordinate//fifth parameter Pivotxtype for the animation on the y-axis relative to the object position type // The sixth parameter pivotyvalue the start position of the animation relative to the object's y-coordinate myanimation_rotate=new rotateanimation (0.0f, +350.0f,animation.relative_to_self , 0.5f,animation.relative_to_self, 0.5f);
②rotateanimation Class object constructs
Set time duration is 3000 milliseconds myanimation_rotate.setduration (3000);
8. How to use animation effects in Java code
Use the method inherited from the View parent class Startanimation () to add an animation effect for view or subclass view, and so on.
public void Startanimation (Animation Animation)
9. Let's have a chestnut.
9.1 Using XML file mode
① as follows:
② defines animations in an XML file, as mentioned earlier
③ main interface layout, this is nothing to say, very simple O (∩_∩) o
④ main interface Logic code, mainly this, control animation display
Package Com.yanis.base;import Android.app.activity;import Android.os.bundle;import android.view.view;import Android.view.view.onclicklistener;import Android.view.animation.animation;import Android.view.animation.animationutils;import Android.widget.button;import Android.widget.imageview;public Class Animationactivity extends Activity implements Onclicklistener {private ImageView imgpic; Private Button Btnalpha, Btnscale, Btntranslate, btnrotate; Private Animation myanimation; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_animation); Intiview (); InitData (); }/** * Initialize components */private void Intiview () {imgpic = (ImageView) Findviewbyid (r.id.imgpic); Btnalpha = (Button) Findviewbyid (R.id.btnalpha); Btnscale = (Button) Findviewbyid (R.id.btnscale); Btntranslate = (Button) Findviewbyid (r.id.btntranslate); Btnrotate = (Button) Findviewbyid (r.id.btnrotate); }/** * Initialize data */private void InitData () {Btnalpha.setonclicklistener (this); Btnscale.setonclicklistener (this); Btntranslate.setonclicklistener (this); Btnrotate.setonclicklistener (this); } @Override public void OnClick (View v) {switch (V.getid ()) {r.id.btnalpha:/** * Use the animation effect in XML the first argument to context for the program contexts the second parameter ID is a reference to an animated XML file */myanimation = Animationutils.loadan Imation (this, r.anim.alpha_anim); Imgpic.startanimation (myanimation); Break Case r.id.btnscale:myanimation = Animationutils.loadanimation (this, r.anim.scale_anim); Imgpic.startanimation (myanimation); Break Case r.id.btntranslate:myanimation = Animationutils.loadanimation (This, r.anim.translate_an IM); Imgpic.startanimation (myanimation); Break CASE r.id.btnrotate:myanimation = animationutils. Loadanimation (this, r.anim.rotate_anim); Imgpic.startanimation (myanimation); Break } }}
9.2 How to use Java code
There are examples in the Fundamentals of blog Game Development (animation), which are not mentioned here.
10. Use Animation-list to animate frames by frame
Chestnuts are as follows:
The steps are as follows:
① add picture footage to the res/drawable directory
② adding an animated animation-list frame layout file to the Drawable folder
<?xml version= "1.0" encoding= "Utf-8"?><!--root tag is animation-list, where oneshot represents whether to show only once, set to false will not stop looping animation root tag under , each picture in the animation is declared by the item tag android:duration indicates the length of time that the picture was used--><animation-list xmlns:android= "/http Schemas.android.com/apk/res/android "android:oneshot=" false "> <item android:drawable=" @drawable/cmmusi C_progress_1 "android:duration=" > </item> <item android:drawable= "@drawable/cmmusic_pr Ogress_2 "android:duration=" > </item> <item android:drawable= "@drawable/cmmusic_progre Ss_3 "android:duration=" > </item> <item android:drawable= "@drawable/cmmusic_progress_4 "Android:duration=" > </item> <item android:drawable= "@drawable/cmmusic_progress_5" android:duration= "> </item> <item android:drawable=" @drawable/cmmusic_progress_6 " android:duration= "> </"item> <item android:drawable= "@drawable/cmmusic_progress_7" android:duration= "> </item" > <item android:drawable= "@drawable/cmmusic_progress_8" android:duration= "> </item> </animation-list>
③ main interface page layout settings, too simple, not to repeat the
The ④ main interface code is as follows:
Package Com.yanis.base;import Android.app.activity;import Android.graphics.drawable.animationdrawable;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.Button; Import Android.widget.imageview;public class Animationactivity extends Activity implements Onclicklistener {private Im Ageview Imgpic; Private Button btnstart, btnstop; Private Animationdrawable animationdrawable; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_animation); Intiview (); InitData (); }/** * Initialize components */private void Intiview () {imgpic = (ImageView) Findviewbyid (r.id.imgpic); btnstart = (Button) Findviewbyid (R.id.btnstart); Btnstop = (Button) Findviewbyid (r.id.btnstop); }/** * Initialize data */private void InitData () {Btnstart.setonclicklistener (this); Btnstop.setonclicklIstener (this); Sets a drawable as the content of this ImageView. Imgpic.setimageresource (R.drawable.loading_anim); Assign a value to an animation resource animationdrawable = (animationdrawable) imgpic.getdrawable (); } @Override public void OnClick (View v) {switch (V.getid ()) {R.id.btnstart:animatio Ndrawable.start ();//Start break; Case R.id.btnstop:animationdrawable.stop (); Stop break; } }}
This address: http://www.cnblogs.com/yc-755909659/p/4290114.html
Android Animation (i): Motion tweens