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

2019-02-28  本文已影响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();
上一篇下一篇

猜你喜欢

热点阅读