android:
interpolator="@android:
anim/accelerate_decelerate_interpolator"
android:
fromDegrees="0"
android:
toDegrees="+350"
android:
pivotX="50%"
android:
pivotY="50%"
android:
duration="3000"/>
--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属性为动画持续时间
说明:
时间以毫秒为单位
-->
如何使用XML中的动画效果
1
2
3
4
5
6
publicstaticAnimationloadAnimation(Contextcontext,intid)
//第一个参数Context为程序的上下文
//第二个参数id为动画XML文件的引用
//例子:
myAnimation=AnimationUtils.loadAnimation(this,R.anim.my_action);
//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
如何在Java代码中定义动画
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//在代码中定义动画实例对象
privateAnimationmyAnimation_Alpha;
privateAnimationmyAnimation_Scale;
privateAnimationmyAnimation_Translate;
privateAnimationmyAnimation_Rotate;
//根据各自的构造方法来初始化一个实例对象
myAnimation_Alpha=newAlphaAnimation(0.1f,1.0f);
myAnimation_Scale=newScaleAnimation(0.0f,1.4f,0.0f,1.4f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
myAnimation_Translate=newTranslateAnimation(30.0f,-80.0f,30.0f,300.0f);
myAnimation_Rotate=newRotateAnimation(0.0f,+350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
Android动画解析–JavaCode
AlphaAnimation
① AlphaAnimation类对象定义
1
privateAlphaAnimationmyAnimation_Alpha;
② AlphaAnimation类对象构造
1
2
3
4
5
6
7
AlphaAnimation(floatfromAlpha,floattoAlpha)
//第一个参数fromAlpha为动画开始时候透明度
//第二个参数toAlpha为动画结束时候透明度
myAnimation_Alpha=newAlphaAnimation(0.1f,1.0f);
//说明:
//0.0表示完全透明
//1.0表示完全不透明
③ 设置动画持续时间
1
2
myAnimation_Alpha.setDuration(5000);
//设置时间持续时间为5000毫秒
ScaleAnimation
① ScaleAnimation类对象定义
1
privateAlphaAnimationmyAnimation_Alpha;
② ScaleAnimation类对象构造
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ScaleAnimation(floatfromX,floattoX,floatfromY,floattoY,
intpivotXType,floatpivotXValue,intpivotYType,floatpivotYValue)
//第一个参数fromX为动画起始时X坐标上的伸缩尺寸
//第二个参数toX为动画结束时X坐标上的伸缩尺寸
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
/*说明:
以上四种属性值
0.0表示收缩到没有
1.0表示正常无伸缩
值小于1.0表示收缩
值大于1.0表示放大
*/
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Scale=newScaleAnimation(0.0f,1.4f,0.0f,1.4f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
③ 设置动画持续时间
1
2
myAnimation_Scale.setDuration(700);
//设置时间持续时间为700毫秒
TranslateAnimation
① TranslateAnimation类对象定义
1
privateAlphaAnimationmyAnimation_Alpha;
② TranslateAnimation类对象构造
1
2
3
4
5
6
TranslateAnimation(floatfromXDelta,floattoXDelta,
floatfromYDelta,floattoYDelta)
//第一个参数fromXDelta为动画起始时X坐标上的移动位置
//第二个参数toXDelta为动画结束时X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第四个参数toYDelta为动画结束时Y坐标上的移动位置
③ 设置动画持续时间
1
2
myAnimation_Translate.setDuration(2000);
//设置时间持续时间为2000毫秒
RotateAnimation
① RotateAnimation类对象定义
1
privateAlphaAnimationmyAnimation_Alpha;
② RotateAnimation类对象构造
1
2
3
4
5
6
7
8
9
10
RotateAnimation(floatfromDegrees,floattoDegrees,
intpivotXType,floatpivotXValue,intpivotYType,floatpivotYValue)
//第一个参数fromDegrees为动画起始时的旋转角度
//第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型
//第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型
//第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
myAnimation_Rotate=newRotateAnimation(0.0f,+350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
③ 设置动画持续时间
1
2
myAnimation_Rotate.setDuration(3000);
//设置时间持续时间为3000毫秒
如何使用Java代码中的动画效果
使用从View父类继承过来的方法startAnimation()来为View或是子类View等等添加一个动画效果
1
publicvoidstartAnimation(Animationanimation)
结果: