Android 翻转动画 Rotate3dAnimation 效果

十一前请了一天假,后面请了三天假,串起来一共休息了十二天,这期间见了女朋友的家长,也领女朋友见了我家长,好消息是各方家长反馈不错,坏消息是涨了五斤肉,毕竟是吃了12天的呼伦贝儿牛羊肉哈。玩了这么多天,回来要收收心好好工作了,毕竟要攒钱娶媳妇哈~


上班回来,按照产品的需求做了这样一个效果,如下图:

这其中包含了3个动画,分别是移动动画、放大动画、旋转动画。

前两个动画比较好完成,可以用 TranslateAnimation 和 ScaleAnimation来完成,但是第三个就比较麻烦了,因为这个旋转动画是根据 Y 轴来旋转的,而 RotateAnimation 是根据垂直屏幕的 Z 轴旋转的,所以 RotateAnimation 并不能够完成我们需要的效果,这里就需要我们去继承 Animation 去自定义动画了。

但是!!!
但是 APIdemo 里面已经实现了这个动画,so,我们拿来用就好了,下面是我贴出来的Rotate3dAnimation,其中我添加了一个构造,添加了相对自己位置的 type。

package cn.kenneth.rotate3danimation;

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

public class Rotate3dAnimation extends Animation {
    private int mCenterXType = ABSOLUTE;
    private int mCenterYType = ABSOLUTE;

    private final float mFromDegrees;
    private final float mToDegrees;
    private float mCenterX;
    private float mCenterY;
    private final float mDepthZ;
    private final boolean mReverse;
    private Camera mCamera;

    /**
 * Creates a new 3D rotation on the Y axis. The rotation is defined by its
 * start angle and its end angle. Both angles are in degrees. The rotation
 * is performed around a center point on the 2D space, definied by a pair
 * of X and Y coordinates, called centerX and centerY. When the animation
 * starts, a translation on the Z axis (depth) is performed. The length
 * of the translation can be specified, as well as whether the translation
 * should be reversed in time.
 *
 * @param fromDegrees the start angle of the 3D rotation
 * @param toDegrees the end angle of the 3D rotation
 * @param centerX the X center of the 3D rotation
 * @param centerY the Y center of the 3D rotation
 * @param reverse true if the translation should be reversed, false otherwise
 */
    public Rotate3dAnimation(float fromDegrees, float toDegrees,
                             float centerX, float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterX = centerX;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;
    }

    public Rotate3dAnimation(float fromDegrees, float toDegrees,
                             int centerXType, float centerX, int centerYType, float centerY, float depthZ, boolean reverse) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;
        mCenterXType = centerXType;
        mCenterX = centerX;
        mCenterYType = centerYType;
        mCenterY = centerY;
        mDepthZ = depthZ;
        mReverse = reverse;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
        mCenterX = resolveSize(mCenterXType, mCenterX, width, parentWidth);
        mCenterY = resolveSize(mCenterYType, mCenterY, height, parentHeight);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final float fromDegrees = mFromDegrees;
        float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

        final float centerX = mCenterX;
        final float centerY = mCenterY;
        final Camera camera = mCamera;

        final Matrix matrix = t.getMatrix();

        camera.save();
        if (mReverse) {
            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
        } else {
            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
        }
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();

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

移动动画也比较好做,点击的时候记录点击 icon 在 window 上的位置,然后传到下一个界面,计算一下偏移量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苦茶子12138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值