Android中軸旋轉特效實現,製作別樣的圖片瀏覽器

來源:互聯網
上載者:User

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項目,起名叫做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);      }  }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.