Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 系列:3D 翻轉動畫的實作細節

編輯:Android 開發實例

日前和一位朋友聊到 Android 上 3D 旋轉效果該怎麼做,今天剛好有空,把細節寫出來。

結論先說:在 Android 中要做出 3D 旋轉,只要 animation 搭配 Camera 就够了,ApiDemo 裡就有類似範例。

一、自訂 Rotate3d 動畫類別

 1 public class Rotate3d extends Animation {
 2     private float mFromDegree;
 3     private float mToDegree;
 4     private float mCenterX;
 5     private float mCenterY;
 6     private float mLeft;
 7     private float mTop;
 8     private Camera mCamera;
 9
10     public Rotate3d(float fromDegree, float toDegree, float left, float top,
11                     float centerX, float centerY) {
12         this.mFromDegree = fromDegree;
13         this.mToDegree   = toDegree;
14         this.mLeft  = left;
15         this.mTop   = top;
16         this.mCenterX = centerX;
17         this.mCenterY = centerY;
18     }
19
20     @Override
21     public void initialize(int width, int height, int parentWidth, int parentHeight) {
22         super.initialize(width, height, parentWidth, parentHeight);
23         mCamera = new Camera();
24     }
25
26     @Override
27     protected void applyTransformation(float interpolatedTime, Transformation t) {
28         final float FromDegree = mFromDegree;
29         float degrees = FromDegree + (mToDegree - mFromDegree) * interpolatedTime;
30         final float centerX = mCenterX;
31         final float centerY = mCenterY;
32         final Matrix matrix = t.getMatrix();
33
34         if (degrees <= -76.0f) {
35             degrees = -90.0f;
36             mCamera.save();
37             mCamera.rotateY(degrees);
38             mCamera.getMatrix(matrix);
39             mCamera.restore();
40         } else if (degrees >= 76.0f) {
41             degrees = 90.0f;
42             mCamera.save();
43             mCamera.rotateY(degrees);
44             mCamera.getMatrix(matrix);
45             mCamera.restore();
46         } else {
47             mCamera.save();
48             // 這裡很重要:先把 Camera 往 Z 軸拉遠一點,
49             // 避免接近 90 度時視野穿透造成閃爍
50             mCamera.translate(0, 0, centerX);
51             mCamera.rotateY(degrees);
52             mCamera.translate(0, 0, -centerX);
53             mCamera.getMatrix(matrix);
54             mCamera.restore();
55         }
56
57         matrix.preTranslate(-centerX, -centerY);
58         matrix.postTranslate(centerX, centerY);
59     }
60 }

幾個關鍵細節:applyTransformation() 裡的 interpolatedTime 是 0 到 1 的插值,用它把起始角與結束角做一次線性內插,動畫才會平滑;mCamera.save() 與 restore() 必須成對出現,否則矩陣狀態會互相污染;最後那兩行 pre/post translate 是把旋轉中心從左上角改到指定座標,少了它畫面就會繞著錯誤的原點轉。

二、在 Activity 中驅動兩個 View

 1 // 這兩句是關鍵
 2 Rotate3d leftAnimation  = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY);
 3 Rotate3d rightAnimation = new Rotate3d(-0 + 90, -90 + 90, 0.0f, 0.0f, mCenterX, mCenterY);
 4
 5 leftAnimation.setFillAfter(true);
 6 leftAnimation.setDuration(1000);
 7 rightAnimation.setFillAfter(true);
 8 rightAnimation.setDuration(1000);
 9
10 mImageView1.startAnimation(leftAnimation);
11 mImageView2.startAnimation(rightAnimation);

兩個動畫一正一反、時間相同,一張圖轉出去、另一張轉進來,看起來就是卡片翻面的效果。setFillAfter(true) 是必要的,否則動畫結束後 View 會瞬間跳回原始狀態。

三、版面檔

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent"
 5     android:layout_height="wrap_content" >
 6
 7     <FrameLayout
 8         android:layout_width="fill_parent"
 9         android:layout_height="fill_parent">
10
11         <ImageView
12             android:id="@+id/image1"
13             android:layout_gravity="center_horizontal"
14             android:layout_width="fill_parent"
15             android:layout_height="wrap_content"
16             android:src="@drawable/image1" />
17
18         <ImageView
19             android:id="@+id/image2"
20             android:background="#ffff0000"
21             android:layout_gravity="center_horizontal"
22             android:layout_width="fill_parent"
23             android:layout_height="wrap_content"
24             android:src="@drawable/image2" />
25
26     </FrameLayout>
27 </LinearLayout>

用 FrameLayout 把兩張圖疊在同一位置,動畫才能做出「互相取代」的錯覺——這也是 FrameLayout 最典型的用途。

動畫串接時若想用「轉完 A 自動接著轉 B」的效果,記得搭配 AnimationSet 或在動畫監聽器的 onAnimationEnd 裡啟動下一段,並留意 setFillAfter 與 setFillBefore 的組合,避免出現畫面閃跳。

轉自:https://www.cnblogs.com/jk1001/archive/2010/07/28/1787009.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved