Android animation, androidanimation
Android animation Basics
Animation type
Android animation consists of four types
In XML
Alpha |
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 |
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 |
How to define an animation in an XML file
① Open Eclipse and create an Android Project
② Create an anim folder in the res directory
③ Create a new myanim. xml file in the anim directory (note that the file name is in lower case)
④ Add XML animation code
<?xml version="1.0" encoding="utf-8"?><set xmlns:android ="http://schemas.android.com/apk/res/android"> <alpha/> <scale/> <translate/> <rotate/></set>
Android animation Parsing
-- XML
<Alpha> <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <alphaandroid: fromAlpha = "0.1" android: toAlpha = "1.0" android: duration = "3000"/> <! -- 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>
<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: effectx = "50%" android: effecty = "50%" android: fillAfter = "false" android: duration = "700"/> </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 -->
<Translate> <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <translateandroid: fromXDelta = "30" android: toXDelta = "-80" android: fromYDelta = "30" android: toYDelta = "300" android: duration = "2000"/> <! -- 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>
<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: durtx = "50%" android: Durty = "50%" android: duration = "3000"/> <! -- 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>
// How to use the Animation effect public static Animation loadAnimation (Context context, int id) in XML) // The first parameter Context is the Context of the program // The second parameter id is the reference of the animated XML file // example: myAnimation = AnimationUtils. loadAnimation (this, R. anim. my_action); // use the static method loadAnimation () of the AnimationUtils class () to load the Animation XML file in XML. How can I define an Animation in Java code? // define the Animation Instance Object private Animation myAnimation_Alpha in the Code; private Animation myAnimation_Scale; private Animation myAnimation_Translate; private Animation attributes; // Initialize an instance object myAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f) based on 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 );
Android animation parsing-JavaCode
AlphaAnimation
① AlphaAnimation class object definition
- Private AlphaAnimation myAnimation_Alpha;
Copy code
② AlphaAnimation Class Object Construction
- AlphaAnimation (float fromAlpha, float toAlpha)
- // The first parameter fromAlpha indicates the transparency when the animation starts.
- // The second toAlpha parameter indicates the transparency at the animation end.
- MyAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f );
- // Description:
- // 0.0 indicates completely transparent
- // 1.0 indicates completely opaque
Copy code
③ Set the animation duration
- MyAnimation_Alpha.setDuration (5000 );
- // Set the duration to 5000 milliseconds
Copy code
ScaleAnimation
① ScaleAnimation class object definition
- Private AlphaAnimation myAnimation_Alpha;
Copy code
② ScaleAnimation Class Object Construction
- ScaleAnimation (float fromX, float toX, float fromY, float toY,
- Int struct txtype, float struct txvalue, int struct tytype, float struct tyvalue)
- // The first parameter fromX is the scaling size on the X coordinate when the animation starts.
- // The second toX parameter is the scaling size on the X coordinate at the animation end.
- // The third parameter fromY is the scaling size on the Y coordinate when the animation starts.
- // The fourth toY parameter is the scaling size on the Y coordinate at the animation end.
- /* Description:
- The preceding four property values
- 0.0 indicates shrinking to none
- 1.0 indicates normal scaling-free
- A value smaller than 1.0 indicates contraction.
- Value greater than 1.0 indicates Amplification
- */
- // The Fifth parameter "animation txtype" indicates the animation position type on the X axis relative to the object.
- // The sixth parameter, txvalue, is the starting position of the animation relative to the X coordinate of the object.
- // The Seventh parameter "animation txtype" is the animation position type on the Y axis relative to the object.
- // The eighth parameter polictyvalue is the starting position of the animation relative to the Y coordinate of 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 );
Copy code
③ Set the animation duration
- MyAnimation_Scale.setDuration (700 );
- // Set the duration to 700 milliseconds
Copy code
TranslateAnimation
① TranslateAnimation class object definition
- Private AlphaAnimation myAnimation_Alpha;
Copy code
② Construct a TranslateAnimation Class Object
- TranslateAnimation (float fromXDelta, float toXDelta,
- Float fromYDelta, float toYDelta)
- // 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 moving position on the Y coordinate when the animation starts.
- // The fourth parameter toYDelta is the moving position of the Y coordinate at the animation end.
Copy code
③ Set the animation duration
- MyAnimation_Translate.setDuration (2000 );
- // Set the duration to 2000 milliseconds
Copy code
RotateAnimation
① RotateAnimation class object definition
- Private AlphaAnimation myAnimation_Alpha;
Copy code
② RotateAnimation Class Object Construction
- RotateAnimation (float fromDegrees, float toDegrees,
- Int struct txtype, float struct txvalue, int struct tytype, float struct tyvalue)
- // The first parameter fromDegrees is the rotation angle at the start of the animation.
- // The second parameter toDegrees indicates the animation rotation angle.
- // The third parameter "animation txtype" indicates the animation position type on the X axis relative to the object.
- // The fourth argument txvalue is the starting position of the animation relative to the X coordinate of the object.
- // The Fifth parameter "effectxtype" indicates the animation position type on the Y axis relative to the object.
- // The sixth parameter polictyvalue is the starting position of the animation relative to the Y coordinate of the object.
- MyAnimation_Rotate = new RotateAnimation (0.0f, + 3500000f,
- Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
Copy code
③ Set the animation duration
- MyAnimation_Rotate.setDuration (3000 );
- // Set the duration to 3000 milliseconds
Copy code
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)