本站文章均爲 李華明Himi 原創,轉載務必在明顯處註明:
轉載自【黑米GameDev街區】 原文連接: http://www.himigame.com/android-game/306.htmlhtml
各位童鞋請大家注意:surfaceview中確實有 onDraw這個方法,可是surfaceview不會本身去調用!!!android
而我代碼中的ondraw 也好 draw 也好,都是我本身定義的一個方法。。。放在線程中不斷調用的,必定要注意!! app
昨天聖誕節,沒有出去,而是一天時間所有糾結在如何在SurfaceView中添加組件,例如添加經常使用的Button,TextView等等、一開始也想着從網上找些資料看看有沒有可參考的,可是發現搜到的結果還是些童鞋對此很疑惑而且也在找尋答案,那麼,這裏就把聖誕節一天的成果來和各位童鞋分享; ide
1.由於咱們的SurfaceView是個View對於添加的組件其實也是View,若是咱們只是一味的想在SurfaceView中添加View組件實際上是錯誤的思想,固然我一開始也是想着直接在SurfaceView中定義或者去使用組件,可是結果確定是不成功的,由於View不能添加View! 函數
2.既然第一條確定是錯誤的,那麼咱們就應該想到把咱們的SurfaceView和組件都放在一個Layout裏面,畢竟咱們的的SurfaceView也是一個view和其餘組件一同放在咱們的layout裏,那麼這樣一來確定就能完成在SurfaceView中添加組件的目的啦。下面先上截圖 佈局
你們看到中間白色區域就是咱們的SurfaceView啦,最上方是組件TextView ,最下方是Button 、對的,要的就是這個效果!而不是像前面文章中多個Activity切換,這樣都在一個界面中啦。哇哈哈啊。好、下面來看代碼吧:this
先放上Xml 代碼: spa
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://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>
以上代碼很簡單,都是一些佈局方式和各個組件一些屬性及顯示方式的設定,固然主要看如何對咱們的SurfaceView如何註冊在xml中的,那麼每一個組件都有id這樣爲了對後面其交互數據用到,由於咱們要對每一個組件操做,因此這裏都索引了id方面從R文件中取出其對象。線程
那麼,xml咱們定義好了,看看代碼中如何實現的,這裏先說下Activity類中代碼: 3d
- package com.himi;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity implements OnClickListener {
- /** Called when the activity is first created. */
- 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);//這裏是監聽按鍵,由於本類使用了OnClickListener接口
- button2 = (Button) findViewById(R.id.button2);
- button2.setOnClickListener(this);
- /* 其實你們也能夠不用本類使用接口,能夠內部類來完成。
- * 如下是不使用OnClickListener接口的綁定監聽方式;
- 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 的變量用來交互數據用到;在那麼下面就要看咱們的SurfaceView,當在Xml註冊須要注意什麼了,我半天的時候都花在了這裏!!!必定要引發注意,這也是在SurfaceView中並顯示組件完成最重要的一步。
先分析:
1.SurfaceView類的建立和實現等等和以前都是同樣的,該怎麼去寫還怎麼去寫,可是!構造函數必定要注意!
- /*
- * public MySurfaceView(Context context) { super(context); }//備註1(這裏必定要引發注意,仔細看下文對備註1的解釋 )
- */
- public MySurfaceView(Context context, AttributeSet attrs) {//備註1}
這裏解釋下備註1: 這裏有兩個構造函數,固然咱們用哪一個都是能夠的,可是在此時咱們須要明確咱們到底要使用哪一個。
一個參數的構造函數:若是是new出來的此類實例確定是沒有問題,可是咱們爲了能在顯示SurfaceView同時顯示別的組件,因此把自定義的SurfaceView也看成組件註冊在了main——xml中,因此這裏須要注意,當在xml中註冊的就必須在SurfaceView中使用這種含有兩個參數的構造函數的方法, xml初始化的時候會調用兩個參數的這個構造方法, (當時這個問題困擾了半天的研究時間,最後在一個羣友的幫助下才發現是這裏出了問題) 那麼含有兩個構造參數的方法裏第二個參數指的自定義的組件的一些屬性,就像長寬同樣,你能夠給組件屬性,就是經過這個來傳遞的!
那麼在SurfaceView 中並一同顯示組件也就到底完結了,回顧下,一共分爲3步,1.將咱們的SurfaceView 做爲一個組件view 和其餘組件一同放置到佈局中,固然佈局的方式和顯示的方式你們本身隨本身喜歡定義! 2.在咱們的SurfaceView中必定要使用兩個構造函數的構造函數,必定!必定! 就這裏有區別,別的仍是該怎麼處理就怎麼處理,就是構造函數換了 3.交互數據,對其按鍵的綁定在 activity中完成,別把view綁定在我們的SurfaceView中啊,不然報錯- -、
這裏說下爲何要在activity中去綁定按鍵處理 而不是在咱們的surfaceview中去綁定:
其實根據xml中定義button時的id 咱們能夠經過R.id 索引取到button,無論在activity中仍是咱們的surfaceview中均可以取到,可是!綁定button這一步若是在 surfaceview中去寫就必定報錯,緣由我解釋下;
咱們在xml中定義咱們的surfaceview 和 組件button、textview等等的時候 他們是同一級別的!!而不是把button包含在 surfaceview裏,因此雖然在surfaceview中能夠根據id索引到button但綁定的時候是沒法找到button的,只有咱們的activitysetContentView(R.layout.main); 顯示的button,因此只能在顯示它的activity中去綁定,這裏須要注意下;
下面分享出源碼:
×××地址: http://www.himigame.com/android-game/306.html
(推薦你們訂閱本博客,由於咱的更新速度但是很快的~娃哈哈)