AnimationSet 动画集合
Android 提供了AnimationSet 动画集合用于将多种补间动画联合起来一起使用,这样就能实现更多复杂的动画效果。动画集合既可以使用AnimationSet 类来定义也可以在XML 文件中使用<set>节点来定义。
一、 使用XML 文件实现动画集合
在res/anim 目录下创建xml 文件,set_demo.xml。文件清单如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="false" >
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toDegrees="360" >
</rotate>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="20%"
android:fromYScale="20%"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:repeatMode="reverse"
android:toXScale="200%"
android:toYScale="200%" >
</scale>
</set>
在Activity 中添加如下方法,实现业务功能:
/**
* 动画合集 集合
*/
public void set(View view){
Animation set = AnimationUtils.loadAnimation(this, R.anim.set_demo);
iv.startAnimation(set);
}
通过上面的代码我们发现,代码逻辑跟实现普通的代码其实是一模一样的,无非就是使用到的动画资源文件不一样罢了。
二、 使用Java 代码实现动画集合
使用Java 代码实现动画集合的核心代码如下所示:
/**
* 动画合集 集合
*/
public void set(View view){
AnimationSet set = new AnimationSet(false);
RotateAnimation ra = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//动画播放的时间长度
ra.setDuration(2000);
//设置重复播放的次数
ra.setRepeatCount(Animation.INFINITE);
//设置重复播放的模式
ra.setRepeatMode(Animation.REVERSE);
ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//动画播放的时间长度
sa.setDuration(2000);
//设置重复播放的次数
sa.setRepeatCount(Animation.INFINITE);
//设置重复播放的模式
sa.setRepeatMode(Animation.REVERSE);
//让iv播放aa动画
TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
0,
Animation.RELATIVE_TO_SELF,
1f,
Animation.RELATIVE_TO_SELF,
0,
Animation.RELATIVE_TO_SELF,
1f);
//动画播放的时间长度
ta.setDuration(2000);
//设置重复播放的次数
ta.setRepeatCount(Animation.INFINITE);
//设置重复播放的模式
ta.setRepeatMode(Animation.REVERSE);
set.addAnimation(ta);
set.addAnimation(sa);
set.addAnimation(ra);
iv.startAnimation(set);
}