android 翻转效果动画源码

本文介绍了一种使用Camera和Matrix实现的翻转动画效果,并提供了具体的代码实现细节。该动画适用于视图之间的翻转过渡,通过调整旋转角度实现平滑转换。

     最近项目上要求做一个翻转的动画效果,由于动画还没有怎么使用过。然后再网上找到一份很实用的翻转动画先用起来,以后再学习下动画相关的。

源码修改后如下:

    public class FlipAnimator extends Animation {

        private Camera camera;

        private float centerX;

        private float centerY;

        public FlipAnimator() {
            setFillAfter(true);
        }


        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            camera = new Camera();
            this.centerX = width / 2;
            this.centerY = height / 2;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            // Angle around the y-axis of the rotation at the given time. It is
            // calculated both in radians and in the equivalent degrees.
            final double radians = Math.PI * interpolatedTime;
            float degrees = (float) (180.0 * radians / Math.PI);

            degrees = -degrees;

            // Once we reach the midpoint in the animation, we need to hide the
            // source view and show the destination view. We also need to change
            // the angle by 180 degrees so that the destination does not come in
            // flipped around. This is the main problem with SDK sample, it does not
            // do this.
            if (interpolatedTime >= 0.5f) {
                degrees += 180.f;
            }

            final Matrix matrix = t.getMatrix();

            camera.save();
            camera.translate(0.0f, 0.0f, (float) (150.0 * Math.sin(radians)));
//            camera.rotateX(degrees);
            camera.rotateY(degrees);
//            camera.rotateZ(degrees);
            camera.getMatrix(matrix);
            camera.restore();

            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
        }
    }

        动画通过Camera 和Matrix实现。文中注视的部分camera.rotateX(degrees)和camera.rotateZ(degrees)分别为X和Z轴的旋转,没有用到故先注释。

        使用方法很简单

                FlipAnimator flipAnimator = new FlipAnimator();
                flipAnimator.setDuration(200);
                startAnimation(flipAnimator);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值