Android动画开发——Animation动画效果
Android开发, 动画效果
动画类型
Android的animation由四种类型组成
XML中
alpha
渐变透明度动画效果
scale
渐变尺寸伸缩动画效果
translate
画面转换位置移动动画效果
rotate
画面转移旋转动画效果
JavaCode中
AlphaAnimation
渐变透明度动画效果
ScaleAnimation
渐变尺寸伸缩动画效果
TranslateAnimation
画面转换位置移动动画效果
RotateAnimation
画面转移旋转动画效果
Android动画模式
Animation主要有两种动画模式:
一种是tweened animation(渐变动画)
XML中
JavaCode
alpha
AlphaAnimation
scale
ScaleAnimation
一种是frame by frame(画面转换动画)
XML中
JavaCode
translate
TranslateAnimation
rotate
RotateAnimation
如何在XML文件中定义动画
① 打开Eclipse,新建Android工程
② 在res目录中新建anim文件夹
③ 在anim目录中新建一个myanim.xml(注意文件名小写)
④ 加入XML的动画代码
1.
2.
3.
4.
5.
6.
7.
复制代码
Android动画解析--XML
1.
2.
3.
8.
22.
复制代码
1.
2.
3.
14.
15.
复制代码
1.
2.
3.
10.
23.
复制代码
1.
2.
3.
10.
42.
复制代码
如何使用XML中的动画效果
1. public static Animation loadAnimation (Context context, int id)
2. //第一个参数Context为程序的上下文
3. //第二个参数id为动画XML文件的引用
4. //例子:
5. myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_action);
6. //使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件
复制代码
如何在Java代码中定义动画
1. //在代码中定义 动画实例对象
2. private Animation myAnimation_Alpha;
3. private Animation myAnimation_Scale;
4. private Animation myAnimation_Translate;
5. private Animation myAnimation_Rotate;
6.
7. //根据各自的构造方法来初始化一个实例对象
8. myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
9.
10. myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
11. Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
12.
13. myAnimation_Translate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
14.
15. myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
16. Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
复制代码
Android动画解析--JavaCode
AlphaAnimation
① AlphaAnimation类对象定义
1. 1. private AlphaAnimation myAnimation_Alpha;
复制代码
② AlphaAnimation类对象构造
1. AlphaAnimation(float fromAlpha, float toAlpha)
2. //第一个参数fromAlpha为 动画开始时候透明度
3. //第二个参数toAlpha为 动画结束时候透明度
4. myAnimation_Alpha=new AlphaAnimation(0.1f, 1.0f);
5. //说明:
6. // 0.0表示完全透明
7. // 1.0表示完全不透明
复制代码
③ 设置动画持续时间
1. myAnimation_Alpha.setDuration(5000);
2. //设置时间持续时间为 5000毫秒
复制代码
ScaleAnimation
① ScaleAnimation类对象定义
1. private AlphaAnimation myAnimation_Alpha;
复制代码
② ScaleAnimation类对象构造
1. ScaleAnimation(float fromX, float toX, float fromY, float toY,
2. int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
3. //第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
4. //第二个参数toX为动画结束时 X坐标上的伸缩尺寸
5. //第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
6. //第四个参数toY为动画结束时Y坐标上的伸缩尺寸
7. /*说明:
8. 以上四种属性值
9. 0.0表示收缩到没有
10. 1.0表示正常无伸缩
11. 值小于1.0表示收缩
12. 值大于1.0表示放大
13. */
14. //第五个参数pivotXType为动画在X轴相对于物件位置类型
15. //第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
16. //第七个参数pivotXType为动画在Y轴相对于物件位置类型
17. //第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
18. myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
19. Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
复制代码
③ 设置动画持续时间
1. myAnimation_Scale.setDuration(700);
2. //设置时间持续时间为 700毫秒
复制代码
TranslateAnimation
① TranslateAnimation类对象定义
1. private AlphaAnimation myAnimation_Alpha;
复制代码
② TranslateAnimation类对象构造
1. TranslateAnimation(float fromXDelta, float toXDelta,
2. float fromYDelta, float toYDelta)
3. //第一个参数fromXDelta为动画起始时 X坐标上的移动位置
4. //第二个参数toXDelta为动画结束时 X坐标上的移动位置
5. //第三个参数fromYDelta为动画起始时Y坐标上的移动位置
6. //第四个参数toYDelta为动画结束时Y坐标上的移动位置
复制代码
③ 设置动画持续时间
1. myAnimation_Translate.setDuration(2000);
2. //设置时间持续时间为 2000毫秒
复制代码
RotateAnimation
① RotateAnimation类对象定义
1. private AlphaAnimation myAnimation_Alpha;
复制代码
② RotateAnimation类对象构造
1. RotateAnimation(float fromDegrees, float toDegrees,
2. int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
3. //第一个参数fromDegrees为动画起始时的旋转角度
4. //第二个参数toDegrees为动画旋转到的角度
5. //第三个参数pivotXType为动画在X轴相对于物件位置类型
6. //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
7. //第五个参数pivotXType为动画在Y轴相对于物件位置类型
8. //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
9. myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f,
10. Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
复制代码
③ 设置动画持续时间
1. myAnimation_Rotate.setDuration(3000);
2. //设置时间持续时间为 3000毫秒
复制代码
如何使用Java代码中的动画效果
使用从View父类继承过来的方法startAnimation()来为View或是子类View等等添加一个动画效果
1. public void startAnimation (Animation animation)
复制代码
Android动画开发——Animation动画效果
最新推荐文章于 2025-03-11 21:49:04 发布