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

自定義控制項與旋轉動畫:做一個會展開收起的扇形選單

編輯:關於 Android 開發

很多播放器 App 底部都有一個扇形展開的選單:點一下主圖示,二級、三級選項依序旋轉展開;再點一下,依序收回。這個效果不需要任何第三方套件,用 RotateAnimation 加上一點狀態管理就能做出來。

先認識 RotateAnimation 的參數

RotateAnimation(float fromDegrees, float toDegrees,
                int pivotXType, float pivotXValue,
                int pivotYType, float pivotYValue)
  • fromDegrees:起始角度。
  • toDegrees:結束角度。
  • pivotXType / pivotYType:旋轉軸心的座標模式,可選 ABSOLUTE(絕對像素)、RELATIVE_TO_SELF(相對於自身尺寸)、RELATIVE_TO_PARENT(相對於父容器尺寸)。
  • pivotXValue / pivotYValue:軸心位置。搭配 RELATIVE_TO_SELF 時,0.5f 代表 50% 處。

另外三個常用設定:

animation.setRepeatCount(int repeatCount);  // 重複次數
animation.setFillAfter(boolean);            // 動畫結束後是否停在最終狀態
animation.setStartOffset(long startOffset); // 開始前的延遲毫秒數

setFillAfter(true) 在這個效果裡是必須的——否則動畫播完,控制項會「啪」地跳回原位。

動畫工具類

把展開與收起抽成兩個靜態方法,並用一個計數器記錄目前有幾個動畫正在跑,避免使用者連點造成狀態錯亂:

public class AnimUtil {
    public static int animCount = 0;   // 正在執行的動畫數量

    public static void closeMenu(RelativeLayout rl, int startOffset) {
        for (int i = 0; i < rl.getChildCount(); i++) {
            rl.getChildAt(i).setEnabled(false);
        }
        RotateAnimation animation = new RotateAnimation(
            0, -180,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 1);
        animation.setDuration(500);
        animation.setFillAfter(true);
        animation.setStartOffset(startOffset);
        animation.setAnimationListener(new MyAnimationListener());
        rl.startAnimation(animation);
    }

    public static void showMenu(RelativeLayout rl, int startOffset) {
        for (int i = 0; i < rl.getChildCount(); i++) {
            rl.getChildAt(i).setEnabled(true);
        }
        RotateAnimation animation = new RotateAnimation(
            -180, 0,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 1);
        animation.setDuration(500);
        animation.setFillAfter(true);
        animation.setStartOffset(startOffset);
        animation.setAnimationListener(new MyAnimationListener());
        rl.startAnimation(animation);
    }

    static class MyAnimationListener implements AnimationListener {
        @Override public void onAnimationStart(Animation animation) { animCount++; }
        @Override public void onAnimationEnd(Animation animation)   { animCount--; }
        @Override public void onAnimationRepeat(Animation animation) { }
    }
}

這裡有兩個設計值得注意。第一,旋轉軸心設為 RELATIVE_TO_SELF 的 0.5f、1(水平中間、底部),這樣控制項會繞著自己的「底部中心」翻轉,視覺上像是從主圖示向外展開。第二,把子控制項的 enabled 狀態與動畫同步切換,可以避免動畫進行中被誤觸。

Activity 中的狀態管理

三層選單需要三個旗標,而「層級之間的延遲」是節奏感的來源:

public class MainActivity extends Activity implements OnClickListener {
    private ImageView iv_home, iv_menu;
    private RelativeLayout level1, level2, level3;
    private boolean isShowLevel2 = true;   // 二級選單是否顯示
    private boolean isShowLevel3 = true;   // 三級選單是否顯示
    private boolean isShowMenu   = true;   // 整個選單是否顯示

    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.iv_home:
                if (AnimUtil.animCount != 0) return;   // 有動畫在跑就忽略點擊

                if (isShowLevel2) {
                    int startOffset = 0;
                    if (isShowLevel3) {
                        AnimUtil.closeMenu(level3, startOffset);
                        startOffset += 200;            // 三級先收,二級延遲 200ms
                        isShowLevel3 = false;
                    }
                    AnimUtil.closeMenu(level2, startOffset);
                } else {
                    AnimUtil.showMenu(level2, 0);
                }
                isShowLevel2 = !isShowLevel2;
                break;

            case R.id.iv_menu:
                if (AnimUtil.animCount != 0) return;
                if (isShowLevel3) {
                    AnimUtil.closeMenu(level3, 0);
                } else {
                    AnimUtil.showMenu(level3, 0);
                }
                isShowLevel3 = !isShowLevel3;
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            if (isShowMenu) {
                int startOffset = 0;
                if (isShowLevel3) { AnimUtil.closeMenu(level3, startOffset); isShowLevel3 = false; startOffset += 200; }
                if (isShowLevel2) { AnimUtil.closeMenu(level2, startOffset); isShowLevel2 = false; startOffset += 200; }
                AnimUtil.closeMenu(level1, startOffset);
            } else {
                AnimUtil.showMenu(level1, 0);
                AnimUtil.showMenu(level2, 200);
                isShowLevel2 = true;
                AnimUtil.showMenu(level3, 400);
                isShowLevel3 = true;
            }
            isShowMenu = !isShowMenu;
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

整段邏輯的核心就一句話:由外而內展開時遞增延遲,由內而外收起時遞增延遲。這個 200 毫秒的間隔讓動畫有了「層層展開」的層次感,而不是三層同時甩出來。

版面配置

三層用三個同心的 RelativeLayout 疊在底部中央,各自設定不同的背景圖與子控制項位置:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <RelativeLayout android:id="@+id/level1"
        android:layout_width="100dp" android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level1" >
        <ImageView android:id="@+id/iv_home"
            android:layout_centerInParent="true"
            android:background="@drawable/icon_home" />
    </RelativeLayout>

    <RelativeLayout android:id="@+id/level2"
        android:layout_width="180dp" android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level2" >
        <ImageView android:id="@+id/iv_menu"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:background="@drawable/icon_menu" />
        <!-- 其餘子控制項以 layout_marginBottom / marginLeft 定位 -->
    </RelativeLayout>

    <RelativeLayout android:id="@+id/level3"
        android:layout_width="280dp" android:layout_height="142dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level3" >
        <!-- 七個頻道圖示,以 marginBottom + margin 交錯排列成扇形 -->
    </RelativeLayout>
</RelativeLayout>

可以怎麼優化

這個版本用 view 的旋轉屬性做動畫,缺點是控制項在動畫期間的可點擊區域不會跟著轉(View 的 hit test 仍依原始位置計算)。若要更精確的行為,可以改用 ObjectAnimator 搭配 setRotation 並在動畫結束後手動更新 layout,或直接換成属性動畫操作 translation 與 alpha 的組合。另外,把三個旗標封裝進一個選單狀態物件,也能讓 Activity 的程式碼更乾淨。

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