AnimationSet:动画集合
同样,创建AnimationSet也有两种方式
- XML文件+Java代码
- Java代码方式
XML文件+Java代码
文件目录:res/anim/animation_set.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:shareInterpolator="true">
<alpha
android:duration="2000"
android:fillAfter="true"
android:fillBefore="false"
android:fromAlpha="1.0"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toAlpha="0.0"/>
<rotate
android:duration="2000"
android:fillAfter="true"
android:fillBefore="false"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toDegrees="360"/>
<set
android:interpolator="@android:anim/bounce_interpolator"
android:shareInterpolator="true"
android:startOffset="1000">
<scale
android:duration="2000"
android:fillAfter="true"
android:fillBefore="false"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toXScale="2.0"
android:toYScale="2.0"/>
<translate
android:duration="2000"
android:fillAfter="true"
android:fillBefore="false"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="-1"
android:repeatMode="reverse"
android:toXDelta="200%"
android:toYDelta="200%"/>
</set>
</set>
动画解读:先是渐变动画和旋转动画同时动,后面的缩放动画和位移动画延时1000毫秒
Java代码:
AnimationSet animationSet = (AnimationSet) AnimationUtils.loadAnimation(XmlViewAnimationActivity.this, R.anim.animation_set);
mIvImg.startAnimation(animationSet);
- 首标签必须是set标签,set标签可以包含set标签
- android:interpolator:动画插值器。参考Interpolator插值器
- android:shareInterpolator:是否和自己子标签共享插值器
- 其他的属性都在前面讲了,参考前面
Java代码方式创建动画集合
用XML文件的形式创建会提供设置AnimationSet相应的属性,同样Java代码也提供了相应的方法去设置
public void startAnimationSet() {
//创建动画,参数表示他的子动画是否共用一个插值器
AnimationSet animationSet = new AnimationSet(true);
//添加动画
animationSet.addAnimation(new AlphaAnimation(1.0f, 0.0f));
//设置插值器
animationSet.setInterpolator(new LinearInterpolator());
//设置动画持续时长
animationSet.setDuration(3000);
//设置动画结束之后是否保持动画的目标状态
animationSet.setFillAfter(true);
//设置动画结束之后是否保持动画开始时的状态
animationSet.setFillBefore(false);
//设置重复模式
animationSet.setRepeatMode(AnimationSet.REVERSE);
//设置重复次数
animationSet.setRepeatCount(AnimationSet.INFINITE);
//设置动画延时时间
animationSet.setStartOffset(2000);
//取消动画
animationSet.cancel();
//释放资源
animationSet.reset();
//开始动画
mIvImg.startAnimation(animationSet);
}