//创建自定义动画
//创建Animation的子类
public class MyAnimation extends Animation {
int mWidth;
int mHeight;
private Camera mCamera;
//有必要的情况下,需要复写initialize()
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mWidth=width;
mHeight=height;
setDuration(500);
setFillAfter(true);//为真,则保持最后的显示效果
//设置默认插值器 效果是0到1;
BounceInterpolator bounceInterpolator = new BounceInterpolator();
setInterpolator(bounceInterpolator);
mCamera = new Camera();
}
//核心方法applyTransformation(),interpolatedTime是插值器,Transformation 通过其矩阵来实现动画
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// super.applyTransformation(interpolatedTime, t);父类的applyTransformation是空实现,其四个子类才有具体的实现
Matrix matrix = t.getMatrix();
/* //通过修改matrix来实现电视机关闭的效果
matrix.preScale(1,
1-interpolatedTime,
mWidth/2,mHeight/2);*/
//Camera是3D轴修改的封装后的类,作用是修改 3D坐标轴
mCamera.save();
mCamera.rotateY(360*interpolatedTime);
mCamera.getMatrix(matrix);
mCamera.restore();//将修改后的效果作用到matrix上
//开始和结束时候的矩阵
// matrix.preTranslate(mWidth/2,mHeight/2);
matrix.postTranslate(mWidth/2,mHeight/2);
}
}
自定义Animation
最新推荐文章于 2024-08-05 22:41:18 发布