I. Type of animation
Android animation consists of four types: alpha, scale, translate, rotate
In the XML configuration file
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 Java Code code
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 |
Second, Android animation mode
Animation There are two main modes of animation: tweened and Frame
- 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 |
Iii. defining animations in XML files
① 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
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <alpha/>
- <scale/>
- <translate/>
- <rotate/>
- </set>
Iv. Android XML Animation parsing
1. Alpha
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <alpha
- Android:fromalpha= "0.1"
- Android:toalpha= "1.0"
- Android:duration= "3000"
- />
- <!--transparency Controls animation effect Alpha
- Floating-point values:
- The Fromalpha property is the transparency at the beginning of the animation
- Toalpha property is the transparency at the end of the animation
- Description
- 0.0 indicates full transparency
- 1.0 means completely opaque
- The above values take a number of float data types between 0.0-1.0
- Long Integer value:
- The Duration property is the duration of the animation
- Description
- The time is measured in milliseconds
- -
- </set>
2. Scale
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <scale
- Android: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= "/>"
- </set>
- <!--size Scaling animation effect scale
- Property: interpolator Specifies the insertion of an animation
- In my experiment, using the resources in Android.res.anim, I found
- There are three types of animation inserts:
- Accelerate_decelerate_interpolator acceleration-deceleration animation insertion device
- Accelerate_interpolator Acceleration-Animation insertion Device
- Decelerate_interpolator deceleration-Animation insertion device
- Other animations that belong to a specific animation effect
- 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
- The Fromyscale property is the scaling dimension on the y-coordinate at the start of the animation
- The Toyscale property is the scaling dimension on the y-coordinate at the end of the animation
- Description
- Above four attribute values
- 0.0 means shrink to No
- 1.0 indicates normal no scaling
- Values less than 1.0 indicate shrinkage
- Values greater than 1.0 indicate magnification
- The Pivotx property is the start position of the animation relative to the X coordinate of the object
- The Pivoty property is the start position of the animation relative to the y-coordinate of the object
- Description
- The above two attribute values are taken from the 0%-100% value
- 50% is the midpoint position on the x or Y coordinate of the object
- Long Integer value:
- The Duration property is the duration of the animation
- Description: The time is measured in milliseconds
- Boolean-Value:
- Fillafter property When set to True, the animation conversion is applied after the animation is finished
- -
3. Translate
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <translate
- Android:fromxdelta= "30"
- Android:toxdelta= "-80"
- Android:fromydelta= "30"
- Android:toydelta= "300"
- Android:duration= "2000"
- />
- <!--translate position transfer animation effect
- 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
- The Fromydelta property is the position of the y-coordinate at the start of the animation
- The Toydelta property is the position of the y-coordinate at the end of the animation
- Attention:
- Not specified Fromxtype toxtype fromytype toytype time,
- The default is to use your own relative reference
- Long Integer value:
- The Duration property is the duration of the animation
- Description: The time is measured in milliseconds
- -
- </set>
4. Rotate
- <?xml version= "1.0" encoding= "Utf-8"?>
- <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
- <rotate
- Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
- android:fromdegrees= "0"
- Android:todegrees= "+350"
- android:pivotx= "50%"
- Android:pivoty= "50%"
- Android:duration= "/>"
- <!--rotate rotation animation effect
- Property: interpolator Specifies the insertion of an animation
- In my experiment, using the resources in Android.res.anim, I found
- There are three types of animation inserts:
- Accelerate_decelerate_interpolator acceleration-deceleration animation insertion device
- Accelerate_interpolator Acceleration-Animation insertion Device
- Decelerate_interpolator deceleration-Animation insertion device
- Other animations that belong to a specific animation effect
- Floating-point value:
- Fromdegrees property is the angle of the object at the beginning of the animation
- Todegrees property rotates the angle of the object at the end of the animation to be 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: counterclockwise rotation)
- The Pivotx property is the start position of the animation relative to the X coordinate of the object
- The Pivoty property is the start position of the animation relative to the y-coordinate of the object
- Note: The above two attribute values are taken from the 0%-100% value
- 50% is the midpoint position on the x or Y coordinate of the object
- Long Integer value:
- The Duration property is the duration of the animation
- Description: The time is measured in milliseconds
- -
- </set>
XML use animation effects in
- public static Animation Loadanimation (context context, int id)
- The first parameter is context for the program
- The second parameter ID is a reference to the animated XML file
- Example:
- Myanimation= animationutils.loadanimation (this, r.anim.my_action);
- Use the static method of the Animationutils class Loadanimation () to load an animated XML file in the XML
v. Java defining animations in code
- 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 according to its own construction method
- Myanimation_alpha = new Alphaanimation (0.1f, 1.0f);
- 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);
Six, Android code animation analysis
1. Alphaanimation
Alphaanimation Class object definitions
- 1. Private Alphaanimation Myanimation_alpha;
Alphaanimation Class object constructs
- Alphaanimation (float fromalpha, float toalpha)
- The first parameter fromalpha is the transparency at the beginning of the animation
- The second parameter, Toalpha, is the transparency at the end of an animation
- Myanimation_alpha = new Alphaanimation (0.1f, 1.0f);
- Description
- 0.0 indicates full transparency
- 1.0 means completely opaque
Set the duration of an animation
- Myanimation_alpha.setduration (5000);
- Set time duration to 5000 milliseconds
2. Scaleanimation
Scaleanimation Class object definitions
- Private Scaleanimation Myanimation_scale;
Scaleanimation Class object constructs
- Scaleanimation (float FromX, float toX, float fromY, float ToY,
- int Pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)
- The first parameter fromx is the scaling dimension on the x-coordinate at the start of the animation
- The second parameter tox the scaling dimension at the end of the animation at the x-coordinate
- The third parameter, fromy, is 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:
- Above four attribute values
- 0.0 means shrink to No
- 1.0 indicates normal no scaling
- Values less than 1.0 indicate shrinkage
- Values greater than 1.0 indicate magnification
- */
- The fifth parameter Pivotxtype for the animation on the x-axis relative to the object position type
- The sixth parameter pivotxvalue the start position of the animation relative to the x-coordinate of the object
- The seventh parameter Pivotxtype for the animation on the y-axis relative to the object position type
- The 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);
Set the duration of an animation
- Myanimation_scale.setduration (700);
- Set time duration to 700 milliseconds
3. Translateanimation
Ranslateanimation Class object definitions
- Private Translateanimation myanimation_translate;
Translateanimation Class object constructs
- Translateanimation (float Fromxdelta, float toxdelta,
- Float Fromydelta, float toydelta)
- The first argument fromxdelta the position at the start of the animation when the x-coordinate is moved
- The second parameter toxdelta is the position of movement at the end of the animation at the X coordinate
- The third parameter, Fromydelta, is the moving position on the y-coordinate at the start of the animation
- The fourth parameter, Toydelta, is the moving position on the y-coordinate at the end of the animation
Set the duration of an animation
- Myanimation_translate = new Translateanimation (10f, 100f, 10f, 100f);
- Myanimation_translate.setduration (2000);
- Set time duration to 2000 milliseconds
4. Rotateanimation
Rotateanimation Class object definitions
- Private Rotateanimation myanimation_rotate;
Rotateanimation Class object constructs
- Rotateanimation (float fromdegrees, float todegrees,
- int Pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)
- The first parameter fromdegrees is the angle of rotation at the beginning 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 x-coordinate of the object
- The 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);
Set the duration of an animation
- Myanimation_rotate.setduration (3000);
- Set time duration to 3000 milliseconds
how Java using animation effects in your 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)
- View.startanimation (Myanimation_alpha);
- View.startanimation (Myanimation_scale);
- View.startanimation (myanimation_translate);
- View.startanimation (myanimation_rotate);
Reference: http://blog.csdn.net/ithomer/article/details/7523328
Android Animation detailed