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 上的位置,然后传到下一个界面,计算一下偏移量。

这里面要注意,里面的 weibo 等 icon,我用的并不是图片,用的是FontAwesome,具体用法看下面博客:
中文地址:传送门
项目网站:传送门

demo 代码地址:传送门

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于谷歌官方提供的3D翻转示例进行修改,修复了在不同设备上显示效果差异过大的问题。项目地址:https://github.com/GcsSloop/Rotate3dAnimation 效果图:如何使用:// 计算中心点(这里是使用view的中心作为旋转的中心点)         final float centerX = view.getWidth() / 2.0f;                 final float centerY = view.getHeight() / 2.0f;        //括号内参数分别为(上下文,开始角度,结束角度,x轴中心点,y轴中心点,深度,是否扭曲)         final Rotate3dAnimation rotation = new Rotate3dAnimation(this, start, end, centerX, centerY, 1.0f, true);         rotation.setDuration(1500);                               //设置动画时长         rotation.setFillAfter(true);                              //保持旋转后效果         rotation.setInterpolator(new AccelerateInterpolator());   //设置插值器         rotation.setAnimationListener(new AnimationListener() {   //设置监听器             @Override             public void onAnimationStart(Animation animation) {             }            @Override             public void onAnimationRepeat(Animation animation) {             }            @Override             public void onAnimationEnd(Animation animation) {             }         });         view.startAnimation(rotation);                            //开始动画

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值