[Android] Animation introduction and implementation, androidanimation
Animation effects to display and hide menus. This article will introduce them.
1. Animation type
Android animation consists of four types:
In XML
| Alph |
Gradient transparency animation effect |
| Scale |
Gradient scaling animation effect |
| Translate |
Animation Effect |
| Rotate |
Animation effects of screen transfer and Rotation |
Java code
| AlphaAnimation |
Gradient transparency animation effect |
| ScaleAnimation |
Gradient scaling animation effect |
| TranslateAnimation |
Animation Effect |
| RotateAnimation |
Animation effects of screen transfer and Rotation |
2. Android Animation Mode
Animation mainly has two Animation modes:
One is tweened animation (gradient animation)
| In XML |
JavaCode |
| Alpha |
AlphaAnimation |
| Scale |
ScaleAnimation |
One is frame by frame (screen conversion animation)
| In XML |
JavaCode |
| Translate |
TranslateAnimation |
| Rotate |
RotateAnimation |
3.HowDefine an animation in an XML file
The procedure is as follows:
① Create an Android Project
② Create an anim folder in the res directory
③ Create a new my_anim.xml file in the anim directory (note that the file name is in lower case)
④ Add the 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 = "1000" android: fromAlpha = "0.0" android: toAlpha = "1.0"/> <! -- Opacity control animation effect alpha floating point value: the fromAlpha attribute specifies the transparency when the animation starts. The toAlpha attribute specifies the transparency when the animation ends: 0.0 indicates completely transparent. 1.0 indicates completely opaque. The value above is a long integer of the float data type between 0.0 and 1.0. The duration attribute is the animation duration description: time in milliseconds --> </set>
4.2 scale scaling animation effect
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <scale android: duration = "1000" android: fillAfter = "false" android: fromXScale = "0.0" android: fromYScale = "0.0" android: interpolator = "@ android: anim/accelerate_decelerate_interpolator" android: effectx = "50%" android: effecty = "50%" android: toXScale = "1.4" android: toYScale = "1.4"/> </set> <! -- Size scaling animation effect scale attribute: interpolator specifies an animation plug-in. During my experiment, android is used. res. three types of animation inserts are found in anim resources: accelerate_decelerate_interpolator acceleration-Deceleration animation inserter accelerate_interpolator acceleration-animation inserter decelerate_interpolator deceleration-animation inserter other floating point values of specific animation effects: the fromXScale attribute is the scale size toXScale attribute on the X coordinate at the animation start. The scale size on the X coordinate at the animation end. The fromYScale attribute is the scale size toYScale attribute on the Y coordinate at the animation start. description of scaling dimensions on Y coordinate at end: the preceding four property values 0.0 indicate that the image is scaled down to no 1.0, indicating that the normal scaling-free value is less than 1.0, indicating that the shrinking value is greater than 1.0, indicating that the zoomed-in tx attribute is the animation's starting position relative to the X coordinate of the object. description of the start position of Y coordinate relative to the object: the preceding two attribute values are from 0%-100%. The value 50% indicates the long integer value of the midpoint position on the X or Y coordinate of the object. The duration Attribute indicates the animation duration: boolean value in milliseconds: When the fillAfter attribute is set to true, the animation will be applied after the animation ends -->
4.3 animated effect of moving the position of the translate Image
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <translate android: duration = "2000" android: fromXDelta = "30" android: fromYDelta = "30" android: toXDelta = "-80" android: toYDelta = "300"/> <! -- Integer value of the translate position transfer animation effect: the fromXDelta attribute is the position on the X coordinate when the animation starts. The toXDelta attribute is the position on the X coordinate when the animation ends. The fromYDelta attribute is the position on the Y coordinate when the animation starts. The toYDelta attribute is Y at the animation end. note: if fromXType toXType fromYType toYType is not specified, the animation duration is defined by default as long integer value of the reference object: duration Attribute. Description: Time in milliseconds --> </set>
4.4 rotate screen transfer rotation animation effect
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <rotate android: duration = "3000" android: fromDegrees = "0" android: interpolator = "@ android: anim/accelerate_decelerate_interpolator" android: required Tx = "50%" android: Required ty = "50%" android: toDegrees = "+ 350"/> <! -- Rotate rotation animation effect attribute: interpolator specifies an animation plug-in. During my experiment, android is used. res. there are three types of animation inserts in anim resources: accelerate_decelerate_interpolator acceleration-accelerator animation plug-in accelerate_interpolator acceleration-animation plug-in decelerate_interpolator deceleration-animation plug-in other floating-point value for specific animation effects: the fromDegrees attribute is the angle of the object when the animation starts. The toDegrees attribute is that the angle of the object rotation when the animation ends can be greater than 360 degrees. Description: when the angle is negative, it indicates clockwise rotation. When the angle is positive, it indicates clockwise rotation (negative from -- to positive: clockwise rotation) (negative from -- to negative: counter-clockwise rotation) (positive from -- to positive: clockwise rotation) (positive from -- to negative: clockwise rotation) the "TX" attribute is the starting position of the animation relative to the X coordinate of the object. The "ty" attribute is the starting position of the animation relative to the Y coordinate of the object: the preceding two attribute values are from 0%-100%. The value 50% indicates the long integer value of the midpoint position on the X or Y coordinate of the object. The duration Attribute indicates the animation duration: time in milliseconds --> </set>
5.How to Use the animation effect in XML
Public static Animation loadAnimation (Context context, int id) // The first parameter Context is the Context of the program // The second parameter id is a reference to the animated XML file // example: myAnimation = AnimationUtils. loadAnimation (this, R. anim. my_anim); // use the static method loadAnimation () of the AnimationUtils class to load the animation XML file in XML.
6.How to Use the animation effect in XML
// Define the Animation Instance Object private Animation myAnimation_Alpha; private Animation myAnimation_Scale; private Animation myAnimation_Translate; private Animation myAnimation_Rotate in the Code; // Initialize an instance object myAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f) according to their respective constructor; myAnimation_Scale = new ScaleAnimation (0.0f, 1.4f, 0.0f, 1.4f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. updated, 0.5f); myAnimation_Translate = new TranslateAnimation (30366f,-80366f, 30366f, 300366f); myAnimation_Rotate = new RotateAnimation (0.0f, + 350366f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
7. Android animation parsing-JavaCode
7.1 AlphaAnimation
① AlphaAnimation class object definition
private AlphaAnimation myAnimation_Alpha
② AlphaAnimation Class Object Construction
// The first parameter fromAlpha indicates the transparency when the animation starts. // The second parameter toAlpha indicates the transparency when the animation ends. AlphaAnimation (float fromAlpha, float toAlpha) // Description: 0.0 indicates that the animation is completely transparent, 1.0 indicates completely opaque myAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f );
③ Set the animation duration
// Set the duration to 5000 Ms myAnimation_Alpha.setDuration (5000 );
7.2 ScaleAnimation
① ScaleAnimation class object definition
private AlphaAnimation myAnimation_Alpha;
② ScaleAnimation Class Object Construction
ScaleAnimation (float fromX, float toX, float fromY, float toY, int limit txtype, float limit txvalue, int limit tytype, float limit tyvalue) // The first parameter fromX is the scaling size on the X coordinate when the animation starts. // The second parameter toX is the scaling size on the X coordinate when the animation ends. // The third parameter fromY is the animation. the scaling size on the Y coordinate at the start/The Fourth toY parameter is the scaling size on the Y coordinate at the end of the animation/* description: the preceding four property values 0.0 indicate that the animation is scaled to a position on the X axis relative to the object, indicating that the normal scaling-free value is less than 1.0, indicating that the contraction value is greater than 1.0, indicating that the normal scaling-free value is less than 1.0. // The sixth parameter "effectxvalue" is the starting position of the animation relative to the X coordinate of the object. // The Seventh parameter "effectxtype" is the animation relative to the object position on the Y axis. // The eighth parameter "effectyvalue" is the animation relative to the object. myAnimation_Scale = new ScaleAnimation (0.0f, 1.4f, 0.0f, 1.4f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
③ Set the animation duration
// Set the duration to 700 ms myAnimation_Scale.setDuration (700 );
7.3 TranslateAnimation
① TranslateAnimation class object definition
private AlphaAnimation myAnimation_Alpha;
② Construct a TranslateAnimation Class Object
// The first parameter fromXDelta is the moving position on the X coordinate when the animation starts. // The second parameter toXDelta is the moving position on the X coordinate when the animation ends. // The third parameter fromYDelta is the animation. moving position on the Y coordinate at the start // The fourth parameter toYDelta is the moving position on the Y coordinate at the end of the animation. TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
③ Set the animation duration
// Set the duration to 2000 ms myAnimation_Translate.setDuration (2000 );
7.4 RotateAnimation
① RotateAnimation class object definition
private AlphaAnimation myAnimation_Alpha;
② RotateAnimation Class Object Construction
RotateAnimation (float fromDegrees, float toDegrees, int limit txtype, float limit txvalue, int limit tytype, float limit tyvalue) // The first parameter fromDegrees is the rotation angle when the animation starts. // The second parameter toDegrees is the animation rotation angle. // The third parameter effectxtype is the animation position type on the X axis relative to the object/ /The Fourth parameter, "effectxvalue", is the start position of the animation relative to the X coordinate of the object. // The Fifth parameter, "effectxtype", is the type of the animation relative to the object position on the Y axis. // The sixth parameter, "effectyvalue", is Y coordinate start position myAnimation_Rotate = new RotateAnimation (0.0f, + 3500000f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
② RotateAnimation Class Object Construction
// Set the duration to 3000 Ms myAnimation_Rotate.setDuration (3000 );
8.How to Use the animation effect in Java code
Use the startAnimation () method inherited from the View parent class to add an animation effect for the View or subclass View.
public void startAnimation (Animation animation)
9. Let's have a chestnut.
9.1 using XML files
① As follows:
② Define an animation in an XML file, as mentioned earlier
③ The layout of the main interface, which is easy to say.
④ The logic code of the main interface is mainly used to control the 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, role; private Animation parent; @ Override protected void onCreate (Bundle parent. onCreate (savedInstanceState); setContentView (R. layout. activity_animation); intiView (); initData ();}/*** initialization component */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);}/*** initialization 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 () {case R. id. btnAlpha:/*** use the first parameter of the animation effect in XML to set Context as the Context of the program. The second parameter id is the reference of the animation XML file */myAnimation = AnimationUtils. loadAnimation (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_anim); imgPic. startAnimation (myAnimation); break; case R. id. btnRotate: myAnimation = AnimationUtils. loadAnimation (this, R. anim. rotate_anim); imgPic. startAnimation (myAnimation); break ;}}}
9.2 using Java code
Examples of blog game development BASICS (animations) are provided here.
10. Use Animation-list to implement frame-by-frame Animation
The chestnuts are as follows:
The procedure is as follows:
① Add image clips to the res/drawable directory
② Add an Animation-list frame layout file to the drawable folder.
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- The root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation root tag is continuously played, android: duration represents the length of time used to display each image in the animation through the item tag --> <animation-list xmlns: android = "http://schemas.android.com/apk/res/android" android: oneshot = "false"> <item android: drawable = "@ drawable/cmmsusic_progress_1" android: duration = "150"> </item> <item android: drawable = "@ drawable/cmcmusic_progress_2" android: duration = "150"> </item> <item android: drawable = "@ drawable/cmusic_progress_3" android: duration = "150"> </item> <item android: drawable = "@ drawable/cmusic_progress_4" android: duration = "150"> </item> <item android: drawable = "@ drawable/cmcmusic_progress_5" android: duration = "150"> </item> <item android: drawable = "@ drawable/cmusic_progress_6" android: duration = "150"> </item> <item android: drawable = "@ drawable/cmusic_progress_7" android: duration = "150"> </item> <item android: drawable = "@ drawable/cmcmusic_progress_8" android: duration = "150"> </item> </animation-list>
③ The page layout settings on the main interface are too simple to go into details.
④ 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 ImageView imgPic; private Button btnStart, btnStop; private AnimationDrawable restart; @ Override protected void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. activity_animation); intiView (); initData ();}/*** initialization component */private void intiView () {imgPic = (ImageView) findViewById (R. id. imgPic); btnStart = (Button) findViewById (R. id. btnStart); btnStop = (Button) findViewById (R. id. btnStop);}/*** initialization 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 the animation resource the value animationDrawable = (AnimationDrawable) imgPic. getDrawable () ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. btnStart: animationDrawable. start (); // start break; case R. id. btnStop: animationDrawable. stop (); // stop break ;}}}Certificate ----- reproduced to: http://www.cnblogs.com/yc-755909659/p/4290114.html