編輯:關於 Android 程式設計
想讓App的頁面切換更有質感嗎?本文帶你動手實作一款帶有立體感的3D滑動翻頁動畫,透過自定義Animation類別,讓畫面像書頁一樣沿著Y軸翻轉,兼顧視覺效果與程式碼的靈活性。
Android的動畫體系主要分成兩大類:逐幀動畫(frame-by-frame animation)與補間動畫(tweened animation)。本範例屬於後者,我們透過繼承Animation類別,自訂一個名為Rotate3D的旋轉動畫,模擬翻書般的3D翻頁體驗。效果如下圖:

核心在於繼承Animation並覆寫applyTransformation方法,利用Camera物件沿Y軸旋轉,同時透過Matrix處理旋轉中心的偏移,讓翻頁動作自然流暢。
public class Rotate3D extends Animation { private float fromDegree; // 旋轉起始角度 private float toDegree; // 旋轉終止角度 private float mCenterX; // 旋轉中心x private float mCenterY; // 旋轉中心y private Camera mCamera; public Rotate3D(float fromDegree, float toDegree, float centerX, float centerY) { this.fromDegree = fromDegree; this.toDegree = toDegree; this.mCenterX = centerX; this.mCenterY = centerY; } @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 FromDegree = fromDegree; float degrees = FromDegree + (toDegree - fromDegree) * interpolatedTime; // 旋轉角度(angle) final float centerX = mCenterX; final float centerY = mCenterY; final Matrix matrix = t.getMatrix(); if (degrees <= -76.0f) { degrees = -90.0f; mCamera.save(); mCamera.rotateY(degrees); // 旋轉 mCamera.getMatrix(matrix); mCamera.restore(); } else if (degrees >= 76.0f) { degrees = 90.0f; mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); } else { mCamera.save(); mCamera.translate(0, 0, centerX); // 位移x mCamera.rotateY(degrees); mCamera.translate(0, 0, -centerX); mCamera.getMatrix(matrix); mCamera.restore(); } matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } }
接著初始化各個方向的旋轉動畫。這裡以水平方向為0度基準,分別定義「上一頁」與「下一頁」在離開與進場時的旋轉角度:
public void initAnimation() { // 獲取旋轉中心 DisplayMetrics dm = new DisplayMetrics(); dm = getResources().getDisplayMetrics(); mCenterX = dm.widthPixels / 2; mCenterY = dm.heightPixels / 2; // 定義旋轉方向 int duration = 1000; lQuest1Animation = new Rotate3D(0, -90, mCenterX, mCenterY); // 下一頁的【question1】旋轉方向(從0度轉到-90,參考系為水平方向為0度) lQuest1Animation.setFillAfter(true); lQuest1Animation.setDuration(duration); lQuest2Animation = new Rotate3D(90, 0, mCenterX, mCenterY); // 下一頁的【question2】旋轉方向(從90度轉到0,參考系為水平方向為0度)(起始第一題) lQuest2Animation.setFillAfter(true); lQuest2Animation.setDuration(duration); rQuest1Animation = new Rotate3D(0, 90, mCenterX, mCenterY); // 上一頁的【question1】旋轉方向(從0度轉到90,參考系為水平方向為0度) rQuest1Animation.setFillAfter(true); rQuest1Animation.setDuration(duration); rQuest2Animation = new Rotate3D(-90, 0, mCenterX, mCenterY); // 上一頁的【question2】旋轉方向(從-90度轉到0,參考系為水平方向為0度) rQuest2Animation.setFillAfter(true); rQuest2Animation.setDuration(duration); }
我們準備兩份佈局檔,分別對應翻頁前後的畫面:
main.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/layout_main" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> ... </LinearLayout>
next.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/layout_next" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> ... </LinearLayout>
受限於篇幅,完整佈局檔請參考原始碼。
初始化兩份佈局並取得其中的控制項:
private void initMain(){ setContentView(R.layout.main); layoutmain = (LinearLayout)findViewById(R.id.layout_main); btn_MainLast = (Button)findViewById(R.id.main_last); btn_MainNext = (Button)findViewById(R.id.main_next); btn_MainLast.setOnClickListener(listener); btn_MainNext.setOnClickListener(listener); } private void initNext(){ setContentView(R.layout.next); layoutnext = (LinearLayout)findViewById(R.id.layout_next); btn_NextLast = (Button)findViewById(R.id.next_last); btn_NextNext = (Button)findViewById(R.id.next_next); btn_NextLast.setOnClickListener(listener); btn_NextNext.setOnClickListener(listener); }
最後,透過按鈕的點擊事件觸發對應方向的3D翻頁動畫:
private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.main_last: // 上一頁 layoutmain.startAnimation(lQuest1Animation); // 當前頁向左旋轉(0,-90) initNext(); layoutnext.startAnimation(lQuest2Animation); // 下一頁向左旋轉(90, 0) break; case R.id.main_next: // 下一頁 layoutmain.startAnimation(rQuest1Animation); // 當前頁向右旋轉(0,90) initNext(); layoutnext.startAnimation(rQuest2Animation); // 下一頁向右旋轉(-90, 0) break; case R.id.next_last: layoutnext.startAnimation(lQuest1Animation); initMain(); layoutmain.startAnimation(lQuest2Animation); break; case R.id.next_next: layoutnext.startAnimation(rQuest1Animation); initMain(); layoutmain.startAnimation(rQuest2Animation); break; } } };
透過上述步驟,即可為App加入流暢的3D翻頁體驗。完整範例程式碼可至本站下載,希望本文能為你的Android開發帶來靈感。
初始化時取得布局寬高:RelativeLayout 的 LayoutParams 陷阱
前一篇談過如何在 onCreate 階段取得尚未顯示的 View 寬高——呼叫 view.measure(0, 0),再用 getMeasuredWidth() 與 ge
Android 設計模式筆記(二):觀察者模式的四個角色與實作
觀察者模式有時也被稱為發布/訂閱模式。它定義了一種一對多的依賴關係,讓多個觀察者物件同時監聽同一個主題物件;當主題的狀態發生變化時,會主動通知所有觀察者,使它們能夠自動更
OkHttp 簡單封裝:打造省心的網絡請求輔助框架
序言 OkHttp 的強大毋庸置疑,幾乎能在網絡層搞定一切。但也正因為功能全面,每構建一個請求都得寫一大堆配置代碼。這裡分享一個對 OkHttp 做了極限封裝的輔助框架,
Android ViewPager 使用方法詳解
ViewPager 是實作左右滑動切頁最常見的元件,核心在於三件事:準備好要切換的 View 清單、寫一個 PagerAdapter、然後掛到 ViewPager 上。