Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android游戲 >> Android游戲開發 >> Android游戲開發6:SurfaceView中添加組件並進行數據交互的方法

Android游戲開發6:SurfaceView中添加組件並進行數據交互的方法

編輯:Android游戲開發

       SurfaceView中如何添加系統組件,如我們經常會用到的EditText、TextView、Button等,讓很多朋友非常糾結。網上常見到有童鞋在尋找此問題的答案。本文就和各位分享下SurfaceView中添加組件的方法。

       1、因為我們的SurfaceView是個View,對於添加的組件其實也是View,如果我們只是一味的想在SurfaceView中添加View組件其實是錯誤的思想,當然我一開始也是想著直接在SurfaceView中定義或者去使用組件,但是結果肯定是不成功的,因為View中不能添加View

       2、既然第一條肯定是錯誤的,那麼我們就應該想到把我們的SurfaceView和組件都放在一個Layout裡面,畢竟我們的的SurfaceView也是一個view和其他組件一同放在我們的layout裡,那麼這樣一來肯定就能完成在SurfaceView中添加組件的目的啦。下面先上截圖:

Android游戲開發6:SurfaceView中添加組件並進行數據交互的方法

       大家看到中間白色區域就是我們的SurfaceView啦,最上方是組件TextView,最下方是Button,對的,要的就是這個效果!而不是像前面文章中多個Activity切換,這樣都在一個界面中啦。下面貼相關代碼。

       先放上Xml 代碼:

XML/HTML代碼
  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類中代碼:

Java代碼
  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.     }   
  49. }  

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

       先分析:

       1、SurfaceView類的創建和實現等等和之前都是一樣的,該怎麼去寫還怎麼去寫,但是!構造函數一定要注意!

Java代碼
  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就必須在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

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved