framework 之 Animation (一)

2014-11-24 11:24:38 · 作者: · 浏览: 19

一、前言:

Animation是OS 4.0以前就有的一个动画框架,源代码位于SDK / android / view / animation下,此目录下还有自带实现的几种动画类,和一些Interpolater(插补器),不过,它只实现了基础的动画效果,见UML图。此类是对整个View做了动画效果,但是效率较低,因此在OS4.0 之后,又添加了新的动画框架:Animator。本篇文章只讲Animation,以后会写篇关于Animator的文章。

二、UML图:

\

图中,显示系统提供了四种动画:透明、旋转、缩放和位移。

三、详细讲解抽象类Animation:

3.1 如何使用Animation

使用它有两种方法:

1. 写一个xml文件,里面定义了一些animation效果,可以是一个效果,也可以是一组(AnimationSet);

2. 继承Animation,自己写个,或者使用系统提供的那四种;

因此,Animation初始化也对应两种方法:

[java]
/**
* Creates a new animation with a duration of 0ms, the default interpolator, with
* fillBefore set to true and fillAfter set to false
*/
public Animation() {
ensureInterpolator();
}

/**
* Creates a new animation whose parameters come from the specified context and
* attributes set.
*
* @param context the application environment
* @param attrs the set of attributes holding the animation parameters
*/
public Animation(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animation);

setDuration((long) a.getInt(com.android.internal.R.styleable.Animation_duration, 0));
setStartOffset((long) a.getInt(com.android.internal.R.styleable.Animation_startOffset, 0));

setFillEnabled(a.getBoolean(com.android.internal.R.styleable.Animation_fillEnabled, mFillEnabled));
setFillBefore(a.getBoolean(com.android.internal.R.styleable.Animation_fillBefore, mFillBefore));
setFillAfter(a.getBoolean(com.android.internal.R.styleable.Animation_fillAfter, mFillAfter));

setRepeatCount(a.getInt(com.android.internal.R.styleable.Animation_repeatCount, mRepeatCount));
setRepeatMode(a.getInt(com.android.internal.R.styleable.Animation_repeatMode, RESTART));

setZAdjustment(a.getInt(com.android.internal.R.styleable.Animation_zAdjustment, ZORDER_NORMAL));

setBackgroundColor(a.getInt(com.android.internal.R.styleable.Animation_background, 0));

setDetachWallpaper(a.getBoolean(com.android.internal.R.styleable.Animation_detachWallpaper, false));

final int resID = a.getResourceId(com.android.internal.R.styleable.Animation_interpolator, 0);

a.recycle();

if (resID > 0) {
setInterpolator(context, resID);
}

ensureInterpolator();
}

/**
* Creates a new animation with a duration of 0ms, the default interpolator, with
* fillBefore set to true and fillAfter set to false
*/
public Animation() {
ensureInterpolator();
}

/**
* Creates a new animation whose parameters come from the specified context and
* attributes set.
*
* @param context the application environment
* @param attrs the set of attributes holding the animation parameters
*/
public Animation(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animation);

setDuration((long) a.getInt(com.android.internal.R.styleable.Animation_duration, 0));
setStartOffset((long) a.getInt(com.android.internal.R.styleable.Animation_startOffset, 0));

setFillEnabled(a.getBoolean(com.android.internal.R.styleable.Animation_fillEnabled, mFillEnabled));
setFillBefore(a.getBoolean(com.and