Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

自訂 Android 動畫:繼承 Animation 實作 3D 翻轉效果

編輯:關於 Android 開發

自訂動畫的關鍵只有兩個方法:繼承 Animation,然後在 initialize 中做初始化、在 applyTransformation 中依插值時間做矩陣變換。

以下示範一個模仿 3D 翻轉的動畫,靠 Camera 提供的旋轉與透視變換來達成景深感。

Rotate3dAnimation

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;

    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();
    }

    // 生成 Transformation
    @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.rotateY(mCenterY);
        } else {
            camera.rotateY(-mCenterY);
        }
        camera.rotateY(degrees);
        // 取得變換後的矩陣
        camera.getMatrix(matrix);
        camera.restore();

        // 以中心點為軸旋轉:先平移到原點,變換後再平移回去
        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}

呼叫方式

以下 mSecondMenuLayout 為要套用動畫的 View:

/**
 * @param start 起始角度
 * @param end   結束角度
 */
private void applyRotation(float start, float end) {
    // 計算中心點
    final float centerX = mSecondMenuLayout.getWidth() / 2.0f;

    final Rotate3dAnimation rotation =
            new Rotate3dAnimation(start, end, centerX, 360, 310.0f, true);

    rotation.setDuration(1000);                                    // 動畫時長
    rotation.setFillAfter(false);                                  // 結束後是否保留最終狀態
    rotation.setInterpolator(new AccelerateDecelerateInterpolator()); // 先加速後減速
    rotation.setAnimationListener(mAnimListener);                  // 動畫監聽

    mSecondMenuLayout.startAnimation(rotation);
}

幾個容易忽略的細節

中心點為什麼要 pre/post 成對出現。Matrix 的旋轉預設以原點為軸,所以必須先把座標系平移到原點、做完變換、再平移回來。少了這一段,View 就會繞著左上角翻,而不是繞著自己。

setFillAfter 的意義。設為 false 時,動畫結束後 View 會回到原本的狀態;若希望停在最終畫面,則設為 true,或在 onAnimationEnd 中直接改 View 屬性。只依賴 setFillAfter 有時會在不同版本上表現不一致,正式專案中比較穩妥的做法是動畫結束後設定 View 的實際屬性。

插值器決定手感。AccelerateDecelerateInterpolator 讓翻轉有自然的加速與收尾;若想強調「啪」一下的速度感,可以改用 AccelerateInterpolator 或自訂路徑插值器。

記得清理動畫監聽。若 Activity 可能在動畫結束前被銷毀,監聽器中持有 Activity 參照會造成洩漏,記得在 onDestroy 中把監聽器設為 null。

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