android 动画代码实现
2016-04-05 16:35
423 查看
1、效果图跟上一遍博客一样,请参考Android动画效果Animation之Tween实现简单动画;
接下来直接java代码实现如何实现动画:
定义
[java] view plaincopy
private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;
private AnimationSet animationSet;
分别是透明度动画、旋转动画、尺寸伸缩动画、移动动画
具体实现代码如下:
[java] view plaincopy
private void initAnimation() {
//透明度控制动画效果 alpha
animation_alpha=new AlphaAnimation(0.1f,1.0f);
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
animation_alpha.setRepeatCount(-1);//设置循环
animation_alpha.setDuration(5000);//设置时间持续时间为 5000毫秒
// 旋转效果rotate
animation_rotate = new RotateAnimation(0, -720,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_rotate.setRepeatCount(-1);
animation_rotate.setDuration(5000);//设置时间持续时间为 5000毫秒
//尺寸伸缩动画效果 scale
animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_scale.setRepeatCount(-1);
animation_scale.setDuration(5000);//设置时间持续时间为 5000毫秒
//移动动画效果translate
animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第三个参数toYDelta为动画结束时Y坐标上的移动位置
animation_translate.setRepeatCount(-1);//设置动画执行多少次,如果是-1的话就是一直重复
animation_translate.setDuration(5000);//设置时间持续时间为 5000毫秒
animationSet=new AnimationSet(true);
animationSet.addAnimation(animation_alpha);//透明度
animationSet.addAnimation(animation_rotate);//旋转
animationSet.addAnimation(animation_scale);//尺寸伸缩
animationSet.addAnimation(animation_translate);//移动
image.startAnimation(animationSet);//开始播放
}
Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore),
the values of AnimationSet override the child values.
意思是:代表一群动画应该一同玩耍。转变是由每个个体的动画在一起,成为一个单一的变换。如果AnimationSet设置任何属性,其子女也设置(例如,持续时间或fillBefore)的值AnimationSet推翻这孩子的价值观。
转载自: http://blog.csdn.net/rhljiayou/article/details/7194807
接下来直接java代码实现如何实现动画:
定义
[java] view plaincopy
private Animation animation_alpha,animation_scale,animation_translate,animation_rotate;
private AnimationSet animationSet;
分别是透明度动画、旋转动画、尺寸伸缩动画、移动动画
具体实现代码如下:
[java] view plaincopy
private void initAnimation() {
//透明度控制动画效果 alpha
animation_alpha=new AlphaAnimation(0.1f,1.0f);
//第一个参数fromAlpha为 动画开始时候透明度
//第二个参数toAlpha为 动画结束时候透明度
animation_alpha.setRepeatCount(-1);//设置循环
animation_alpha.setDuration(5000);//设置时间持续时间为 5000毫秒
// 旋转效果rotate
animation_rotate = new RotateAnimation(0, -720,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
//第一个参数fromDegrees为动画起始时的旋转角度 //第二个参数toDegrees为动画旋转到的角度
//第三个参数pivotXType为动画在X轴相对于物件位置类型 //第四个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第五个参数pivotXType为动画在Y轴相对于物件位置类型 //第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_rotate.setRepeatCount(-1);
animation_rotate.setDuration(5000);//设置时间持续时间为 5000毫秒
//尺寸伸缩动画效果 scale
animation_scale=new ScaleAnimation(0.1f,3.0f,0.1f,3.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//第一个参数fromX为动画起始时 X坐标上的伸缩尺寸
//第二个参数toX为动画结束时 X坐标上的伸缩尺寸
//第三个参数fromY为动画起始时Y坐标上的伸缩尺寸
//第四个参数toY为动画结束时Y坐标上的伸缩尺寸
//第五个参数pivotXType为动画在X轴相对于物件位置类型
//第六个参数pivotXValue为动画相对于物件的X坐标的开始位置
//第七个参数pivotXType为动画在Y轴相对于物件位置类型
//第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置
animation_scale.setRepeatCount(-1);
animation_scale.setDuration(5000);//设置时间持续时间为 5000毫秒
//移动动画效果translate
animation_translate=new TranslateAnimation(-20f,300f,-20f,300f);
//第一个参数fromXDelta为动画起始时 X坐标上的移动位置
//第二个参数toXDelta为动画结束时 X坐标上的移动位置
//第三个参数fromYDelta为动画起始时Y坐标上的移动位置
//第三个参数toYDelta为动画结束时Y坐标上的移动位置
animation_translate.setRepeatCount(-1);//设置动画执行多少次,如果是-1的话就是一直重复
animation_translate.setDuration(5000);//设置时间持续时间为 5000毫秒
animationSet=new AnimationSet(true);
animationSet.addAnimation(animation_alpha);//透明度
animationSet.addAnimation(animation_rotate);//旋转
animationSet.addAnimation(animation_scale);//尺寸伸缩
animationSet.addAnimation(animation_translate);//移动
image.startAnimation(animationSet);//开始播放
}
AnimationSet这个类是:
Represents a group of Animations that should be played together. The transformation of each individual animation are composed together into a single transform. If AnimationSet sets any properties that its children also set (for example, duration or fillBefore),the values of AnimationSet override the child values.
意思是:代表一群动画应该一同玩耍。转变是由每个个体的动画在一起,成为一个单一的变换。如果AnimationSet设置任何属性,其子女也设置(例如,持续时间或fillBefore)的值AnimationSet推翻这孩子的价值观。
转载自: http://blog.csdn.net/rhljiayou/article/details/7194807
相关文章推荐
- Android的性能模式:救援技巧
- Android 简单三级文字列表
- 管理照相机-控制相机硬件(摘自Android官方培训课程中文版(v0.9.5))
- Android应用中通过Layout_weight属性用ListView实现表格
- Android Studio 1.3.0 以上gradle proxy代理设置异常
- [安卓开发笔记二]android Studio通过jni调用C++代码
- Android之AsyncTask学习
- I.MX6 Android iperf3 porting failed
- 【Android 基础】Animation 动画介绍和实现
- 29.Android展示PDF文件
- 关于fragment切换失效问题
- android shape的使用
- Android ListView相关技术
- 模仿美团点评的Android应用中价格和购买栏悬浮固定的效果
- android加载html乱码解决办法
- Android Market 链接的生成与分享
- Android根据文件名(String类型)去查找R文件中的对应id(int类型)
- YY项目之帧动画(二)
- 如何设置android状态栏的透明度
- Installation error: INSTALL_FAILED_VERSION_DOWNGRADE Android