Animations可以分为两大类:
第一类:Tweened Animations
该类Animations提供了旋转,移动,伸展和淡出效果
第二类:Frame-by-Frame Animations
这个类Animations可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个一个的显示
Tweened Animations的分类
1.Alpha:淡入淡出效果
2.Scale:缩放效果
3.Rotate:旋转效果
4.Translate:移动效果
使用Tweened Animations的步骤
1.创建一个AnimationSet对象
2.根据需要创建相应的Animation对象
3.根据软件动画的需要,为Animation对象设置相应的数据
4.将Animation对象添加到AnimationSet对象中
5.使用控件对象开始执行AnimationSet
Tween Animations的通用属性
//设置动画执行的时间为1s
animationSet.setDuration(1000);
//如果为true,执行完动画后,停留到执行结束的时候
animationSet.setFillAfter(true);
//如果为true,执行完动画后,停留到执行开始的时候
animationSet.setFillBefore(false);
//执行前停留的时间(毫秒)
animationSet.setStartOffset(1000);
//执行次数
animationSet.setRepeatCount(2);
第一个:alpha,淡入淡出效果
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
AnimationSet animationSet = new AnimationSet(true);
// 创建一个AlphaAnimation对象(参数表示从完全不透明到完全透明)
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
// 设置动画执行的时间(单位:毫秒)
alphaAnimation.setDuration(1000);
// 将AlphaAnimation对象添加到AnimationSet当中
animationSet.addAnimation(alphaAnimation);
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);//mImageView为一个设置的监听器为Imageview的对象
第二个:scale 缩放
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
animationSet = new AnimationSet(true);
// 创建一个ScaleAnimation对象(以某个点为中心缩放)
//参数1:X轴的初始值,参数2:X轴的收缩后的值,参数3:Y轴的初始值,参数4:Y轴
//的缩放后的值,参数5:缩放中心点的X坐标类型,Animation.RELATIVE_TO_SELF
//为相对自己的坐标,Animation.RELATIVE_TO_PARENT相对于控件的坐标
//参数6:X轴的值,参数7,参数8为Y轴的
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1, 0.1f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// 设置动画执行之前等待的时间(单位:毫秒)
scaleAnimation.setStartOffset(1000);
// 设置动画执行的时间(单位:毫秒)
scaleAnimation.setDuration(2000);
// 如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
animationSet.setFillAfter(true);则会停留在动画结束的状态
// 将ScaleAnimation对象添加到AnimationSet当中
animationSet.addAnimation(scaleAnimation);
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);
第三个:translate 移动
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
animationSet = new AnimationSet(true);
// 创建一个RotateAnimation对象(从某个点移动到另一个点)
//参数1~2:X起始坐标,参数3~4:X结束坐标,参数5~6:Y轴起始坐标,参数7~8:Y结束坐标
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1.0f);
// 设置动画执行的时间(单位:毫秒)
translateAnimation.setDuration(1000);
// 将TranslateAnimation对象添加到AnimationSet当中
animationSet.addAnimation(translateAnimation);
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);
第四个:Rotate:旋转效果
// 创建一个AnimationSet对象(AnimationSet是存放多个Animations的集合)
animationSet = new AnimationSet(true);
// 创建一个RotateAnimation对象(以某个点为圆心旋转360度)
//参数1:旋转角度的开始值,参数2:旋转角度的结束值
//参数3~4:确定开始旋转中心X坐标,参数5~6:确定旋转中心Y坐标
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_PARENT, 0.25f);
// 设置动画执行的时间(单位:毫秒)
rotateAnimation.setDuration(5000);
// 将RotateAnimation对象添加到AnimationSet当中
animationSet.addAnimation(rotateAnimation)
// 使用ImageView的startAnimation方法开始执行动画
mImageView.startAnimation(animationSet);
xml中一些通用属性:
android:duration:设置动画持续事件(单位:毫秒)
android:fillAfter:如果fillAfter设为true,则动画执行后,控件将停留在动画结束的状态
android:fillBefore:如果fillBefore设为true,则动画执行后,控件将回到动画开始的状态
android:startOffset(long startOffset):设置动画执行之前等待的时间(单位:毫秒)
android:repeatCount(int repeatCount):设置动画重复的次数
android:interpolator:设置动画的变化速度
android:interpolator="@android:anim/accelerate_decelerate_interpolator":先加速,后减速
android:interpolator="@android:anim/accelerate_interpolator":加速
android:interpolator="@android:anim/decelerate_interpolator":减速
android:interpolator="@android:anim/cycle_Interpolator":动画循环播放特定次数,速率改变沿着正弦曲线
android:interpolator="@android:anim/linear_Interpolator":匀速
第二类:
Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。在XML中的定义方式如下:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各
项显示的时间。XML文件要放在/res/drawable/目录下。示例:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setBackgroundResource(R.drawable.drawable_anim);
anim = (AnimationDrawable) imageView.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
anim.stop();
anim.start();
return true;
}
return super.onTouchEvent(event);
}
要在代码中调用Imageview的setBackgroundResource方法,如果直接在XML布局文件中设置其src属性
当触发动画时会FC。
在动画start()之前要先stop(),不然在第一次动画之后会停在最后一帧,这样动画就只会触发一次。
最后一点是SDK中提到的,不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,
如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。
AnimationSet提供了一个把多个动画组合成一个组合的机制,并可设置组中动画的时序关系,
如同时播放,顺序播放等。
以下例子同时应用5个动画:
播放anim1;
同时播放anim2,anim3,anim4;
播放anim5。
AnimatorSet bouncer = new AnimatorSet();
bouncer.play(anim1).before(anim2);
bouncer.play(anim2).with(anim3);
bouncer.play(anim2).with(anim4)
bouncer.play(anim5).after(amin2);
animatorSet.start();