自定义Animation

来源:互联网 发布:吴德周 知乎 编辑:程序博客网 时间:2024/06/11 07:32
//创建自定义动画//创建Animation的子类public class MyAnimation extends Animation {int mWidth;int mHeight;private Camera mCamera;//有必要的情况下,需要复写initialize()@Overridepublic 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 通过其矩阵来实现动画    @Overrideprotected 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);}}
原创粉丝点击