最近有一款Android平台下的遊戲很是火爆----2048。下面記錄一下開發過程。由於筆者是Android開發的初學者,所以希望藉以此文熟悉整個Android開發的流程。
首先建立Game2048的遊戲項目。我們選擇最低平台為Android4.0(API 14),最高支援平台Android4.4(API 19),然後一路Next,建立完成之後,我們修改activity_main.xml檔案。修改預設的布局方式為LinearLayout布局方式。然後我們在嵌套一個Linearyout布局,使用者遊戲分數的顯示。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/score" /> <TextView android:id="@+id/tvScore" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <GridLayout android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/gameView" ></GridLayout></LinearLayout>
然後我們在主包中建立遊戲主介面GameView類,由於我們使用GridLayout布局方式用來主介面的顯示,所以GameView類我們繼承GridLayout,並且建立全部的三個構造方法。然後在GameView類中,建立initGameView()作為遊戲的起始方法。然後找到主方法的路徑com.skk.game2048.GameView,在acitvity_main.xml檔案中綁定我們的主方法
<com.skk.game2048.GameView android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/gameView" ></com.skk.game2048.GameView>
主方法代碼
package com.skk.game2048;import android.content.Context;import android.util.AttributeSet;import android.widget.GridLayout;public class GameView extends GridLayout { public GameView(Context context) { super(context); initGameView(); } public GameView(Context context, AttributeSet attrs) { super(context, attrs); initGameView(); } public GameView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initGameView(); } private void initGameView(){ }}