Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 遊戲 >> Android 遊戲開發

Android 遊戲開發 6:在 SurfaceView 上疊加控制項並完成資料雙向溝通

編輯:Android 遊戲開發

「怎麼在 SurfaceView 上面放 EditText、TextView、Button?」這個問題在論壇上流傳了很久。許多人一開始都卡在完全不同的地方,所以先把結論講清楚:不能放進去,只能疊在上面。

為什麼直覺的做法行不通

SurfaceView 本身是一個 View,而 Button、TextView 也都是 View。直覺上我們會想「把子 View 塞進父 View 裡」,但 Android 的 View 並不是容器——View 裡面不能再放 View。能裝子元件的只有 ViewGroup 這類 Layout。我最初也執著於在 SurfaceView 內部宣告控制項,試了很久都失敗,原因就在這裡。

正確思路是:把 SurfaceView 和那些控制項,一起放進同一個 Layout 裡,讓它們成為同層的兄弟節點。SurfaceView 既然也是一個 View,它和 Button 平起平坐地待在 Layout 中,自然就能同時顯示在同一個畫面上。

成品如下:

中間白色區域是 SurfaceView 的繪圖區,上方是 TextView,下方並排兩個 Button。全部在同一個 Activity 裡,而不是像前面幾節那樣靠切換 Activity 來呈現。

版面配置

這裡用 FrameLayout 來承載 SurfaceView,上下各一個 LinearLayout 放控制項,最外層再用垂直的 LinearLayout 把三段組起來。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <TextView
            android:id="@+id/textview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="This is Himi"
            android:textSize="32sp"
            android:textColor="#00FF00"
            android:gravity="center_horizontal" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <com.himi.MySurfaceView
            android:id="@+id/view3d"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Himi Button_1"
            android:id="@+id/button1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Himi Button_2"
            android:id="@+id/button2" />
    </LinearLayout>
</LinearLayout>

這段 XML 本身沒什麼學問,都是常見的排列與屬性設定。真正的重點有兩個:一是自訂的 com.himi.MySurfaceView 是怎麼被寫進 XML 的,二是每個控制項都必須有 id——後續要透過 R.id 反查物件、進行資料溝通,全靠這個。

Activity 端:取得控制項並綁定事件

public class MainActivity extends Activity implements OnClickListener {

    private Button button1, button2;
    private TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);   // 隱藏標題列
        // 這行必須寫在 setContentView 之前,否則會拋例外
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);   // 先顯示,之後才取得並操作其中的元件

        tv = (TextView) findViewById(R.id.textview);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);

        // 也可以不用介面,改用匿名內部類:
        // button2.setOnClickListener(new OnClickListener() {
        //     @Override
        //     public void onClick(View v) { /* 在這裡處理 */ }
        // });
    }

    @Override
    public void onClick(View v) {
        if (v == button1) {
            MySurfaceView.button_str = "button 1 被觸發";
            tv.setText("button 1 被觸發");
        } else if (v == button2) {
            MySurfaceView.button_str = "button 2 被觸發";
            tv.setText("button 2 被觸發");
        }
    }
}

MySurfaceView.button_str 是 SurfaceView 中定義的一個 static 變數,充當 Activity 與繪圖執行緒之間傳遞訊息的管道。按下按鈕後,Activity 更新這個變數,SurfaceView 的繪圖迴圈下一幀讀取它,就能畫出對應的變化。

最容易卡住的一步:建構子

SurfaceView 的內部邏輯和之前幾節完全一樣,該怎麼寫就怎麼寫,唯一必須改的是建構子。這一步我卡了整個下午,最後是在群友提醒下才找到問題所在。

/*
 * 一個參數的建構子:用 new 直接建立時完全沒問題
 */
// public MySurfaceView(Context context) { super(context); }

/*
 * 兩個參數的建構子:在 XML 中註冊時必須使用這個
 */
public MySurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // ... 其餘初始化
}

原因很直接:當我們把自訂的 SurfaceView 當成一個元件寫進 XML,XML 在解析並實例化它的時候,呼叫的就是帶有 AttributeSet 參數的那個建構子。如果你只寫了一個參數的版本,執行時就會因為找不到對應的建構方法而崩潰。而 AttributeSet 這個參數承載的,正是你在 XML 上為這個元件設定的屬性(寬高、id 等)——它就是那些屬性傳進程式碼的通道。

為什麼事件綁定要寫在 Activity

根據 XML 中設定的 id,理論上在 Activity 和 SurfaceView 裡都能用 R.id 反查到 Button。但如果把 setOnClickListener() 寫在 SurfaceView 裡,一定會出錯。

關鍵在於階層關係:XML 裡的 SurfaceView 與 Button 是同層的兄弟,Button 並沒有被包在 SurfaceView 內部。所以即使在 SurfaceView 中能查到 Button 的 id,當你嘗試綁定事件時,系統在這個 View 的子樹中找不到對應的實例。真正把 Button 掛上畫面的是 Activity 裡那句 setContentView(R.layout.main),因此綁定動作也只能在 Activity 中完成。

整理一下三個步驟

  1. 把 SurfaceView 當成一個普通元件,與其他控制項一起放進 Layout(排列方式自由發揮)。
  2. SurfaceView 一定要使用帶兩個參數的建構子。除了這一點,其餘邏輯照舊。
  3. 按鈕的事件綁定寫在 Activity,不要寫在 SurfaceView 裡。

三點都顧到,控制項與遊戲畫面就能在同一個視圖上共存,並透過 static 變數或共用的資料物件雙向溝通。

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