Android中轴旋转特效实现,制作别样的图片浏览器

Android API Demos中有很多非常Nice的例子,这些例子的代码都写的很出色,如果大家把API Demos中的每个例子研究透了,那么恭喜你已经成为一个真正的Android高手了。这也算是给一些比较迷茫的Android开发者一个指出了一个提升自我能力的方向吧。API Demos中的例子众多,今天我们就来模仿其中一个3D变换的特效,来实现一种别样的图片浏览器。

既然是做中轴旋转的特效,那么肯定就要用到3D变换的功能。在Android中如果想要实现3D效果一般有两种选择,一是使用Open GL ES,二是使用Camera。Open GL ES使用起来太过复杂,一般是用于比较高级的3D特效或游戏,像比较简单的一些3D效果,使用Camera就足够了。

Camera中提供了三种旋转方法,分别是rotateX()、rotateY()和rotateZ,调用这三个方法,并传入相应的角度,就可以让视图围绕这三个轴进行旋转,而今天我们要做的中轴旋转效果其实就是让视图围绕Y轴进行旋转。使用Camera让视图进行旋转的示意图,如下所示:

Android中轴旋转特效实现,制作别样的图片浏览器

那我们就开始动手吧,首先创建一个Android项目,起名叫做RotatePicBrowserDemo,然后我们准备了几张图片,用于稍后在图片浏览器中进行浏览。

而API Demos中已经给我们提供了一个非常好用的3D旋转动画的工具类Rotate3dAnimation,这个工具类就是使用Camera来实现的,我们先将这个这个类复制到项目中来,代码如下所示:

/**
 * An animation that rotates the view on the Y axis between two specified angles.
 * This animation also adds a translation on the Z axis (depth) to improve the effect.
 */
public class Rotate3dAnimation extends Animation {
    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;

    /**
    * 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;
    }

    @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) {
            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);
    }
}

相关阅读:

 

相关推荐