android 翻转效果动画源码

来源:互联网 发布:数据库增删改查 编辑:程序博客网 时间:2024/05/18 00:56

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

源码修改后如下:

    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);



0 0
原创粉丝点击