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

用屬性動畫打造酷炫的圓形展開菜單

編輯:關於 Android 程式設計

直接上代碼。效果是這樣的:中間一個主按鈕,點一下周圍的菜單項以圓心為軸旋轉展開;再點一下旋轉收回。外層還有第二圈菜單,可以分層展開。

package com.example.testroundmenu;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class Main extends Activity {

    // 下標分別是從下到上、從左到右
    private RelativeLayout rl_1, rl_2, rl_3;
    private ImageView iv_rl1_home, iv_rl2_1, iv_rl2_2, iv_rl2_3,
            iv_rl3_1, iv_rl3_2, iv_rl3_3, iv_rl3_4, iv_rl3_5, iv_rl3_6, iv_rl3_7;

    private boolean isMidleMenuShow = true;
    private boolean isOutMenuShow = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
    }

    private void initView() {
        MyListener listener = new MyListener();

        rl_1 = (RelativeLayout) findViewById(R.id.rl_1);
        rl_2 = (RelativeLayout) findViewById(R.id.rl_2);
        rl_3 = (RelativeLayout) findViewById(R.id.rl_3);

        iv_rl1_home = (ImageView) findViewById(R.id.iv_home);

        iv_rl2_1 = (ImageView) findViewById(R.id.iv_search);
        iv_rl2_2 = (ImageView) findViewById(R.id.iv_menu);
        iv_rl2_3 = (ImageView) findViewById(R.id.iv_myyouku);

        iv_rl3_1 = (ImageView) findViewById(R.id.iv_out_1);
        iv_rl3_2 = (ImageView) findViewById(R.id.iv_out_2);
        iv_rl3_3 = (ImageView) findViewById(R.id.iv_out_3);
        iv_rl3_4 = (ImageView) findViewById(R.id.iv_out_4);
        iv_rl3_5 = (ImageView) findViewById(R.id.iv_out_5);
        iv_rl3_6 = (ImageView) findViewById(R.id.iv_out_6);
        iv_rl3_7 = (ImageView) findViewById(R.id.iv_out_7);

        iv_rl1_home.setOnClickListener(listener);
        iv_rl2_1.setOnClickListener(listener);
        iv_rl2_2.setOnClickListener(listener);
        iv_rl2_3.setOnClickListener(listener);
        iv_rl3_1.setOnClickListener(listener);
        iv_rl3_2.setOnClickListener(listener);
        iv_rl3_3.setOnClickListener(listener);
        iv_rl3_4.setOnClickListener(listener);
        iv_rl3_5.setOnClickListener(listener);
        iv_rl3_6.setOnClickListener(listener);
        iv_rl3_7.setOnClickListener(listener);
    }

    /**
     * ImageView 的統一點擊監聽
     */
    public class MyListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_home:
                    if (isMidleMenuShow) {
                        isMidleMenuShow = false;
                        animatrHiden(rl_2, 500);
                        // 如果最外層菜單正在顯示,一起收起
                        if (isOutMenuShow) {
                            animatrHiden(rl_3, 1000);
                            isOutMenuShow = false;
                        }
                    } else {
                        isMidleMenuShow = true;
                        animatrShow(rl_2, 500);
                    }
                    showToast("HOME");
                    break;
                case R.id.iv_menu:
                    showToast("我的菜單");
                    if (isOutMenuShow) {
                        animatrHiden(rl_3, 1000);
                        isOutMenuShow = false;
                    } else {
                        animatrShow(rl_3, 1000);
                        isOutMenuShow = true;
                    }
                    break;
                case R.id.iv_search:
                    showToast("我的搜索");
                    break;
                case R.id.iv_myyouku:
                    showToast("我的優酷");
                    break;
                case R.id.iv_out_1:
                    showToast("第一個");
                    break;
                case R.id.iv_out_2:
                    showToast("第二個");
                    break;
                case R.id.iv_out_3:
                    showToast("第三個");
                    break;
                case R.id.iv_out_4:
                    showToast("第四個");
                    break;
                case R.id.iv_out_5:
                    showToast("第五個");
                    break;
                case R.id.iv_out_6:
                    showToast("第六個");
                    break;
                case R.id.iv_out_7:
                    showToast("第七個");
                    break;
                default:
                    break;
            }
        }
    }

    public void showToast(String text) {
        Toast.makeText(Main.this, text, Toast.LENGTH_SHORT).show();
    }

    /**
     * 讓視圖動畫顯示出來:180° → 360°
     */
    public void animatrShow(View view, long durction) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 180f, 360f);
        // 設置旋轉中心點
        view.setPivotX(view.getWidth() / 2);
        view.setPivotY(view.getHeight());
        objectAnimator.setDuration(durction).start();
    }

    /**
     * 讓視圖動畫隱藏起來:0° → 180°
     */
    public void animatrHiden(View view, long durction) {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "rotation", 0f, 180f);
        view.setPivotX(view.getWidth() / 2);
        view.setPivotY(view.getHeight());
        objectAnimator.setDuration(durction).start();
    }

    // 下面這兩種是傳統補間動畫,沒有交互效果,僅作對比參考

    /**
     * 0° → 180°,讓視圖隱藏
     */
    public static void startAnimout(RelativeLayout view) {
        RotateAnimation rotateAnimation = new RotateAnimation(0, 180,
                view.getWidth() / 2, view.getHeight());
        rotateAnimation.setDuration(500);
        rotateAnimation.setFillAfter(true);
        view.startAnimation(rotateAnimation);
    }

    /**
     * 180° → 360°,讓視圖出現
     */
    public static void startAnimin(RelativeLayout view) {
        RotateAnimation rotateAnimation = new RotateAnimation(180, 360,
                view.getWidth() / 2, view.getHeight());
        rotateAnimation.setDuration(500);
        rotateAnimation.setFillAfter(true);
        view.startAnimation(rotateAnimation);
    }
}

關鍵代碼:設置旋轉中心

// 設置旋轉中心點
view.setPivotX(view.getWidth() / 2);
view.setPivotY(view.getHeight());

這三行是整個效果的靈魂。setPivotX / setPivotY 決定了旋轉、縮放圍繞哪個點進行。把 Y 軸支點設在 view 的底部(getHeight()),X 軸設在水平中線,菜-menu 項就會以「母按鈕所在位置」為圓心做圓周運動,而不是原地打轉。如果改成 getHeight() / 2,效果立刻就變成繞自身中心旋轉,完全不是想要的樣子。

為什麼用屬性動畫而不是補間動畫

文件末尾保留的 startAnimout / startAnimin 用的是老式 RotateAnimation。兩者最大的差別在於:補間動畫只是改變了繪製時的變換矩陣,View 自身的真實屬性並沒有變化,所以動畫結束後視圖會「彈回」原位(除非設了 setFillAfter),而且點擊事件的命中區域仍停留在最初的位置,交互上很容易出 bug。屬性動畫則是真正修改了 View 的 rotation 屬性,位置、事件、狀態全部同步更新,這也是它更值得推薦的原因。

想讓效果更細膩,還可以在 ObjectAnimator 上追加 setInterpolator(new OvershootInterpolator()) 做出回彈手感,或用 ViewPropertyAnimator 串聯多個屬性形成更豐富的運動軌跡。

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