Android的Tween动画详解

jopen 10年前

一个Tween动画将对于View对象的内容进行一系列简单的转换,在animation提供了所以关于Tween动画的类,主要有四个常用的类,AlphaAnimation(透明度渐变),RotateAnimation(旋转动画),ScaleAnimation(图片缩放动画),TranslateAnimation(移动动画),AnimationSet(一个动画的集合类),以下是对常用动画特效类的构造方法的作用和参数进行讲解

(1) AlphaAnimation

public AlphaAnimation(float fromAlpha, float toAlpha)

fromAlpha -  开始时候的透明度,其中1表示完全不透明,0表示完全透明的 

toAlpha  结束时候的透明度

setDuration(long durationMillis)  设置动画执行的时间

setFillAfter(boolean fillAfter)  设置为true时,动画停在执行完后的效果,默认是执行完动画回到刚开始的效果

setRepeatCount(int repeatCount) 设置动画重复次数,repeatCount默认为0,即执行一次,为1时,即执行2次

setRepeatMode(int repeatMode)  设置动画重复的模式,有Animation.REVERSE和Animation.RESTART两种方式,默认为 Animation.RESTART,Animation.RESTART的意思就是说比如你设置重复次数为1,当执行完第一次动画之后,回到动画开始然后执行第二次动画,而你设置Animation.REVERSE时候,比如你动画是从不透明----->透明,执行完第一次动画的时候,变为不透明,然后执行第二次动画,他就从不透明到透明,不知道大家理解我的意思了没?

我就介绍几个常用的方法,其他的动画也有上面的那些方法,然后等下介绍setInterpolator(Interpolator)方法

(2) RotateAnimation

public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

 fromDegrees  动画开始的角度

toDegrees  动画结束的角度

pivotXValue, pivotYValue  绕着旋转的中心点的X坐标和Y坐标

pivotXType, pivotYType 旋转中心点的的相对关系类型,有三种animation.absolute,animation.relative_to_self,或 animation.relative_to_parent,animation.absolute绝对坐标类型,也就是相对O点的位置,animation.relative_to_self相对自己,自己视图的左上角那个点为O点位置, animation.relative_to_parent相对父视图左上角那个点为O点位置,即自己View所在的ViewGroup的位置

(3) ScaleAnimation

public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 

float fromX, float toX  X轴方向从开始的大小到结束的大小

float fromY, float toY  Y轴方向从开始的大小到结束的大小

pivotXValue, pivotYValue  绕着缩放的中心点的X坐标和Y坐标

pivotXType, pivotYType    缩放中心点的的相对关系类型,有三种animation.absolute,animation.relative_to_self,或 animation.relative_to_parent,animation.absolute绝对坐标类型,也就是相对O点的位置,animation.relative_to_self相对自己,自己视图的左上角那个点为O点位置, animation.relative_to_parent相对父视图左上角那个点为O点位置,即自己View所在的ViewGroup的位置

(4) TranslateAnimation

public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) 

fromXType  X轴上开始点相对类型

fromXValue  开始点的值

toXType    X轴上结束点相对类型

toXValue,  结束点的值

Y轴同理

(5) AnimationSet

这是一个动画的集合类,可以设置多个动画一起执行,比较简单,我就不多介绍了

interpolator的解释

interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等。

Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:

AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator 在动画的以均匀的速率改变
上面介绍的是通过代码构造的动画,当然我们也能通过XML文件写动画,个人推荐使用XML文件

动画放在res下的anim下,下面我来介绍用代码生成和XML文件的方式

AlphaAnimation 代码实现

    //构造透明变化动画                    Animation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);                    //设置动画执行时间                    alphaAnimation.setDuration(2000);                    //设置动画重复方式                    alphaAnimation.setRepeatMode(Animation.REVERSE);                    //设置动画重复次数                    alphaAnimation.setRepeatCount(5);                    //设置动画变化率                    alphaAnimation.setInterpolator(new AccelerateInterpolator());  
</div> </div> AlphaAnimation 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.0"            android:duration="2000"            android:repeatCount="5"            android:repeatMode="reverse"            android:interpolator="@android:anim/accelerate_interpolator" />  
</div> </div> 通过AnimationUtils.loadAnimation(this, R.anim.alpha)就能拿到动画了


RotateAnimation代码实现

    Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);                    rotateAnimation.setDuration(2000);                    rotateAnimation.setRepeatMode(Animation.RESTART);                    rotateAnimation.setRepeatCount(5);                    rotateAnimation.setInterpolator(new LinearInterpolator());  
</div> </div> RotateAnimation XML实现
    <?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"            android:repeatCount="5"            android:repeatMode="reverse"            android:interpolator="@android:anim/linear_interpolator">        </rotate>  
</div> </div>

值得注意的地方是android:pivotX="50%",android:pivotY="50%"  当相对自己的是要加"%",相对父容器就不要加“%",这里就这个比较重要

 

RotateAnimation 动画可以自定义圆形进度条,给个例子吧,用的是XML文件定义的

<?xml version="1.0" encoding="utf-8"?>    <rotate xmlns:android="http://schemas.android.com/apk/res/android"        android:drawable="@drawable/loading_large6"        android:duration="1000"        android:fromDegrees="0"        android:interpolator="@android:anim/linear_interpolator"        android:pivotX="50%"        android:pivotY="50%"        android:repeatCount="infinite"        android:repeatMode="restart"        android:toDegrees="360" />  
</div> </div> 来自:http://blog.csdn.net/xiaanming/article/details/8997260