码迷,mamicode.com
首页 > 移动开发 > 详细

android 细节之 旋转动画

时间:2014-08-27 21:41:38      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:android   blog   os   java   io   for   ar   2014   art   


Flip Animation for Android:


近期项目中用到了一个小动画,让物体实现一定的3D旋转效果,现记录例如以下:


public class FlipAnimation extends Animation {
    private Camera mCamera;

    private View mFromView;
    private View mToView;

    private float mCenterX;
    private float mCenterY;

    private boolean mForward = true;

    /**
     * Creates a 3D flip animation between two views.
     *
     * @param fromView First view in the transition.
     * @param toView Second view in the transition.
     */
    public FlipAnimation(View fromView, View toView) {
        mFromView = fromView;
        mToView = toView;

        setDuration(700);
        setFillAfter(false);
        setInterpolator(new AccelerateDecelerateInterpolator());
    }

    public void reverse() {
        mForward = false;
        View switchView = mToView;
        mToView = mFromView;
        mFromView = switchView;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCenterX = width / 2;
        mCenterY = height / 2;
        mCamera = new Camera();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final double radians = Math.PI * interpolatedTime;
        float degrees = (float)(180.0 * radians / Math.PI);

        if (interpolatedTime >= 0.5f) {
            degrees -= 180.f;
            mFromView.setVisibility(View.GONE);
            mToView.setVisibility(View.VISIBLE);
        }

        if (mForward) {
            degrees = -degrees;
        }

        final Matrix matrix = t.getMatrix();
        mCamera.save();
        mCamera.rotateY(degrees);
        mCamera.getMatrix(matrix);
        mCamera.restore();
        matrix.preTranslate(-mCenterX, -mCenterY);
        matrix.postTranslate(mCenterX, mCenterY);
    }
}


android 细节之 旋转动画

标签:android   blog   os   java   io   for   ar   2014   art   

原文地址:http://www.cnblogs.com/hrhguanli/p/3940252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!