最近有一款Android平臺下的遊戲非常火爆----2048。下面記錄一下開發過程。因爲筆者是Android開發的初學者,因此但願藉以此文熟悉整個Android開發的流程。java
首先建立Game2048的遊戲項目。咱們選擇最低平臺爲Android4.0(API 14),最高支持平臺Android4.4(API 19),而後一路Next,建立完成以後,咱們修改activity_main.xml文件。修改默認的佈局方式爲LinearLayout佈局方式。而後咱們在嵌套一個Linearyout佈局,用戶遊戲分數的顯示。android
<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文件中綁定咱們的主方法eclipse
<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(){ } }