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

仿優酷旋轉菜單:Android 多層菜單動畫實現詳解

編輯:關於 Android 程式設計

優酷客戶端那種扇形展開的旋轉菜單很有辨識度。它的實現並不神秘——本質上是分層布局加上旋轉動畫的組合。本文完整拆解整個過程。

效果預覽

一、布局:三層 RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#c9c9c9" >

    <RelativeLayout android:id="@+id/relate_level3"
        android:layout_width="280dp" android:layout_height="140dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level3" >
        <ImageButton android:id="@+id/c1" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentBottom="true"
            android:layout_marginBottom="6dip" android:layout_marginLeft="12dip"
            android:background="@drawable/channel1" />
        <ImageButton android:id="@+id/c2" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_above="@+id/c1"
            android:layout_marginBottom="12dip" android:layout_marginLeft="28dip"
            android:background="@drawable/channel2" />
        <ImageButton android:id="@+id/c3" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_above="@+id/c2"
            android:layout_marginBottom="8dip" android:layout_marginLeft="6dip"
            android:layout_toRightOf="@+id/c2" android:background="@drawable/channel3" />
        <ImageButton android:id="@+id/c4" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_centerHorizontal="true"
            android:layout_margin="6dip" android:background="@drawable/channel4" />
        <ImageButton android:id="@+id/c5" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_above="@+id/c6"
            android:layout_marginBottom="8dip" android:layout_marginRight="6dip"
            android:layout_toLeftOf="@+id/c6" android:background="@drawable/channel5" />
        <ImageButton android:id="@+id/c6" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_above="@+id/c7"
            android:layout_alignParentRight="true" android:layout_marginBottom="12dip"
            android:layout_marginRight="28dip" android:background="@drawable/channel6" />
        <ImageButton android:id="@+id/c7" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true" android:layout_marginBottom="6dip"
            android:layout_marginRight="12dip" android:background="@drawable/channel7" />
    </RelativeLayout>

    <RelativeLayout android:id="@+id/relate_level2"
        android:layout_width="180dp" android:layout_height="90dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level2" >
        <ImageButton android:id="@+id/menu" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_centerHorizontal="true"
            android:layout_margin="6dip" android:background="@drawable/icon_menu" />
        <ImageButton android:id="@+id/search" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentBottom="true"
            android:layout_margin="10dip" android:background="@drawable/icon_search" />
        <ImageButton android:id="@+id/myyouku" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true" android:layout_margin="10dip"
            android:background="@drawable/icon_myyouku" />
    </RelativeLayout>

    <RelativeLayout android:id="@+id/relate_level1"
        android:layout_width="100dp" android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@drawable/level1" >
        <ImageButton android:id="@+id/home" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" android:layout_marginBottom="10dp"
            android:background="@drawable/icon_home" />
    </RelativeLayout>
</RelativeLayout>

三層 RelativeLayout 對應視覺上的三層扇形。你可能會好奇那個傾斜效果是怎麼做出來的——其實是個「假象」:容器本身擺放得整整齊齊,傾斜感完全來自切圖本身。也就是說,美术在切圖時就把傾斜角度畫進去了。

二、Activity:控制層的展開與收起

用兩個布爾變量分別記錄第二層和第三層的顯示狀態,點擊時按需播放動畫:

public class TestYoukuActivity extends Activity {
    private boolean areLevel2Showing = true, areLevel3Showing = true;
    private RelativeLayout relate_level2, relate_level3;
    private ImageButton home, menu;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViews();
        setListener();
    }

    private void findViews() {
        relate_level2 = (RelativeLayout) findViewById(R.id.relate_level2);
        relate_level3 = (RelativeLayout) findViewById(R.id.relate_level3);
        home = (ImageButton) findViewById(R.id.home);
        menu = (ImageButton) findViewById(R.id.menu);
    }

    private void setListener() {
        // 主按鈕:控制第二層的展開與收起
        home.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!areLevel2Showing) {
                    MyAnimation.startAnimationsIn(relate_level2, 500);
                } else {
                    if (areLevel3Showing) {
                        // 第二層收起時,第三層也要一起收起
                        MyAnimation.startAnimationsOut(relate_level2, 500, 500);
                        MyAnimation.startAnimationsOut(relate_level3, 500, 0);
                        areLevel3Showing = !areLevel3Showing;
                    } else {
                        MyAnimation.startAnimationsOut(relate_level2, 500, 0);
                    }
                }
                areLevel2Showing = !areLevel2Showing;
            }
        });

        // 菜單按鈕:控制第三層
        menu.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!areLevel3Showing) {
                    MyAnimation.startAnimationsIn(relate_level3, 500);
                } else {
                    MyAnimation.startAnimationsOut(relate_level3, 500, 0);
                }
                areLevel3Showing = !areLevel3Showing;
            }
        });
    }
}

三、入動畫:旋轉著展開

public static void startAnimationsIn(ViewGroup viewgroup, int durationMillis) {
    viewgroup.setVisibility(0);
    for (int i = 0; i < viewgroup.getChildCount(); i++) {
        viewgroup.getChildAt(i).setVisibility(0);
        viewgroup.getChildAt(i).setClickable(true);
        viewgroup.getChildAt(i).setFocusable(true);
    }

    Animation animation = new RotateAnimation(-180, 0,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 1.0f);
    animation.setFillAfter(true);
    animation.setDuration(durationMillis);
    viewgroup.startAnimation(animation);
}

這裡用的是 RotateAnimation 的五參數構造方法,含義如下:

fromDegrees:起始角度。
toDegrees:結束角度。
pivotXType:X 軸的伸縮模式,可取 ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
pivotXValue:X 軸的伸縮值。
pivotYType:Y 軸的伸縮模式,取值同上。
pivotYValue:Y 軸的伸縮值。

關於角度的正負:負數代表逆時針旋轉,正數代表順時針旋轉。from 到 to 之間只要跨越了零點,旋轉方向就會改變——比如 from 為負、to 為正時是順時針,from 為負、to 為負時是逆時針。

關於 pivotXValue:它指定了「繞哪一點旋轉」。以絕對模式計算時,0 是左邊緣,50% 是水平中點,100% 是右邊緣。pivotYValue 同理,0 是頂部邊緣,100% 是底部邊緣。示例中選 (0.5f, 1.0f),也就是繞著容器底部中心旋轉,這樣展開時就像扇形從底部掀開。

四、出動畫:旋轉著收起

public static void startAnimationsOut(final ViewGroup viewgroup,
        int durationMillis, int startOffset) {
    Animation animation = new RotateAnimation(0, -180,
            Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 1.0f);
    animation.setFillAfter(true);
    animation.setDuration(durationMillis);
    animation.setStartOffset(startOffset);

    animation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation arg0) {}

        @Override
        public void onAnimationRepeat(Animation arg0) {}

        @Override
        public void onAnimationEnd(Animation arg0) {
            // 動畫結束後再真正隱藏,避免過程中閃現
            viewgroup.setVisibility(8);
            for (int i = 0; i < viewgroup.getChildCount(); i++) {
                viewgroup.getChildAt(i).setVisibility(8);
                viewgroup.getChildAt(i).setClickable(false);
                viewgroup.getChildAt(i).setFocusable(false);
            }
        }
    });

    viewgroup.startAnimation(animation);
}

這裡有兩個值得琢磨的細節:

一是 setStartOffset(startOffset) 用來設置動畫延遲多久開始。回到 Activity 的代碼,收起第二層時給它傳了 500 的偏移、給第三層傳了 0,於是第三層先收起,第二層延后半秒再動,形成有層次先後的收尾效果。

二是 setVisibility 的時機被放在了 onAnimationEnd 裡,而不是動畫開始前。如果提前隱藏,旋轉過程中容器就直接消失了;等動畫播完再隱藏,視覺上才連貫。同時把子元素的 clickable 與 focusable 一併關掉,避免隱形控件誤吞點擊事件。

理解了「分層布局 + 旋轉軸設定 + 延遲錯開 + 動畫結束後再隱藏」這四個要點,類似的扇形菜單、環形菜單都能自己拼出來。

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