編輯: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 反查物件、進行資料溝通,全靠這個。
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 等)——它就是那些屬性傳進程式碼的通道。
根據 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 中完成。
三點都顧到,控制項與遊戲畫面就能在同一個視圖上共存,並透過 static 變數或共用的資料物件雙向溝通。
教你開發一個簡單的 Android 五子棋遊戲
範例目標 這個範例實作一個可操作的 Android 五子棋小遊戲,包含棋盤、落子、勝負判斷與重新開局。目前未加入電腦 AI,但已具備完整的基本遊玩流程。 操作方式 遊戲
Android 遊戲開發教學(十):飛行射擊遊戲實作
1. 地圖滾動原理 飛行射擊遊戲常見的背景效果,是讓玩家感覺飛機持續向前飛行。其原理與搭火車時的錯覺類似:當背景圖片不斷向後移動,玩家就會以為自己的飛機正在前進。 實作上
Android遊戲開發實踐之NDK與JNI開發(01):JNI基礎與開發流程
NDK是Native Development Kit的縮寫,顧名思義,它是Google提供的一套讓原生Java程式碼與本地C/C++程式碼「互通」的開發工具集。Andro
Android 遊戲開發 8:MediaPlayer 與 SoundPool 播播音訊的完整比較與實作
遊戲沒有音效,體感會直接掉一個檔次。Android 裡能播聲音的 API 主要有 MediaPlayer、SoundPool 與 JetPlayer 三個;JetPlay