A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 陈同英 中级黑马   /  2015-11-28 00:10  /  544 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

一个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文件的方式

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马