2.视图动画
文章目录
2.1 视图动画标签
2.1.1 概述
Android 的视图动画由5种类型组成:
- alpha:渐变透明度动画效果。
- scale:渐变尺寸伸缩动画效果。
- translate:画面变换位置移动动画效果。
- rotate:画面转移旋转动画效果。
- set:定义动画集。
举例:
scale标签定义XML 动画文件:
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.0" android:toXScale="1.4" android:fromYScale="0.0" android:toYScale="1.4" android:duration="700"/>
存放位置:
res/anim
;访问方式:R.anim.XXX
也可以存放在:
res/drawable
;访问方式:R.drawable.XXX
2.1.2 scale标签
- fomXScale:动画起始时,控件在X轴方向上相对自身的缩放比例,浮点值。
- toXScale:动画结束时,控件在X 轴方向上相对自身的缩放比例,浮点值。
- fomYScale:动画起始时,控件在Y 轴方向上相对自身的缩放比例,浮点值。
- toYScale : 动画结束时,控件在Y 轴方向上相对自身的缩放比例,浮点值。
- pivotX:缩放起始点X轴座标,可以是数值、百分数、百分数p三种样式。
- pivotY:缩放起始点Y 轴座标。
2.1.3 Animation继承属性
- duration :用于设置完成一次动画的持续时间,以毫秒为单位。
- fillAfter:如果设置为true,则控件动画结束时,将保持动画结束时的状态。
- fillBefore:如果设置为true,则控件动画结束时,将还原到初始化状态。
- fillEnable:与fillBefore效果相同。
- repeatCount:用于指定动画的重复次数,当取值为infinite时,表示无限循环。
- repeatMode :用于设定重复的类型,有reverse 和restart 两个值。需与repeatCount一起使用。
- interpolator:用于设定插值器,其实就是指定的动画效果,比如弹跳效果等。
2.1.4 alpha 标签
- fomAlpha:动画开始时的透明度,取值范围为0.0~1 .0,0.0表示全透明,1.0表示完全不透明。
- toAlpha:动画结束时的透明度,取值范围为0.0~ 1.0,0.0表示全透明,1.0表示完全不透明。
2.1.5 rotate 标签
- fromDegrees :动画开始旋转时的角度位置,正值代表顺时针方向的度数,负值代表逆时针方向的度数。
- toDegrees :动画结束时旋转到的角度位置,正值代表顺时针方向的度数,负值代表逆时针方向的度数。
- pivotX: 旋转中心点X轴座标,默认旋转中心点是控件座标原点。可以是数值、百分数、百分数p 三种样式。
- pivotY: 旋转中心点Y轴座标。
2.1.6 translate 标签
- fromXDelta:起始点X轴座标,可以是数值、百分数、百分数p三种样式。
- fromYDelta:起始点Y轴座标。
- toXDelta:终点X轴座标。
- toYDelta:终点Y轴座标。
2.1.7 set 标签
- set标签是一个容器类标签,用于定义动画集。
- 注意:在set标签中设置repeateCount属性是无效的,必须对每个动画单独设直才有作用。
2.2 视图动画的代码实现
2.2.1 概述
- 标签与所对应的类如下表所示:
标签 | 类 |
---|---|
scale | ScaleAnimation |
alpha | AlphaAnimation |
rotate | RotateAnimation |
translate | TranslateAnimation |
set | AnimationSet |
- Animation类里的标签属性与方法的对应关系:
标签属性 | 方法 |
---|---|
duration | setDuration(long) |
fiIIAfter | setFillAfter(boolean) |
fillBefore | setFillBefore(boolean) |
fillEnabled | setFillEnabled(boolean) |
repeatCount | setRepeatCount(int) |
repeatMode | setRepeatMode(int) |
interpolator | setlnterpolator(lnterpolator) |
2.2.2 ScaleAnimation
ScaleAnimation(Context context , AttributeSet attrs) ScaleAnimation(float fromX , float toX, float fromY , float toY) ScaleAnimation(float fromX , float toX , float fromY , float toY , float pivotX, float pivotY) ScaleAnimation(float fromX , float toX, float fromY , float toY, intpivotXType, float pivotXValue , int pivotYType , float pivotYValue)
pivotXType:
- Animation.ABSOLUTE
- Animation.RELATIVE_TO_SELF
- Animation.RELATIVE_TO_PARENT
2.2.3 AlphaAnimation
AlphaAnimation(Context context , AttributeSet attrs) AlphaAnimation(float fromAlpha , float toAlpha)
2.2.4 RotateAnimation
RotateAnimation(Context context , AttributeSet attrs) RotateAnimation(float fromDegrees , float toDegrees) RotateAnimation(float fromDegrees , float toDegrees , float pivotX , float pivotY) RotateAnimation(float fromDegrees , float toDegrees , int pivotXType , float pivotXValue , int pivotYType , float pivotYValue)
2.2.5 TranslateAnimation
TranslateAnimation(Context context , AttributeSet attrs) TranslateAnimation(float fromXDelta , float toXDelta , float fromYDelta , float toYDelta) TranslateAnimation(int fromXType , float fromXValue , int toXType , float toXValue , int fromYType , float fromYValue , int toYType , float toYValue)
2.2.6 AnimationSet
AnimationSet(Context context , AttributeSet attrs) AnimationSet(boolean shareInterpolator)
shareInterpolator参数的取值有两个: true 和false 。
- true:用于在AnimationSet类中定义一个插值器,其下面的所有动画共用该插值器。
- false:表示其下面的动画定义各自的插值器。
增加动画的函数:
public void addAnimation(Animation a)
2.2.7 Animation
一些函数:
void cancel() //取消动画。 void reset() //将控件重置到动画开始前状态。 void setAnimationListener(Animation.AnimationListener listener)//设置动画监昕 //Animation.AnimationListener中的回调函数: abstract void onAnimationEnd(Animation animation) abstract void onAnimationRepeat(Animation animation) abstract void onAnimationStart(Animation animation)
2.3 差值器简介
有关动画的变化速率的问题是由Interpolator类(插值器)来决定的。
Interpolator是一个接口,通过实现这个接口就可以自定义动画的变化速率。
系统提供了如几个己经实现了插值器的类:
Interpolator class Resource ID AccelerateDecelerateinterpolator @android:anim/accelerate_decelerate_interpolator AccelrateInterpolator @android:anim/accelerate_interpolator AnticipateInterpolator @android:anim/anticipate_interpolator AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator BounceInterpolator @android:anim/bounce_interpolator CycleInterpolator @android:anim/cycle_interpolator DecelerateInterpolator @android:anim/decelerate_interpolator LinearInterpolator @android:anim/linear_interpolator OvershootInterpolator @android:anim/overshoot_interpolator 在XML文件中引用差值器:
android:interpolator="@android:anim/accelerate_interpolator"
通过setlnterpolator()函数设置插值器。
2.3.1 差值器说明
AccelerateDeceleratelnterpolator:加速减速插值器。
Acceleratelnterpolator:加速插值器。
Deceleratelnterpolator:减速插值器。
Linearlnterpolator:线性插值器,也称匀速加速器。
Bouncelnterpolator:弹跳插值器。
Anticipatelnterpolator:初始偏移插值器。
/** * 张力值:默认值为2,值越大,初始的偏移量越大,而且速度越快。 */ //XML属性 android:tension //函数 public Anticipateinterpolator(float tension)
Overshootlnterpolator:结束偏移插值器。
/** * 张力值:默认值为2,值越大,结束的偏移量越大,而且速度越快。 */ //XML属性 android:tension //函数 public Overshootinterpolator(float tension)
AnticipateOvershootlnterpolator:Anticipatelnterpolator与Overshootlnterpolator的合体,即在动画开始时向前偏移一段距离,在动画结束时向后偏移一段距离。
/** * tension张力值:默认值为2,值越大,起始和结束时的偏移量越大,而且速度越快。 * extraTension额外张力值:默认值为1.5。 * T = tension * extraTension */ //XML属性 android:tension android:extraTension //函数 public AnticipateOvershootlnterpolator(float tension) public AnticipateOvershootlnterpolator(float tension, float extraTension)
Cyclelnterpolator:是循环插值器,表示动画循环播放特定的次数,速率沿正弦曲线改变。
public Cycleinterpolator(float cycles)
技巧:
//延迟各个动画的开始时间,使各个动画可以相互间隔。 Animation.setStartOffset(int time);
2.4 逐帧动画
- 谓逐帧动画(Frame Animation),就是一帧接着一帧地播放图片。
2.4.1 XML实现
**1.定义XML 动画文件:**定义在
res/drawable
下<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot=["true" | "false"]> <item android:drawable="@[package:]drawable/drawable_resource_name" android:duration="integer" />
- 元素是必需的,并且必须作为根元素,可以包含一个或多个元素。
- oneshot :true:动画只会执行1次;false:一直循环。
- 元素代表一帧动画,android:drawable指定此帧动画所对应的图片资源,android :duration代表此帧动画持续的时间,是一个整数,单位ms。
2.设置ImageView
- 可以通过android:src实现,也可以通过android :background实现。
3.AnimationDrawable 开始动画
ImageView image = (ImageView)findViewById(R.id.frame_image); AnimationDrawable anim = (AnimationDrawable)image.getDrawable(); anim.start();
- 通过android:src设置动画资源时,取出方式是image.getDrawable()。
- 通过android:background设置动画资源,取出方式是image.getBackground()。
AnimationDrawable 类:
- 逐帧动画需要得到AnimationDrawable类的支持,它是Drawable的间接子类。
- void start():开始播放逐帧动画。
- void stop():停止播放逐帧动画。
- int getDuration(int index):得到指定index的帧的持续时间。
- Drawable getFrame(int index):得到指定index 的帧所对应的Drawable 对象。
- int getNumberOfFrames():得到当前AnimationDrawable的所有帧数量。
- boolean isRunning():判断当前AnimationDrawable是否正在播放。
- void setOneShot(boolean oneShot):设置AnimationDrawable是执行一次还是循环播放。
- boolean isOneShot():判断当前AnimationDrawable执行一状态。
- void addFrame(Drawable frame,int duration):为AnimationDrawable添加1帧,并设置持续时间。
2.4.2 代码实现
举例:
ImageView image = (ImageView)findViewById(R.id.frame_image); final AnimationDrawable anim = new AnimationDrawable(); for(int i=1;i<=14;i++){ int id = getResources().getIdentifier("list_icon_gif_playing"+i,"drawable",getPackageName()); Drawable drawable = getResources().getDrawable(id); anim.addFrame(drawable,60); anim.setOneShot(false); image.setBackgroundDrawable(anim); anim.start();
函数说明:
int getidentifier(String name,String defType,String defPackage)
- String name: 所要查找资源ID的资源名称。
- String defType:资源所在的文件类型。[eg.drawable/string/array]
- String defPackage:应用包名。
参考资料
- 《Android自定义控件开发入门与实战》