Android动画——补间动画(Tween Animation)

来源:互联网 发布:摩拜魔方大数据未来 编辑:程序博客网 时间:2024/05/22 17:33

补间动画是通过对View的内容进行一系列的图形变换 (包括平移、缩放、旋转、改变透明度)来实现动画效果。动画效果的定义可以采用XML或者编码来实现。

动画类型 XML配置方式 Java代码实现方式 渐变透明度动画效果 alpha AlphaAnimation 渐变尺寸缩放动画效果 scale ScaleAnimation 画面旋转动画效果 rotate RotateAnimation 画面位置移动动画效果 translate TranslateAnimation 组合动画效果 set AnimationSet

(1)XML实现:
渐变透明度动画效果是使用标签实现,它有如下属性

fromAlpha:开始时透明度toAlpha: 结束时透明度duration:动画持续时间fillAfter:设置动画结束后保持当前的位置

在res目录下的anim文件夹下新建文件anim_alpha.xml

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fillAfter="false"    android:fromAlpha="1.0"    android:toAlpha="0.0" />

XML方式加载方式通过AnimationUtils.loadAnimation(this, R.anim.anim_alpha)获取Animation

Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);imageView.startAnimation(alphaAnimation);

渐变尺寸缩放动画效果是使用标签实现,它有如下属性:
fromXDelta,fromYDelta: 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480
toXDelta, toYDelta :动画结束时X,Y的座标
interpolator: 指定动画插入器
fromXScale,fromYScale: 动画开始前X,Y的缩放,0.0为不显示, 1.0为正常大小
toXScale,toYScale:动画最终缩放的倍数, 1.0为正常大小,大于1.0放大
pivotX, pivotY :动画起始位置,相对于屏幕的百分比,两个都为50%表示动画从自身中间开始
startOffset:动画多次执行的间隔时间,如果只执行一次,执行前会暂停这段时间,单位毫秒
duratio:一次动画效果消耗的时间,单位毫秒,值越小动画速度越快
repeatCount:动画重复的计数,动画将会执行该值+1次
repeatMode:动画重复的模式,reverse为反向,当第偶次执行时,动画方向会相反。restart为重新执行,方向不变
在res目录下的anim文件夹下新建文件anim_scale.xml

<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromXScale="0.0"    android:fromYScale="0.0"    android:interpolator="@android:anim/decelerate_interpolator"    android:pivotX="50%"    android:pivotY="50%"    android:repeatCount="1"    android:repeatMode="reverse"    android:startOffset="0"    android:toXScale="1.5"    android:toYScale="1.5" />

画面旋转动画效果是使用标签实现,它有如下属性:

fromDegrees 动画开始时的角度toDegrees 动画结束时物件的旋转角度,正代表顺时针pivotX 属性为动画相对于物件的X坐标的开始位置pivotY 属性为动画相对于物件的Y坐标的开始位置

在res目录下的anim文件夹下新建文件anim_rotate.xml

<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromDegrees="0"    android:interpolator="@android:anim/accelerate_decelerate_interpolator"    android:pivotX="50%"    android:pivotY="50%"    android:toDegrees="-360" />

画面位置移动动画效果是使用标签实现,它有如下属性:
fromXDelta,fromYDelta 起始时X,Y座标,屏幕右下角的座标是X:320,Y:480
toXDelta, toYDelta 动画结束时X,Y的座标
在res目录下的anim文件夹下新建文件anim_translate.xml

<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="1000"    android:fromXDelta="200"    android:fromYDelta="0"    android:interpolator="@android:anim/cycle_interpolator"    android:toXDelta="0"    android:toYDelta="0" />

组合动画效果是使用标签实现,在res目录下的anim文件夹下新建文件anim_set.xml:

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <alpha        android:duration="1000"        android:fromAlpha="1.0"        android:toAlpha="0.0" />    <scale        android:duration="1000"        android:fromXScale="0.0"        android:fromYScale="0.0"        android:interpolator="@android:anim/decelerate_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:repeatCount="1"        android:repeatMode="reverse"        android:startOffset="0"        android:toXScale="1.5"        android:toYScale="1.5" /></set>

(2)JAVA代码实现

  Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);  //设置动画持续时间为1000毫秒  alphaAnimation.setDuration(1000);  //设置动画结束后保持当前的位置(即不返回到动画开始前的位置)  alphaAnimation.setFillAfter(false);  imageView.startAnimation(alphaAnimation);
Animation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//设置动画持续时间为1000毫秒scaleAnimation.setDuration(1000);//如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态scaleAnimation.setFillAfter(true);//如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态scaleAnimation.setFillBefore(false);//设置动画循环次数scaleAnimation.setRepeatCount(3);scaleAnimation.setRepeatMode(Animation.REVERSE);scaleAnimation.setStartOffset(0);scaleAnimation.setInterpolator(this, //设置动画插入器android.R.anim.decelerate_interpolator);imageView.startAnimation(scaleAnimation);
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);rotateAnimation.setDuration(1000);rotateAnimation.setFillAfter(true);//设置动画插入器rotateAnimation.setInterpolator(this, android.R.anim.accelerate_decelerate_interpolator);imageView.startAnimation(rotateAnimation);
Animation translateAnimation = new TranslateAnimation(0, 100, 0, 0);translateAnimation.setDuration(500);//设置动画插入器translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//设置动画结束后保持当前的位置(即不返回到动画开始前的位置)translateAnimation.setFillAfter(true);imageView.startAnimation(translateAnimation);
AnimationSet animationSet = new AnimationSet(true);Animation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);//设置动画持续时间为1000毫秒alphaAnimation.setDuration(1000);Animation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//设置动画持续时间为1000毫秒scaleAnimation.setDuration(1000);scaleAnimation.setRepeatMode(Animation.REVERSE);scaleAnimation.setStartOffset(0);//设置动画插入器scaleAnimation.setInterpolator(this, android.R.anim.decelerate_interpolator);animationSet.addAnimation(alphaAnimation);animationSet.addAnimation(scaleAnimation);imageView.startAnimation(animationSet);

动画监听器Animation.AnimationListener:
有时可能我们要在动画的每个周期里面做不同的操作,这时候就要借助动画监听器了

alphaAnimation.setAnimationListener(new Animation.AnimationListener() {    @Override    public void onAnimationStart(Animation animation) {        //动画开始时调用    }    @Override    public void onAnimationEnd(Animation animation) {        //动画结束时调用    }    @Override    public void onAnimationRepeat(Animation animation) {        //动画重复时调用    }    });

附:几种自带的动画插入器:
AccelerateInterpolator 加速,开始时慢中间加速
DecelerateInterpolator 减速,开始时快然后减速
AccelerateDecelerateInterolator 先加速后减速,开始结束时慢,中间加速
AnticipateInterpolator 反向,先向相反方向改变一段再加速播放
AnticipateOvershootInterpolator 反向加超越,先向相反方向改变,再加速播放,会超出目的值然后缓慢移动至目的值
BounceInterpolator 跳跃,快到目的值时值会跳跃,如目的值100,后面的值可能依次为85,77,70,80,90,100
CycleIinterpolator 循环,动画循环一定次数,值的改变为一正弦函数:Math.sin(2* mCycles* Math.PI* input)
LinearInterpolator 线性,线性均匀改变
OvershootInterpolator超越,最后超出目的值然后缓慢改变到目的值

1 0
原创粉丝点击