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

在 Android 上打造 Metro 風格九宮格選單

編輯:關於 Android 程式設計

相信大家对 Metro 風格並不陌生,本文就來在 Android 平台上實現這樣一款九宮格選單的動態效果。

實現思路

核心在於利用動畫實現方塊移動的效果,這裡使用 TranslateAnimation 類別。先來看版面配置——透過多層 LinearLayout 與 RelativeLayout 嵌套,將九個色塊(1 到 9)以不同權重排列成不規則的九宮格:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#000000"
   android:orientation="vertical" >
   <!-- 第一層 -->
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1"
       android:orientation="horizontal" >
       <!-- 第一層 橫向左 -->
       <LinearLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight="1"
           android:orientation="vertical" >
           <LinearLayout
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_weight="1"
               android:orientation="horizontal" >
               <!-- 1 -->
               <RelativeLayout
                   android:id="@+id/nine_one"
                   android:layout_width="fill_parent"
                   android:layout_height="fill_parent"
                   android:layout_weight="1"
                   android:background="#FFFF00" >
               </RelativeLayout>
               <!-- 2(背景 #FFC0CB)-->
               <RelativeLayout android:id="@+id/nine_two" ... />
            </LinearLayout>
           <!-- 4(#EE30A7)、5(#EE4000)-->
           <RelativeLayout android:id="@+id/nine_four" ... />
           <RelativeLayout android:id="@+id/nine_five" ... />
       </LinearLayout>
       <!-- 第一層 橫向右 -->
       <LinearLayout
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:layout_weight="2"
           android:orientation="vertical" >
           <!-- 3(#FF8C69)、6(#8C8C8C)-->
           <RelativeLayout android:id="@+id/nine_three" ... />
           <RelativeLayout android:id="@+id/nine_six" ... />
       </LinearLayout>
   </LinearLayout>
   <!-- 第二層 -->
   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="3"
       android:baselineAligned="false"
       android:orientation="horizontal" >
       <!-- 7(#8B3E2F)、9(#A52A2A)-->
       <RelativeLayout android:id="@+id/nine_seven" ... />
       <RelativeLayout android:id="@+id/nine_nine" ... />
   </LinearLayout>
</LinearLayout>

它的靜態效果如下圖所示:

加入平移動畫

接著在 MainActivity 中,為每一個 Layout 套用 TranslateAnimation,讓方塊從螢幕外飛入,營造出 Metro 風格的動態進場效果。透過 DisplayMetrics 取得螢幕寬高,再分別設定右、下、左、上四個方向的平移動畫與各自不同的持續時間:

public class MainActivity extends Activity {
   private View viewNine;
   private LayoutInflater inflater;
   private RelativeLayout nine_one, nine_two, nine_three, nine_four,
       nine_five, nine_six, nine_seven, nine_nine;
   private TranslateAnimation myAnimation_Right, myAnimation_Bottom;
   private TranslateAnimation myAnimation_Left, myAnimation_Top;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_NO_TITLE);
       inflater = LayoutInflater.from(this);
       viewNine = inflater.inflate(R.layout.activity_main, null);
       nine_one = (RelativeLayout) viewNine.findViewById(R.id.nine_one);
       nine_two = (RelativeLayout) viewNine.findViewById(R.id.nine_two);
       nine_three = (RelativeLayout) viewNine.findViewById(R.id.nine_three);
       nine_four = (RelativeLayout) viewNine.findViewById(R.id.nine_four);
       nine_five = (RelativeLayout) viewNine.findViewById(R.id.nine_five);
       nine_six = (RelativeLayout) viewNine.findViewById(R.id.nine_six);
       nine_seven = (RelativeLayout) viewNine.findViewById(R.id.nine_seven);
       nine_nine = (RelativeLayout) viewNine.findViewById(R.id.nine_nine);
       setContentView(viewNine);
       nine_four.setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
               startActivity(new Intent(MainActivity.this, OneActivity.class));
           }
       });
       myAnimation();
       addAnimation();
   }
   // 啟動動畫
   private void addAnimation() {
       nine_one.startAnimation(myAnimation_Right);
       nine_two.startAnimation(myAnimation_Bottom);
       nine_three.startAnimation(myAnimation_Left);
       nine_four.startAnimation(myAnimation_Bottom);
       nine_five.startAnimation(myAnimation_Left);
       nine_six.startAnimation(myAnimation_Top);
       nine_seven.startAnimation(myAnimation_Left);
       nine_nine.startAnimation(myAnimation_Left);
   }
   // 動畫定義
   private void myAnimation() {
       DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
       int screenWidth = displayMetrics.widthPixels;
       int screenHeight = displayMetrics.heightPixels;
       myAnimation_Right = new TranslateAnimation(screenWidth, 0, 0, 0);
       myAnimation_Right.setDuration(1800);
       myAnimation_Bottom = new TranslateAnimation(0, 0, screenHeight, 0);
       myAnimation_Bottom.setDuration(1500);
       myAnimation_Left = new TranslateAnimation(-screenWidth, 0, 0, 0);
       myAnimation_Left.setDuration(2000);
       myAnimation_Top = new TranslateAnimation(0, 0, -screenHeight, 0);
       myAnimation_Top.setDuration(2500);
   }
}

透過為不同方塊指定不同的平移方向與動畫時長,九個色塊便會以富有層次感的方式依序飛入畫面,完成後即呈現出一個可點擊的 Metro 風格九宮格選單。

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