立体反转动画

package net.card7.view.anim;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class RotateAnimation extends Animation {

    /** 值为true时可明确查看动画的旋转方向。 */
    public static final boolean DEBUG = false;
    /** 沿Y轴正方向看,数值减1时动画逆时针旋转。 */
    public static final boolean ROTATE_DECREASE = true;
    /** 沿Y轴正方向看,数值减1时动画顺时针旋转。 */
    public static final boolean ROTATE_INCREASE = false;
    /** Z轴上最大深度。 */
    public static final float DEPTH_Z = 310.0f;
    /** 动画显示时长。 */
    public static final long DURATION = 800l;
    /** 图片翻转类型。 */
    private final boolean type;
    private final float centerX;
    private final float centerY;
    private Camera camera;

    public RotateAnimation(float cX, float cY, boolean type) {
        centerX = cX;
        centerY = cY;
        this.type = type;
        // 设置动画时长
        setDuration(DURATION);
    }

    @Override
    public void initialize(int width, int height, int parentWidth,
            int parentHeight) {
        // 在构造函数之后、applyTransformation()之前调用本方法。
        super.initialize(width, height, parentWidth, parentHeight);
        camera = new Camera();
    }

    @Override
    protected void applyTransformation(float interpolatedTime,
            Transformation transformation) {
        // interpolatedTime:动画进度值,范围为0~1,0.5为正好翻转一半
        if (listener != null) {
            listener.interpolatedTime(interpolatedTime);
        }

        float from = 0.0f, to = 0.0f;
        if (type == ROTATE_DECREASE) {
            from = 0.0f;
            to = 180.0f;
        } else if (type == ROTATE_INCREASE) {
            from = 360.0f;
            to = 180.0f;
        }

        // 旋转的角度
        float degree = from + (to - from) * interpolatedTime;
        boolean overHalf = (interpolatedTime > 0.5f);
        if (overHalf) {
            degree = degree - 180;
        }

        // 旋转深度
        float depth = (0.5f - Math.abs(interpolatedTime - 0.5f)) * DEPTH_Z;

        final Matrix matrix = transformation.getMatrix();
        camera.save();
        camera.translate(0.0f, 0.0f, depth);
        camera.rotateY(degree);
        camera.getMatrix(matrix);
        camera.restore();

        if (DEBUG) {
            if (overHalf) {
                matrix.preTranslate(-centerX * 2, -centerY);
                matrix.postTranslate(centerX * 2, centerY);
            }
        } else {
            matrix.preTranslate(-centerX, -centerY);
            matrix.postTranslate(centerX, centerY);
        }
    }

    /** 用于监听动画进度。当值过半时需更新的内容。 */
    private InterpolatedTimeListener listener;

    public void setInterpolatedTimeListener(InterpolatedTimeListener listener) {
        this.listener = listener;
    }

    /** 动画进度监听器。 */
    public static interface InterpolatedTimeListener {
        public void interpolatedTime(float interpolatedTime);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值