自定义动画-3D旋转效果

来源:互联网 发布:魔域二星副本源码 编辑:程序博客网 时间:2024/06/03 00:17

创建自定义动画非常简单,只需要实现它的applyTransformation的逻辑就可以了,不过正常情况下,还需要覆盖父类的initialize方法来实现一些初始化工作,applyTransformation方法中有2个参数:

 applyTransformation(float interpolatedTime, Transformation t)

第一个参数interpolatedTime,就是前面讲的插值器的时间因子,这个因子是由动画当前完成的百分比和当前时间所对应的差值所计算得来的,取值范围为0到1.0。

第二个参数Transformation 非常简单,它是矩阵的封装类,一般使用这个类获得当前矩阵的对象,代码如下:

final Matrix matrix = t.getMatrix();

通过改变获得的matrix对象,可以将动画效果实现出来,而对于matrix的变换操作,基本可以实现任何效果的动画。

  @Override    protected void applyTransformation(float interpolatedTime, Transformation t) {        final Matrix matrix = t.getMatrix();        matrix.......}

通过模拟电视机关闭的效果来看看简单的矩阵变换,电视变换的效果很简单,让一个图片纵向比例不断缩小就行了。对应的代码:

final Matrix matrix=t.getMatrix();matrix.preScale(1,1-interpolatedTime,mCenterWidth,mCenterHeight);

其中mCenterWidth和mCenterHight即为缩放的中心的,设置为图片中心即可。这样,通过简单的矩阵变换,就可以模拟电视机关闭的动画。

接下来结合矩阵,并使用Camera类来实现一个自定义的3D动画效果。要注意的是,这里的Camera并不是指手机中的相机,而是android.graphic.Camera中的Camera类它封装了openGL的3D动画,从而可以非常方便地创建3D动画效果。把Camera中的Camera想象成一个真实地摄像机,当物体固定在某处时,只要移动摄像机就能拍摄到具有立体感的图像了。因此,可以通过它可以实现各种3D效果。

代码如下:

package animation;import android.graphics.Camera;import android.view.animation.Animation;import android.graphics.Matrix;import android.view.animation.Transformation;/** * Created by mjm  on 2016/11/24 11:15. * 自定义动画-3D旋转动画 */public class Rotate3dAnimation extends Animation {    public static final Byte ROTATE_X_AXIS = 0x00;    public static final Byte ROTATE_Y_AXIS = 0x01;    public static final Byte ROTATE_Z_AXIS = 0x02;    private final float mFromDegrees;    private final float mToDegrees;    private final float mCenterX;    private final float mCenterY;    private final float mDepthZ;    private final boolean mReverse;    private Camera mCamera;    private Byte mRotateAxis;  // 0:X轴  1:Y轴  2:Z轴    /**创建3D旋转动画     * @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 depthZ the Z depth of the 3D rotation     * @param rotateAxis the rotate axis 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, Byte rotateAxis, boolean reverse) {        mFromDegrees = fromDegrees;        mToDegrees = toDegrees;        mCenterX = centerX;        mCenterY = centerY;        mDepthZ = depthZ;        mRotateAxis = rotateAxis;        mReverse = reverse;    }    @Override    public void initialize(int width, int height, int parentWidth, int parentHeight) {        super.initialize(width, height, parentWidth, parentHeight);        mCamera = new Camera();    }    @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) {            // z的偏移会越来越大。这就会形成这样一个效果,view从近到远            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);        } else {            // z的偏移会越来越小。这就会形成这样一个效果,我们的View从一个很远的地方向我们移过来,越来越近,最终移到了我们的窗口上面            camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));        }        // 是给我们的View加上旋转效果,在移动的过程中,视图还会以XYZ轴为中心进行旋转。        if (ROTATE_X_AXIS.equals(mRotateAxis)) {            camera.rotateX(degrees);        } else if (ROTATE_Y_AXIS.equals(mRotateAxis)) {            camera.rotateY(degrees);        } else {            camera.rotateZ(degrees);        }        // 这个是将我们刚才定义的一系列变换应用到变换矩阵上面,调用完这句之后,我们就可以将camera的位置恢复了,以便下一次再使用。        camera.getMatrix(matrix);        // camera位置恢复        camera.restore();        // 下面两句是为了动画是以View中心为旋转点        matrix.preTranslate(-centerX, -centerY);        matrix.postTranslate(centerX, centerY);    }}

通过上面的代码可以看到,使用Camera类实现动画效果 so easy。无非就是设置三个坐标轴的旋转角度,不过需要注意的是最后两行代码,可以改变旋转时的默认旋转中心。

这里写图片描述

源码下载

1 0