Android动画
- View Animation 视图动画(Tween Animation 补间动画),只能用来设置View的动画
- Drawable Animation 帧动画(Frame动画),一帧帧地显示资源文件中的Drawable
- Property Animation 属性动画,在android3.0以上的系统才有。这动画可以设置给任何的Object,包括那些还没有渲染到屏幕的view.
为什么要引入属性动画?
- 补间动画只能够作用在View上的
- 补间动画只能够实现移动、缩放、旋转和淡入淡出这四种动画操作,不能改变View的背景等
- 补间动画只是改变了View的显示效果而已,而不会真正去改变View的属性
View Animation 补间动画
视图动画也叫补间动画,指在一个视图容器中执行一些变换。包含有:位置、大小、旋转、透明 补间动画。
一般通过xml实现,不建议是用android代码实现,因为代码实现的可读性比较差。
补间动画的相关类
- AlphaAnimation <alpha>放在res/anim/目录下 透明渐变动画效果
- RotateAnimation <rotate>放在res/anim/目录下 旋转转移动画效果
- ScaleAnimation <scale>放在res/anim/目录下 缩放动画效果
- TranslateAnimation <translate>放在res/anim/目录下 移动动画效果
- AnimationSet <set> 放在res/anim/目录下 持有动画的容器
补间动画之RotateAnimation使用
RotateAnimation动画的实现可以分类两种,一种是使用Java代码实现,另外一种使用xml。
使用java代码实现
RotateAnimation初始化代码片段
/**
* init animation 1
*/
private void initAnimation1() {
/**
* 0 - 359度旋转
* 相对于自身中心位置
*/
RotateAnimation mRotateAnimation = new RotateAnimation(0, 359,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
//设置线性插值,可以解决旋转一圈后卡顿问题
mRotateAnimation.setInterpolator(new LinearInterpolator());
//设置旋转一圈时间
mRotateAnimation.setDuration(1000);
//设置重复旋转次数, Animation.INFINITE表示无限次
mRotateAnimation.setRepeatCount(Animation.INFINITE);
//设置旋转模式[restart 重新旋转; reverse 旋转一圈后反转]
mRotateAnimation.setRepeatMode(Animation.RESTART);
return;
}
动画的启动和停止
//启动动画 imageView.startAnimation(mRotateAnimation); // 停止动画 imageView.clearAnimation();
使用xml实现
res/anim/anim_rotate.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="1500"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toDegrees="359" />
<!--
duration : 一圈旋转时间
fromDegrees : 开始角度
toDegrees : 结束角度
pivotX : 旋转中心距离view的左顶点为50%距离
pivotY : 旋转中心距离view的左顶点为50%距离
repeatCount="-1":重复次数,-1为一直重复
repeatMode="restart":重复模式,restart从头开始重复 reverse 旋转完后反转旋转
-->
</set>
/**
* init animaton 2
*/
private void initAnimation2() {
//加载xml中的动画
Animation mAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
//设置线性插值
mAnimation.setInterpolator(new LinearInterpolator());
return;
}
