Android群英传——第七章自定义动画

时间:2023-02-10 13:58:05

关于Matrix请看博客:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class CustomTV extends Animation {

private int mCenterWidth;
private int mCenterHeight;
private Camera mCamera = new Camera();
private float mRotateY = 0.0f;

// 动画的初始化操作
@Override
public void initialize(int width,
int height,
int parentWidth,
int parentHeight) {

super.initialize(width, height, parentWidth, parentHeight);
// 设置默认时长
setDuration(1000);
// 动画结束后保留状态
setFillAfter(true);
// 设置默认插值器
setInterpolator(new AccelerateInterpolator());
mCenterWidth = width / 2;
mCenterHeight = height / 2;
}

// 暴露接口-设置旋转角度
public void setRotateY(float rotateY) {
mRotateY = rotateY;
}


/**
* 动画实现的主要逻辑
* @param interpolatedTime 差值器的时间因子,范围为0 - 1,由动画当前完成的百分比和当前时间所对应的插值所计算得来的
* @param t 矩阵封装类,使用该类获得当前的矩阵对象,关于Matrix请看:http://www.cnblogs.com/qiengo/archive/2012/06/30/2570874.html#translate
*/

@Override
protected void applyTransformation(
float interpolatedTime,
Transformation t) {
final Matrix matrix = t.getMatrix();
matrix.preScale(1,
1 - interpolatedTime,
mCenterWidth,
mCenterHeight);
}
}