This article describes the usage of Animations animations in Android development. Share it for your reference, as follows:
1. Animation type
Android animation consists of four types:alpha、scale、translate、rotate
In XML configuration file
Java Code in
2. Android animation mode
Animation has two main animation modes:tweened and frame
One is tweened animation (gradual animation)
One is frame by frame (screen conversion animation)
3. Define animations in XML files
① Open Eclipse and create a new Android project
② Create a new anim folder in the res directory
③ Create a new one in the anim directory (note the lowercase file name)
④ Add XML animation code
<?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <alpha/> <scale/> <translate/> <rotate/> </set>
4. Android XML animation analysis
1. Alpha
<?xml version="1.0" encoding="utf-8"?> <set xmlns:andro > <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="3000" /> <!-- Transparency control animation effect alpha Floating point value: fromAlpha The attribute is transparency at the beginning of the animation toAlpha The attribute is transparency at the end of the animation illustrate: 0.0Indicates complete transparency 1.0Indicates total opaqueness The above value is taken0.0-1.0BetweenfloatNumber of data type Long integer value: duration The attribute is the animation duration illustrate: Time in milliseconds --> </set>
2. Scale
<?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <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="700" /> </set> <!-- Size telescopic animation effect scale property:interpolator Specify an animation inserter During my experiment,Discovering resources in use There are three animation inserters: accelerate_decelerate_interpolator accelerate-slow down Animation Insertler accelerate_interpolator accelerate-Animation Insertler decelerate_interpolator slow down- Animation Insertler Others belong to specific animation effects Floating point value: fromXScale property为动画起始时 XScaling dimensions on coordinates toXScale property为动画结束时 XScaling dimensions on coordinates fromYScale property为动画起始时YScaling dimensions on coordinates toYScale property为动画结束时YScaling dimensions on coordinates illustrate: 以上四种property值 0.0Indicates that it has contracted to no 1.0It means no scaling normally Value is less than1.0Indicates shrinkage Value greater than1.0Indicates enlargement pivotX property为动画相对于物件的XThe start position of the coordinates pivotY property为动画相对于物件的YThe start position of the coordinates illustrate: 以上两个property值 from0%-100%Medium value 50%For objectsXorYMidpoint position on the direction coordinate Long integer value: duration property为动画持续时间 illustrate: Time in milliseconds Boolean value: fillAfter property When set totrue ,The animation conversion is applied after the animation is finished -->
3. Translate
<?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <translate android:fromXDelta="30" android:toXDelta="-80" android:fromYDelta="30" android:toYDelta="300" android:duration="2000" /> <!-- translate Position to move the effect Integer value: fromXDelta Attributes are at the beginning of the animation XPosition on coordinates toXDelta At the end of the animation XPosition on coordinates fromYDelta Attributes are at the beginning of the animation YPosition on coordinates toYDelta At the end of the animation YPosition on coordinates Notice: No specifiedfromXType toXType fromYType toYType when, By default, you are the relative reference 长Integer value: duration The attribute is the animation duration illustrate: Time in milliseconds --> </set>
4. Rotate
<?xml version="1.0" encoding="utf-8"?> <set xmlns:andro> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="3000" /> <!-- rotate Rotate animation effect property:interpolator Specify an animation inserter During my experiment,Discovering resources in use There are three animation inserters: accelerate_decelerate_interpolator accelerate-slow down Animation Insertler accelerate_interpolator accelerate-Animation Insertler decelerate_interpolator slow down- Animation Insertler Others belong to specific animation effects Floating point numeric value: fromDegrees property为动画起始时物件的角Spend toDegrees property为动画结束时物件旋转的角Spend Can be greater than360Spend illustrate: 当角Spend为negative number——Indicates counterclockwise rotation 当角Spend为Positive number——Indicates clockwise rotation (negative numberfrom——toPositive number:Rotate clockwise) (negative numberfrom——tonegative number:Rotate counterclockwise) (Positive numberfrom——toPositive number:Rotate clockwise) (Positive numberfrom——tonegative number:Rotate counterclockwise) pivotX property为动画相对于物件的XThe start position of the coordinates pivotY property为动画相对于物件的YThe start position of the coordinates illustrate: 以上两个property值 from0%-100%Medium value 50%For objectsXorYMidpoint position on the direction coordinate Long integer value: duration property为动画持续时间 illustrate: Time in milliseconds --> </set>
Use animation effects 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 animation XML file//example:myAnimation= (this, .my_action); //Use the static method loadAnimation() of the AnimationUtils class to loadAnimation() to load the animation XML file in XML
5. Define animations in Java code
//Define animated instance object in the codeprivate Animation myAnimation_Alpha; private Animation myAnimation_Scale; private Animation myAnimation_Translate; private Animation myAnimation_Rotate; //Initialize an instance object according to their respective construction methodsmyAnimation_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);
6. Android code animation analysis
1. AlphaAnimation
AlphaAnimation class object definition
1. private AlphaAnimation myAnimation_Alpha;
AlphaAnimation class object construction
AlphaAnimation(float fromAlpha, float toAlpha) //The first parameter fromAlpha is transparency at the beginning of the animation//The second parameter toAlpha is transparency at the end of the animationmyAnimation_Alpha = new AlphaAnimation(0.1f, 1.0f); //illustrate:// 0.0 means complete transparency// 1.0 means total opaque
Set animation duration
myAnimation_Alpha.setDuration(5000); //Set the time duration to 5000 milliseconds
2. ScaleAnimation
ScaleAnimation class object definition
private ScaleAnimation myAnimation_Scale;
ScaleAnimation class object construction
ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) //The first parameter fromX is the scaling size on the X coordinate at the beginning of the animation//The second parameter toX is the scaling size on the X coordinate at the end of the animation//The third parameter fromY is the scaling size on the Y coordinate at the beginning of the animation//The fourth parameter toY is the scaling size on the Y coordinate at the end of the animation/*illustrate: The above four attribute values 0.0 means shrinking to no 1.0 means normal no scaling Value less than 1.0 indicates shrinkage Value greater than 1.0 means enlargement */ //The fifth parameter pivotXType is the type of position of the animation relative to the object in the X-axis//The sixth parameter pivotXValue is the starting position of the animation relative to the X coordinate of the object//The seventh parameter pivotXType is the position type of the animation in the Y axis relative to the object position//The eighth parameter pivotYValue is the starting position of the animation relative to the object's Y coordinatesmyAnimation_Scale = new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Set animation duration
myAnimation_Scale.setDuration(700); //Set the time duration to 700 milliseconds
3. TranslateAnimation
ranslateAnimation class object definition
private TranslateAnimation myAnimation_Translate;
TranslateAnimation class object construction
TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) //The first parameter fromXDelta is the moving position on the X coordinate at the beginning of the animation//The second parameter toXDelta is the moving position on the X coordinate at the end of the animation//The third parameter fromYDelta is the moving position on the Y coordinate at the beginning of the animation//The fourth parameter toYDelta is the movement position on the Y coordinate at the end of the animation
Set animation duration
myAnimation_Translate = new TranslateAnimation(10f, 100f, 10f, 100f); myAnimation_Translate.setDuration(2000); //Set the time duration to 2000 milliseconds
4. RotateAnimation
RotateAnimationClass object definition private RotateAnimation myAnimation_Rotate; RotateAnimationClass object construction RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) //The first parameter fromDegrees is the rotation angle at the beginning of the animation//The second parameter toDegrees is the angle to which the animation rotates//The third parameter pivotXType is the animation position type relative to the object in the X axis//The fourth parameter pivotXValue is the starting position of the animation relative to the X coordinate of the object//The fifth parameter pivotXType is the type of the animation position relative to the object in the Y axis//The sixth parameter pivotYValue is the starting position of the animation relative to the object's Y coordinatesmyAnimation_Rotate = new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
Set animation duration
myAnimation_Rotate.setDuration(3000); //Set the time duration to 3000 milliseconds
How to use animation effects in Java code
Use the method startedAnimation() inherited from the View parent class to add an animation effect to the View or subclass View, etc.
public void startAnimation (Animation animation) (myAnimation_Alpha); (myAnimation_Scale); (myAnimation_Translate); (myAnimation_Rotate);
For more information about Android related content, please check out the topic of this site:Android development animation skills summary》、《Android development introduction and advanced tutorial"and"Android control usage summary》。
I hope this article will be helpful to everyone's Android programming design.