补间动画的优点是可以节省空间。补间动画与逐帧动画在本质上是不同的,逐帧动画通过连续播放图片来模拟动画的效果,而补间动画则是通过在两个关键帧 之间补充渐变的动画效果来实现的。目前Android应用框架支持的补间动画效果有以下5种。具体实现在android.view.animation类 库中。
- AlphaAnimation:透明度(alpha)渐变效果,对应<alpha/>标签。
- TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。
- ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。
RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。
- AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。
补间动画的效果同样可以使用XML语言来定义,这些动画模板文件通常会被放在Android项目的res/anim/目录下。
主代码
public class MainActivity extends Activity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}
public void click1(View v) {
AlphaAnimation ani = new AlphaAnimation(0.0f, 1.0f);
ani.setDuration(2000);
ani.setRepeatCount(2);
ani.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ani);
}
public void click11(View v) {
Animation ani = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
iv.startAnimation(ani);
}
public void click2(View v) {
ScaleAnimation ani = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
ani.setDuration(2000);
ani.setRepeatCount(2);
ani.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ani);
}
public void click22(View v) {
Animation ani = AnimationUtils.loadAnimation(this, R.anim.rotate_ani);
iv.startAnimation(ani);
}
public void click3(View v) {
RotateAnimation ani = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
ani.setDuration(2000);
ani.setRepeatCount(2);
ani.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ani);
}
public void click33(View v) {
Animation ani = AnimationUtils.loadAnimation(this, R.anim.scale_ani);
iv.startAnimation(ani);
}
public void click4(View v) {
TranslateAnimation ani = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 1.0f);
ani.setDuration(2000);
ani.setRepeatCount(2);
ani.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ani);
}
public void click44(View v) {
Animation ani = AnimationUtils.loadAnimation(this, R.anim.translate);
iv.startAnimation(ani);
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
Animation的xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.5"
android:fillAfter="true"
android:duration="2000" >
</alpha>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" >
</rotate>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.2"
android:toXScale="2.0"
android:fromYScale="0.2"
android:toYScale="2.0"
android:fillAfter="true"
android:duration="2000" >
</scale>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="20%p"
android:toXDelta="50%p"
android:fromYDelta="0"
android:toYDelta="50%p"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse" >
</translate>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
代码解析
- alpha
fromAlpha :起始透明度
toAlpha:结束透明度
1.0表示完全不透明
0.0表示完全透明
- rotate
fromDegrees:表示旋转的起始角度
toDegrees:表示旋转的结束角度
repeatCount:旋转的次数 默认值是0 代表旋转1次 如果值是repeatCount=4 旋转5次,值为-1或者infinite时,表示补间动画永不停止
repeatMode 设置重复的模式。默认是restart。当repeatCount的值大于0或者为infinite时才有效。
repeatCount=-1 或者infinite循环了 还可以设成reverse,表示偶数次显示动画时会做与动画文件定义的方向相反的方向动行。
- scale
fromXScale:表示沿着x轴缩放的起始比例
toXScale:表示沿着x轴缩放的结束比例
fromYScale:表示沿着y轴缩放的起始比例
toYScale:表示沿着y轴缩放的结束比例
图片中心点:
android:pivotX="50%"
android:pivotY="50%"
- 1.
- 2.
- translate
android:interpolator 动画的渲染器
accelerate_interpolator(动画加速器) 使动画在开始的时候 最慢,然后逐渐加速
decelerate_interpolator(动画减速器)使动画在开始的时候 最快,然后逐渐减速
accelerate_decelerate_interpolator(动画加速减速器)
中间位置分层: 使动画在开始的时候 最慢,然后逐渐加速
使动画在开始的时候 最快,然后逐渐减速 结束的位置最慢
fromXDelta 动画起始位置的横坐标
toXDelta 动画起结束位置的横坐标
fromYDelta 动画起始位置的纵坐标
toYDelta 动画结束位置的纵坐标
duration 动画的持续时间
在实际项目中,我们经常使用补间动画,原因是补间动画使用起来比较方便,功能也比逐帧动画强大不少,而且还可以很方便地进行动画叠加,实现更加复杂的效果。