Animation Effect Programming Basics--animationandroid
Type of animation
Android animation is made up of four different types
in XML
Alpha
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
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
How to define animations in an XML file
① open Eclipse, new Android Project
② creating a new Anim folder in the Res directory
③ Create a new myanim.xml in the Anim directory (note the file name lowercase)
④ adding animated code to XML
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <set xmlns:android= "Http://schemas.android.com/apk/res /android "> 3 <alpha/> 4 <scale/> 5 <translate/> 6 < rotate/> 7 </set>
Android Animation parsing--xml
<alpha>
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <set xmlns:android= "Http://schemas.android.com/apk/res /android "> 3 <alpha 4 android:fromalpha=" 0.1 " 5 android:toalpha=" 1.0 " 6 android:duration= 7 8 /> 9 <!--transparency Control animation effect Alpha floating-point value: The Fromalpha property is the transparency at the beginning of the animation Toalpha property is the transparency at the end of the animation : 0.0 for full transparency , 1.0 for full opacity. number of float data type between 0.0-1.0 and above value : 19 Duration property for animation duration Description: time in milliseconds--- </set>
<scale>
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <set xmlns:android= "Http://schemas.android.com/apk/res/android" & Gt 3 <scale 4 android:interpolator= 5 "@android: anim/accelerate_decelerate_i Nterpolator "6 android:fromxscale=" 0.0 "7 android:toxscale=" 1.4 "8 ANDROID:FR omyscale= "0.0" 9 android:toyscale= "1.4" android:pivotx= "50%" Android:pivo Ty= "50%" android:fillafter= "false" android:startoffset= "Android:du" Ration= "/>" </set> <!--size Scaling Animation Scale 17 Property: interpolator Specifies an animated insert 18 During my trial, using resources in Android.res.anim, I found that 19 had three animation inserts: Accelerate_decelerate_interpolator acceleration-minus Speed Animation Insert Accelerate_interpolator acceleration-Animation insert decelerate_interpolator slow-motion Draw the insertion device 23 Other animations that belong to a specific 24 float value: Fromxscale property is the scaling size at the x-coordinate of the start of the animation 27 The Toxscale property is the stretch size on the x-coordinate at the end of the animation, and the Fromyscale property is the scaling dimension on the y-coordinate at the start of the animation. The Yscale property is the scaling dimension on the y-coordinate at the end of the animation. The Startoffset property is the next animation starting from the time the last animation was stopped 32 33 description: 34 or more of four attribute values 35 36 0.0 means that shrinking to No 37 1.0 means normal no scaling A value of 38 less than 1.0 means that a contraction of 39 values greater than 1.0 means a magnification of pivotx The Pivoty property is the start position of the animation relative to the object's x-coordinate. The start position of the animation relative to the object's y-coordinate 43 44 Description: 45 The above two attribute values from 0%-100% 46 50% is the midpoint position on the X or y-direction coordinates of the object 47 48 Long Integer value: The Duration property is animated duration 50 Description: Time in milliseconds 51 52 Boolean value: Filla Fter property When set to True, the animation is converted to theAfter the animation is applied---
<translate>
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <set xmlns:android= "Http://schemas.android.com/apk/res /android "> 3 <translate 4 android:fromxdelta=" 5 android:toxdelta= " -80" 6 android:fromydelta= " 7 android:toydelta=" 8 android:duration= " 9" /> <!--translate position transfer animation effect one -to-one integer value: The Fromxdelta property is the position of the x-coordinate at the start of the animation the Toxdelta property is the position of the x-coordinate at the end of the animation, and the Fromydelta property is the position of the y-coordinate at the start of the animation Toydelta property is the position of the y-coordinate at the end of the animation. Note: fromxtype toxtype fromytype toytype The default is to use your own relative reference to the value of the Long integer: The Duration property is the duration of the animation : time in milliseconds 22-- > </set>
<rotate>
1 <?xml version= "1.0" encoding= "Utf-8"?> 2 <set xmlns:android= "Http://schemas.android.com/apk/res/android" & Gt 3 <rotate 4 android:interpolator= "@android: Anim/accelerate_decelerate_interpolator" 5 Android : fromdegrees= "0" 6 android:todegrees= "+350" 7 android:pivotx= "50%" 8 Androi D:pivoty= "50%" 9 android:duration=/> <!--rotate rotate animation effect 11 properties: Interpolato R specifies an animated insert 12 in the course of my experiment, using resources in Android.res.anim, I found that 13 has three kinds of animation inserts: Ccelerate_decelerate_interpolator acceleration-deceleration animation Insert accelerate_interpolator Acceleration-Animation Insert 1 6 Decelerate_interpolator Deceleration-Animation insert 17 Other animation effects that belong to a particular 18 19 floating-point value: The Fromdegrees property is the angle of the object at the start of the animation Todegrees property is the object at the end of the animation The angle of rotation can be greater than 360 degrees 22 23 Description: 24 When the angle is negative--indicates counterclockwise rotation 25 when the angle is positive-- Clockwise rotation 26 (negative from--to positive: clockwise rotation) 27 (negative from--to negative: counterclockwise rotation) 2 8 (positive from--to positive: clockwise rotation) 29 (positive from--to Negative: counterclockwise) 30 31 The Pivotx property is the start position of the animation relative to the object's x-coordinate, the Pivoty property is the start position of the animation relative to the object's y-coordinate 33 34 Note: The above two attribute values from 0%-100% value 35 50% is the midpoint position on the X or y-direction coordinates of the object 36 37 Long integer value: 38 Duration property for animation duration 39 Description: Time in milliseconds---</set>
How to use the animation effect in XML
1 public static Animation loadanimation (context context, int id) 2 ///The first parameter context for the program contexts 3 // The second parameter ID is a reference to an animated XML file 4 //Example: 5 myanimation= animationutils.loadanimation (this,r.anim.my_action); 6 //Use the static method of the Animationutils class Loadanimation () to load an animated XML file in the XML
How to define animations in Java code
1//Define animation instance object in code 2 private Animation myanimation_alpha; 3 private Animation Myanimation_scale; 4 private Animation myanimation_translate; 5 private Animation myanimation_rotate; 6 7 //Initialize an instance object according to the respective construction method 8 myanimation_alpha=new alphaanimation (0.1f, 1.0f); 9 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, 16
Android Animation parsing--javacode
Alphaanimation
①alphaanimation Class object definitions
1 private alphaanimation Myanimation_alpha;
②alphaanimation Class object constructs
1 alphaanimation (float fromalpha, float toalpha) 2 //first parameter Fromalpha for animation start time Transparency 3 //second parameter toalpha to Animation at the end of the Transparency 4 myanimation_alpha=new alphaanimation (0.1f, 1.0f); 5 //Description: 6 // 0.0 = Fully transparent 7 // 1.0 for full opacity
③ Setting Animation duration
1 myanimation_alpha.setduration (); 2
Scaleanimation
①scaleanimation Class object definitions
1 private alphaanimation Myanimation_alpha;
②scaleanimation Class object constructs
1 scaleanimation (float FromX, float toX, float fromY, float ToY, 2 int pivotxtype, float pivotxvalue, int pivotyty PE, float pivotyvalue) 3 //The first parameter fromx the scaling dimension on the x-coordinate at the start of the animation 4 ///The second parameter tox the scaling size at the end of the animation at x-coordinate 5 // The third parameter, fromy, is the scaling dimension on the y-coordinate at the start of the animation 6 //fourth parameter toy the scaling size on the y-coordinate at the end of the animation 7 8 // The fifth parameter Pivotxtype for the animation at the x-axis relative to the object position type 9 //Sixth parameter pivotxvalue the start position of the animation relative to the object's x-coordinate The seventh parameter Pivotxtype for the animation on the y-axis relative to the object position type One//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
1 myanimation_scale.setduration (a); 2 //Set time duration is 700 milliseconds
Translateanimation
①translateanimation Class object definitions
1 private alphaanimation Myanimation_alpha;
②translateanimation Class object constructs
1 translateanimation (float fromxdelta, float toxdelta, 2 float fromydelta, float toydelta) 3 // The first parameter fromxdelta is the move position at the start of the animation at the x-coordinate 4 //The second parameter toxdelta the position of the x-coordinate at the end of the animation 5 // The third parameter fromydelta the move position on the y-coordinate at the start of the animation 6 //Fourth parameter toydelta the move position on the y-coordinate at the end of the animation
③ Setting Animation duration
1 myanimation_translate.setduration (+); 2 //Set time duration is 2000 milliseconds
Rotateanimation
①rotateanimation Class object definitions
1 private alphaanimation Myanimation_alpha;
②rotateanimation Class object constructs
1 rotateanimation (float fromdegrees, float todegrees, 2 int pivotxtype, float pivotxvalue, int pivotytype, float Pivotyvalue) 3 ////The first parameter fromdegrees the angle of rotation at the start of the animation 4 ////The second parameter todegrees for animation rotation to 5 // The third parameter Pivotxtype for the animation on the x-axis relative to the object position type 6 //Fourth parameter pivotxvalue the start position of the x-coordinate of the animation relative to the object 7 // The fifth parameter Pivotxtype for the animation on the y-axis relative to the object position type 8 //Sixth parameter pivotyvalue the start position of the animation relative to the object's y-coordinate 9 myanimation_rotate=new Rotateanimation (0.0f, +350.0f, animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);
③ Setting Animation duration
1 myanimation_rotate.setduration (+); 2 //Set time duration is 3000 milliseconds
How to use the animation effect 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.
Order of animation effects
1 android:animationorder= "random" //Random 2 3 4 android:animationorder= "reverse"//Reverse 5 6 7 8 9 gridLayoutAnimation10 android:directionpriority= "Row" android:directionpriority= "column" 15 16 android:direction= "Right_to_left|bottom_to_top"