美文网首页
一个类搞定红包旋转动画-非帧动画

一个类搞定红包旋转动画-非帧动画

作者: 林锦洲 | 来源:发表于2019-02-28 17:40 被阅读41次
    只需一张图片、一个动画类,按照图片Y轴旋转,动画效果如下: 红包动画
    public class RedAnimation extends Animation {
        private int centerX, centerY;
        private Camera camera = new Camera();
    
        @Override
        public void initialize(int width, int height, int parentWidth,
                               int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            //获取中心点坐标
            centerX = width / 2;
            centerY = height / 2;
            //动画执行时间  自行定义
            setDuration(800);
            setInterpolator(new DecelerateInterpolator());
        }
    
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            final Matrix matrix = t.getMatrix();
            camera.save();
            //中心是绕Y轴旋转  这里可以自行设置X轴 Y轴 Z轴
            camera.rotateY(360 * interpolatedTime);
            //把我们的摄像头加在变换矩阵上
            camera.getMatrix(matrix);
            //设置翻转中心点
            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
            camera.restore();
        }
    }
    
    开始动画
    RedAnimation animation = new RedAnimation();
    animation.setRepeatCount(Animation.INFINITE);
    ivOpen.startAnimation(animation);
    
    结束动画
    ivOpen.clearAnimation();
    

    相关文章

      网友评论

          本文标题:一个类搞定红包旋转动画-非帧动画

          本文链接:https://www.haomeiwen.com/subject/updkuqtx.html