补间动画详解四 平移动画TranslateAnimation

来源:互联网 发布:dbc数据库保存 编辑:程序博客网 时间:2024/05/23 00:05
TranslateAnimation是平移动画的类,负责View的位移。

TranslateAnimation类官方文档:
https://developer.android.com/reference/android/view/animation/TranslateAnimation.html

关于父类Animation的详解可参考文章:
http://blog.csdn.net/ruancoder/article/details/52347243

一、TranslateAnimation的使用
(1).使用xml文件创建TranslateAnimation
属性说明:
android:fromXDelta:动画开始点的X轴坐标。有三种表示方式,一是纯数字,使用绝对位置(比如"50",表示以当前View左上角坐标加50px作为X坐标);二是百分数,相对于控件本身定位(比如"50%",表示以当前View的左上角加上当前View宽度的50%作为X坐标);三是百分数p,相对于父控件定位(比如"50%p",表示以当前View的左上角加上父控件宽度的50%做为X坐标)。
android:fromYDelta:动画开始点的Y轴坐标。
android:toXDelta:动画结束点的X轴坐标。
android:toYDelta:动画结束点的Y轴坐标。

示例代码:

从屏幕底部进入的动画。

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="3000">    <translate        android:fromYDelta="100%p"        android:toYDelta="0.0"/></set>


从屏幕左侧退出的动画。

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"     android:duration="3000">    <translate        android:fromXDelta="0.0"        android:toXDelta="-100%p"/></set>

(2).使用java代码创建TranslateAnimation
示例代码:
//从当前位置,向下和向右各平移300pxTranslateAnimation animation = new TranslateAnimation(0.0f, 300.0f, 0.0f, 300.0f);animation.setDuration(3000);view.startAnimation(animation);

// 从屏幕底部进入的动画TranslateAnimation animation = new TranslateAnimation(        Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,        Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f);animation.setDuration(3000);view.startAnimation(animation);

二、TranslateAnimation的分析
TranslateAnimation继承自Animation,除了拥有父类的属性外,添加了mFromXType、mToXType、mFromYType、mToYType和mFromXValue、mToXValue、mFromYValue、mToYValue八个属性。这个八个属性可以通过构造方法或xml属性传入,最终由mFromXDelta、mToXDelta、mFromYDelta和mToYDelta参与动画的计算。
public class TranslateAnimation extends Animation {    private int mFromXType = ABSOLUTE;    private int mToXType = ABSOLUTE;    private int mFromYType = ABSOLUTE;    private int mToYType = ABSOLUTE;    protected float mFromXValue = 0.0f;    protected float mToXValue = 0.0f;    protected float mFromYValue = 0.0f;    protected float mToYValue = 0.0f;    protected float mFromXDelta;    protected float mToXDelta;    protected float mFromYDelta;    protected float mToYDelta;}

使用java代码的方式创建TranslateAnimation,传入六个参数,fromXType、fromXValue、toXType、toXValue和fromYType、fromYValue、toYType、toYValue,使用如下构造方法。
参数说明:
fromXType:动画开始前的X坐标类型。取值范围为ABSOLUTE(绝对位置)、RELATIVE_TO_SELF(以自身宽或高为参考)、RELATIVE_TO_PARENT(以父控件宽或高为参考)。
fromXValue:动画开始前的X坐标值。当对应的Type为ABSOLUTE时,表示绝对位置;否则表示相对位置,1.0表示100%。
toXType:动画结束后的X坐标类型。
toXValue:动画结束后的X坐标值。
fromYType:动画开始前的Y坐标类型。
fromYValue:动画开始前的Y坐标值。
toYType:动画结束后的Y坐标类型。
toYValue:动画结束后的Y坐标值。
public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,                          int fromYType, float fromYValue, int toYType, float toYValue) {    mFromXValue = fromXValue;    mToXValue = toXValue;    mFromYValue = fromYValue;    mToYValue = toYValue;    mFromXType = fromXType;    mToXType = toXType;    mFromYType = fromYType;    mToYType = toYType;}

使用java代码的方式创建TranslateAnimation,传入四个参数,fromXDelta、toXDelta、fromYDelta和toYDelta,使用如下构造方法。
此时mFromXType、mToXType、mFromYType和mToYType默认为ABSOLUTE。
参数说明:
fromXDelta:动画开始前,离当前View X坐标上的距离。
toXDelta:动画结束后,离当前View X坐标上的距离。
fromYDelta:动画开始前,离当前View Y坐标上的距离。
toYDelta:动画结束后,离当前View Y坐标上的距离。
public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {    mFromXValue = fromXDelta;    mToXValue = toXDelta;    mFromYValue = fromYDelta;    mToYValue = toYDelta;    mFromXType = ABSOLUTE;    mToXType = ABSOLUTE;    mFromYType = ABSOLUTE;    mToYType = ABSOLUTE;}

当使用xml文件的方式创建TranslateAnimation时,由AnimationUtils工具类加载动画文件,使用如下构造方法,通过xml中的属性来获取值。
public TranslateAnimation(Context context, AttributeSet attrs) {    super(context, attrs);    TypedArray a = context.obtainStyledAttributes(attrs,            com.android.internal.R.styleable.TranslateAnimation);    Animation.Description d = Animation.Description.parseValue(a.peekValue(            com.android.internal.R.styleable.TranslateAnimation_fromXDelta));    mFromXType = d.type;    mFromXValue = d.value;    d = Animation.Description.parseValue(a.peekValue(            com.android.internal.R.styleable.TranslateAnimation_toXDelta));    mToXType = d.type;    mToXValue = d.value;    d = Animation.Description.parseValue(a.peekValue(            com.android.internal.R.styleable.TranslateAnimation_fromYDelta));    mFromYType = d.type;    mFromYValue = d.value;    d = Animation.Description.parseValue(a.peekValue(            com.android.internal.R.styleable.TranslateAnimation_toYDelta));    mToYType = d.type;    mToYValue = d.value;    a.recycle();}

完成成员变量的初始化后,接下来进入动画的计算。核心在于重写父类Animation的initialize()和applyTransformation()方法。

initialize()方法中,根据上面传入的八个属性值,结合当前View和父View的宽高,计算出平移的绝对位置。
@Overridepublic void initialize(int width, int height, int parentWidth, int parentHeight) {    super.initialize(width, height, parentWidth, parentHeight);    mFromXDelta = resolveSize(mFromXType, mFromXValue, width, parentWidth);    mToXDelta = resolveSize(mToXType, mToXValue, width, parentWidth);    mFromYDelta = resolveSize(mFromYType, mFromYValue, height, parentHeight);    mToYDelta = resolveSize(mToYType, mToYValue, height, parentHeight);}protected float resolveSize(int type, float value, int size, int parentSize) {    switch (type) {        case ABSOLUTE:            return value;        case RELATIVE_TO_SELF:            return size * value;        case RELATIVE_TO_PARENT:            return parentSize * value;        default:            return value;    }}

applyTransformation()方法负责动画的执行。在动画从开始倒结束的过程中,参数interpolatedTime从0.0递增到1.0。通过不断的调整dx和dy的值,调用Matrix的setTranslate()方法,达到平移View的效果。
@Overrideprotected void applyTransformation(float interpolatedTime, Transformation t) {    float dx = mFromXDelta;    float dy = mFromYDelta;    if (mFromXDelta != mToXDelta) {        dx = mFromXDelta + ((mToXDelta - mFromXDelta) * interpolatedTime);    }    if (mFromYDelta != mToYDelta) {        dy = mFromYDelta + ((mToYDelta - mFromYDelta) * interpolatedTime);    }    t.getMatrix().setTranslate(dx, dy);}

0 0
原创粉丝点击