編輯:關於 Android 程式設計
本文要動手做一個 Android 上的立體旋轉(3D 翻轉)動畫,下圖是最終跑出來的樣子。

立體旋轉其實只有三種基本型態:
1. 繞 X 軸 翻轉
2. 繞 Y 軸 翻轉
3. 繞 Z 軸 旋轉——這一種等同於 Android 內建的 RotateAnimation。
三種效果共用同一套程式碼,只差在呼叫 Camera 的哪一個方法。
第一,繼承 Animation 並覆寫 applyTransformation。這個方法的兩個回調參數就掌握了整支動畫:interpolatedTime 是 0~1 的動畫進度,用來插值算出當前角度;Transformation t 則提供變換矩陣,實際的旋轉效果都寫在它身上。
第二,用 android.graphics.Camera 負責 3D 運算。Camera 能對影像做旋轉、放縮這類較複雜的空間操作,搭配 Matrix 還能做出傾斜效果。它省去我們自己推 3D 投影矩陣的麻煩。
package rotateanim.example.com.androidrotateanim;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.graphics.Camera;
import android.graphics.Matrix;
/**
* 在 X、Y、Z 軸之間旋轉 View 的動畫類別。
* 同時會沿 Z 軸做一段位移(景深),讓立體感更明顯。
*/
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 起始角度
* @param toDegrees 結束角度
* @param centerX 旋轉中心的 X 座標
* @param centerY 旋轉中心的 Y 座標
* @param depthZ Z 軸(景深)位移量
* @param rotateAxis 旋轉所繞的軸
* @param reverse 是否反向位移
*/
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));
}
// 依指定軸心加上旋轉效果
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.restore();
// 以下兩行確保旋轉是以 View 自己的中心為軸心
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
用法和一般動畫沒有兩樣:new 出來、設定時長、丟給 View 的 startAnimation 就結束了。以下假設 mRotateImgv 是要旋轉的 View:
// 繞 X 軸翻轉
private void rotateOnXCoordinate() {
float centerX = mRotateImgv.getWidth() / 2.0f;
float centerY = mRotateImgv.getHeight() / 2.0f;
float depthZ = 0f;
Rotate3dAnimation rotate3dAnimationX =
new Rotate3dAnimation(0, 180, centerX, centerY, depthZ,
Rotate3dAnimation.ROTATE_X_AXIS, true);
rotate3dAnimationX.setDuration(1000);
mRotateImgv.startAnimation(rotate3dAnimationX);
}
// 繞 Y 軸翻轉
private void rotateOnYCoordinate() {
float centerX = mRotateImgv.getWidth() / 2.0f;
float centerY = mRotateImgv.getHeight() / 2.0f;
float centerZ = 0f;
Rotate3dAnimation rotate3dAnimationY =
new Rotate3dAnimation(0, 180, centerX, centerY, centerZ,
Rotate3dAnimation.ROTATE_Y_AXIS, true);
rotate3dAnimationY.setDuration(1000);
mRotateImgv.startAnimation(rotate3dAnimationY);
}
// 繞 Z 軸旋轉,等同一般的平面旋轉動畫
private void rotateAnimHorizon() {
float centerX = mRotateImgv.getWidth() / 2.0f;
float centerY = mRotateImgv.getHeight() / 2.0f;
float centerZ = 0f;
Rotate3dAnimation rotate3dAnimationZ =
new Rotate3dAnimation(180, 0, centerX, centerY, centerZ,
Rotate3dAnimation.ROTATE_Z_AXIS, true);
rotate3dAnimationZ.setDuration(1000);
mRotateImgv.startAnimation(rotate3dAnimationZ);
// 也可以直接用 Android 內建的動畫達到同樣效果
// RotateAnimation rotateAnimation = new RotateAnimation(0, 180,
// Animation.RELATIVE_TO_SELF, 0.5f,
// Animation.RELATIVE_TO_SELF, 0.5f);
// rotateAnimation.setDuration(1000);
// mRotateImgv.startAnimation(rotateAnimation);
}
把 depthZ 調大一點、再搭配 reverse 參數控制方向,就能做出「卡片飛遠」或「卡片飛近」兩種截然不同的手感,實務上很常用在商品卡翻面、封面切換這類場景。
Android Studio 進階詳細配置:提升開發效率的實用技巧
Android Studio 是 Android 開發中最常用的 IDE 之一。除了基礎功能,合理的進階配置能顯著提升閱讀代碼、編寫代碼與排查問題的效率。以下幾項配置非常
Android 本地儲存方案對比:5 款值得關注的 ORM 框架
做 Android 應用時,資料放哪裡通常是首先要決定的問題。大致可以分成三條路:存到本機、放到自己的後端(提供 API)、或者託給開放的雲端服務(SyncAdapter
Android 列表互動實戰:PullToRefresh 實現下拉刷新與左滑刪除
寫在前面:ListView 的下拉刷新功能,做 Android 的同學大概都不陌生。如今 Google 官方的 SwipeRefreshLayout 效果在很多 App
Android Activity 四種啟動模式比較:standard、singleTop、singleTask、singleInstance
Activity 啟動模式 Android Activity 共有四種啟動模式:standard、singleTop、singleTask、singleInstance。