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

自製安卓環形菜單:菜單鍵、返回鍵交互與進場動畫一次搞定

編輯:關於 Android 程式設計

先上效果圖:

最外層佈局很樸素,就是一個撐滿父容器的 RelativeLayout:

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

動畫工具類

環形菜單的進場與退場本質上是旋轉,因此封裝成一個工具類,並預留延時參數,方便多層菜單錯開播放:

package com.example.circlemenuofbottom.anim;

import android.view.animation.RotateAnimation;
import android.widget.RelativeLayout;

/**
 * 環形菜單動畫工具
 */
public class MyAnimationUtils {

    /** 退場動畫:順時針轉出 */
    public static void animaOut(RelativeLayout view) {
        animaOut(view, 0);
    }

    /** 進場動畫:逆時針轉入 */
    public static void animaIn(RelativeLayout view) {
        animaIn(view, 0);
    }

    /** 退場動畫:順時針轉出,可指定延時 */
    public static void animaOut(RelativeLayout view, int offSetTime) {
        RotateAnimation animation = new RotateAnimation(
                0, 180, view.getWidth() / 2, view.getHeight());
        animation.setDuration(500);
        animation.setStartOffset(offSetTime);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }

    /** 進場動畫:逆時針轉入,可指定延時 */
    public static void animaIn(RelativeLayout view, int offSetTime) {
        RotateAnimation animation = new RotateAnimation(
                -180, -360, view.getWidth() / 2, view.getHeight());
        animation.setDuration(500);
        animation.setStartOffset(offSetTime);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }
}

這裡有個細節值得留意:旋轉的軸心取 view.getWidth() / 2 與 view.getHeight(),也就是把菜單擺在底部扇形展開的效果來源。

按鍵處理思路

菜單鍵負責一級、二級菜單的顯示與隱藏;返回鍵則做成「再按一次退出」,避免誤觸:

/** 監聽菜單鍵:切換一級、二級菜單的顯示與隱藏 */
/** 監聽返回鍵:再按一次退出程式 */
private long exitTime = 0;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // 菜單鍵處理
    if (event.getAction() == KeyEvent.ACTION_DOWN
            && keyCode == KeyEvent.KEYCODE_MENU) {

        if (isShowLevel1) {
            // 一級菜單已顯示,全部收起
            MyAnimationUtils.animaOut(level);
            isShowLevel1 = false;

            if (isShowLevel2) {
                MyAnimationUtils.animaOut(level2, 120);
                isShowLevel2 = false;

                if (isShowLevel3) {
                    MyAnimationUtils.animaOut(level3, 120);
                    isShowLevel3 = false;
                }
            }
        } else {
            // 一級菜單隱藏中,展開一、二級菜單
            MyAnimationUtils.animaIn(level);
            MyAnimationUtils.animaIn(level2, 120);
            isShowLevel1 = true;
            isShowLevel2 = true;
        }
    }

    // 返回鍵:兩秒內再按一次才退出
    if (event.getAction() == KeyEvent.ACTION_DOWN
            && keyCode == KeyEvent.KEYCODE_BACK) {
        if (System.currentTimeMillis() - exitTime > 2000) {
            Toast.makeText(this, "再按一次返回鍵退出", Toast.LENGTH_SHORT).show();
            exitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

三個要點:一是用 120 毫秒的 startOffset 讓二、三級菜單依次錯開出現,視覺上更有層次;二是每層菜單用獨立的狀態標誌記錄開合,避免狀態錯亂;三是返回鍵攔截後返回 true,並以兩秒時間窗判斷是否真的退出。

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