Android动画三

一、自定义动画步骤

  继承Animation,通常会重写public void initialize(int width, int height, int parentWidth, int parentHeight)方法,来设置动画的基本属性
  重写protected void applyTransformation(float interpolatedTime, Transformation t),来实现动画
  interpolatedTime表示插值器的时间因子,取值范围0-1
  t一个矩阵的封装类,实现动画就是利用矩阵来计算的

二、示例

  关于矩阵的前乘后乘等概念,在前面矩阵中有解释

2.1 实现关闭效果

public class CustomTV extends Animation{

    private int mCenterWidth;
    private int mCenterHeight;

    @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;
    }


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

  

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                CustomTV custom = new CustomTV();
                v.startAnimation(custom);
                break;

            case R.id.button2:
                CustomAnimation customAnimation = new CustomAnimation();
                customAnimation.setRotateY(30);
                v.startAnimation(customAnimation);
                break;
        }
    }
}

  运行效果

2.2 实现关闭效果

public class CustomAnimation extends Animation{

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

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        setDuration(2000);
        setFillAfter(true);
        setInterpolator(new BounceInterpolator());
        mCenterWidth = width/2;
        mCenterHeight = height/2;
    }


    /**
     * 设置旋转角度
     * @param rotateY
     */
    public void setRotateY(float rotateY){
        mRotateY = rotateY;
    }


    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final Matrix matrix = t.getMatrix();
        mCamera.save();
        // 设置旋转角度
        mCamera.rotateY(mRotateY * interpolatedTime);
        // 将旋转变换作用到matrix上
        mCamera.getMatrix(matrix);
        mCamera.restore();
        // 改变旋转中心
        matrix.preTranslate(mCenterWidth, mCenterHeight);
        matrix.postTranslate(-mCenterWidth, -mCenterHeight);
    }

}

  运行效果

posted @ 2015-12-05 15:03  轻云沉峰  阅读(160)  评论(0)    收藏  举报