【Android遊戲開發之六】在SurfaceView中添加組件!!!!而且相互交互數據!!!!

本站文章均爲 李華明Himi 原創,轉載務必在明顯處註明:
轉載自【黑米GameDev街區】 原文連接: http://www.himigame.com/android-game/306.html
html

  

    各位童鞋請大家注意: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

  
  
           
  
  
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.    <LinearLayout 
  8.             android:orientation="horizontal" 
  9.             android:layout_width="wrap_content" 
  10.             android:layout_height="wrap_content" 
  11.             android:layout_gravity="center"> 
  12.               
  13.     <TextView   
  14.             android:id="@+id/textview" 
  15.             android:layout_width="fill_parent" 
  16.             android:layout_height="fill_parent" 
  17.             android:text="This is Himi" 
  18.             android:textSize="32sp"   
  19.             android:textColor="#00FF00" 
  20.             android:gravity="center_horizontal"/>   
  21.            
  22.     </LinearLayout> 
  23.         
  24.     <FrameLayout 
  25.             android:layout_width="fill_parent" 
  26.             android:layout_height="wrap_content" 
  27.             android:layout_weight="1" > 
  28.     <com.himi.MySurfaceView android:id="@+id/view3d" 
  29.             android:layout_width="fill_parent" 
  30.             android:layout_height="fill_parent"/>                  
  31.     </FrameLayout>     
  32.      
  33.     <LinearLayout 
  34.             android:orientation="horizontal" 
  35.             android:layout_width="wrap_content" 
  36.             android:layout_height="wrap_content" 
  37.             android:layout_gravity="center"> 
  38.         <Button 
  39.          
  40.          android:layout_width="wrap_content" 
  41.                 android:layout_height="wrap_content" 
  42.                 android:text="Himi Button_1" 
  43.                  android:id="@+id/button1"/> 
  44.       
  45.         <Button android:layout_width="wrap_content" 
  46.                 android:layout_height="wrap_content" 
  47.                 android:text="Himi Button_2" 
  48.                   android:id="@+id/button2"/> 
  49.      </LinearLayout> 
  50. </LinearLayout> 

    以上代碼很簡單,都是一些佈局方式和各個組件一些屬性及顯示方式的設定,固然主要看如何對咱們的SurfaceView如何註冊在xml中的,那麼每一個組件都有id這樣爲了對後面其交互數據用到,由於咱們要對每一個組件操做,因此這裏都索引了id方面從R文件中取出其對象。線程

     那麼,xml咱們定義好了,看看代碼中如何實現的,這裏先說下Activity類中代碼: 3d

  
  
           
  
  
  1. package com.himi;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.Window;  
  6. import android.view.WindowManager;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10. public class MainActivity extends Activity implements OnClickListener {  
  11.     /** Called when the activity is first created. */  
  12.     private Button button1, button2;  
  13.     private TextView tv ;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         this.requestWindowFeature(Window.FEATURE_NO_TITLE);//隱去標題(應用的名字)  
  18.         //此設定必需要寫在setContentView以前,不然會有異常)  
  19.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  20.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  21.         setContentView(R.layout.main); //要先顯示,而後再對其組件取出、處理操做  
  22.         tv=(TextView)findViewById(R.id.textview);   
  23.         button1 = (Button) findViewById(R.id.button1);  
  24.         button1.setOnClickListener(this);//這裏是監聽按鍵,由於本類使用了OnClickListener接口  
  25.         button2 = (Button) findViewById(R.id.button2);  
  26.         button2.setOnClickListener(this);   
  27.         /* 其實你們也能夠不用本類使用接口,能夠內部類來完成。  
  28.          * 如下是不使用OnClickListener接口的綁定監聽方式;  
  29.         button2.setOnClickListener(new OnClickListener() {  
  30.               
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 //這裏處理按鍵操做  
  34.                   
  35.             }  
  36.         });  
  37.         */   
  38.     }   
  39.     @Override  
  40.     public void onClick(View v) {  
  41.         if (v == button1) {  
  42.             MySurfaceView.button_str = "button 1被觸發";  
  43.             tv.setText("button 1被觸發");  
  44.         } else if (v == button2) {  
  45.             MySurfaceView.button_str = "button 2被觸發";  
  46.             tv.setText("button 2被觸發");  
  47.         }  
  48.     }  

    該有的備註在代碼後面都備註了,MySurfaceView.button_str,這個是本身的SurfaceView中定義的一個static 的變量用來交互數據用到;在那麼下面就要看咱們的SurfaceView,當在Xml註冊須要注意什麼了,我半天的時候都花在了這裏!!!必定要引發注意,這也是在SurfaceView中並顯示組件完成最重要的一步。 

先分析:

    1.SurfaceView類的建立和實現等等和以前都是同樣的,該怎麼去寫還怎麼去寫,可是!構造函數必定要注意! 

  
  
           
  
  
  1. /*  
  2.      * public MySurfaceView(Context context) { super(context); }//備註1(這裏必定要引發注意,仔細看下文對備註1的解釋 )  
  3.      */  
  4. 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

(推薦你們訂閱本博客,由於咱的更新速度但是很快的~娃哈哈)

相關文章
相關標籤/搜索