1. Android animation consists of four types:
In XML:
alph | Gradient transparency animation effect |
scale | Gradient size scaling animation effect |
translate | Screen transition position moving animation effect |
rotate | Screen transfer rotation animation effect |
AlphaAnimation | Gradient transparency animation effect |
ScaleAnimation | Gradient size scaling animation effect |
TranslateAnimation | Screen transition position moving animation effect |
RotateAnimation | Screen transfer rotation animation effect |
① Create a new anim folder in the res directory of your Android project
② Create a new anim.xml in the anim directory
③ In anim.xml tagging, as follows:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha />
<scale />
<translate />
<rotate />
</set>
3. Android animation attribute analysis
3.1 alpha represents the gradient transparency
<?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" />
<!--
透明度控制动画效果 alpha
浮点型值:
fromAlpha 属性为动画起始时透明度
toAlpha 属性为动画结束时透明度
说明:
0.0表示完全透明
1.0表示完全不透明
以上值取0.0-1.0之间的float数据类型的数字
长整型值:
duration 属性为动画持续时间
说明:
时间以毫秒为单位
-->
</set>
3.2
scale indicates that the gradient size is stretched
<?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:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.4"
android:toYScale="1.4" />
</set><!--
尺寸伸缩动画效果 scale
属性:interpolator 指定一个动画的插入器
在我试验过程中,使用android.res.anim中的资源时候发现
有三种动画插入器:
accelerate_decelerate_interpolator 加速-减速 动画插入器
accelerate_interpolator 加速-动画插入器
decelerate_interpolator 减速- 动画插入器
其他的属于特定的动画效果
浮点型值:
fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
toXScale 属性为动画结束时 X坐标上的伸缩尺寸
fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
toYScale 属性为动画结束时Y坐标上的伸缩尺寸
说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
说明:
以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置
长整型值:
duration 属性为动画持续时间
说明: 时间以毫秒为单位
布尔型值:
fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用
-->
3.3
translate means that the screen transition position moves
<?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" />
<!--
translate 位置转移动画效果
整型值:
fromXDelta 属性为动画起始时 X坐标上的位置
toXDelta 属性为动画结束时 X坐标上的位置
fromYDelta 属性为动画起始时 Y坐标上的位置
toYDelta 属性为动画结束时 Y坐标上的位置
注意:
没有指定fromXType toXType fromYType toYType 时候,
默认是以自己为相对参照物
长整型值:
duration 属性为动画持续时间
说明: 时间以毫秒为单位
-->
</set>
3.4 rotate represents the screen transfer rotation
<?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:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+350" />
<!--
rotate 旋转动画效果
属性:interpolator 指定一个动画的插入器
在我试验过程中,使用android.res.anim中的资源时候发现
有三种动画插入器:
accelerate_decelerate_interpolator 加速-减速 动画插入器
accelerate_interpolator 加速-动画插入器
decelerate_interpolator 减速- 动画插入器
其他的属于特定的动画效果
浮点数型值:
fromDegrees 属性为动画起始时物件的角度
toDegrees 属性为动画结束时物件旋转的角度 可以大于360度
说明:
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
说明: 以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置
长整型值:
duration 属性为动画持续时间
说明: 时间以毫秒为单位
-->
</set>
4. How to use animation in code
Animation myAnimation;
myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_anim);
//在代码中定义 动画实例对象
private Animation myAnimation_Alpha;
private Animation myAnimation_Scale;
private Animation myAnimation_Translate;
private Animation myAnimation_Rotate;
//根据各自的构造方法来初始化一个实例对象
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);
5.Android animation analysis--JavaCode
5.1 AlphaAnimation
① AlphaAnimation class object definition
private AlphaAnimation myAnimation_Alpha
② AlphaAnimation class object construction
//The first parameter fromAlpha is the transparency at the beginning of the animation //The second parameter toAlpha is the transparency at the end of the animation AlphaAnimation( float fromAlpha, float toAlpha ) // Note: 0.0 means completely transparent, 1.0 means completely opaque myAnimation_Alpha = new AlphaAnimation( 0.1f, 1.0f);③ Set the animation duration
// Set the time duration to 5000 milliseconds myAnimation_Alpha.setDuration(5000);5.2 ScaleAnimation
① ScaleAnimation class object definition
private AlphaAnimation myAnimation_Alpha;
② ScaleAnimation class object structure
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 telescopic size on the X coordinate at the end of the animation //The third parameter fromY is the telescopic size on the Y coordinate at the beginning of the animation //The fourth parameter toY is the telescopic size on the Y coordinate at the end of the animation /* Description: the above four attribute value 0.0 indicates that no shrinkage 1.0 indicates a normal non-stretch value of less than 1.0 represents a shrinkage value greater than 1.0 represents an enlarged */ //The fifth parameter pivotXType is the position type of the animation on the X axis relative to the object //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 animation relative to the Y axis In the object position type // the eighth parameter pivotYValue 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);③ Set the animation duration
// Set the time duration to 700 milliseconds myAnimation_Scale.setDuration(700);5.3 TranslateAnimation
① TranslateAnimation class object definition
private AlphaAnimation myAnimation_Alpha;
② TranslateAnimation class object construction
// The first parameter is fromXDelta animation start moving position on the X-coordinate // The second parameter is the end of the animation toXDelta movement position of the X-coordinate // third parameter fromYDelta starting Y coordinate when the animation The moving position of // 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 time duration to 2000 milliseconds myAnimation_Translate.setDuration(2000);5.4 RotateAnimation
① RotateAnimation class object definition
private AlphaAnimation myAnimation_Alpha;
② RotateAnimation class object construction
RotateAnimation(float fromDegrees, float toDegrees,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) //第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度 //第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置 //第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置 myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);③ RotateAnimation类对象构造
// Set the time duration to 3000 milliseconds myAnimation_Rotate.setDuration(3000);6. How to use animation effects in Java code
Use the method startAnimation() inherited from the View parent class to add an animation effect to View or subclass View, etc.
public void startAnimation (Animation animation)