各位童鞋請你們注意:surfaceview中確實有 onDraw這個方法,但是surfaceview不會自己去調用!!!
而我代碼中的ondraw
也好
draw 也好,都是我自己定義的一個方法。。。放在執行緒中不斷調用的,一定要注意!!
一天時間全部糾結在如何在SurfaceView中添加元件,例如添加常用的Button,TextView等等、一開始也想著從網上找些資料看看有沒有可參考的,但是發現搜到的結果仍是些童鞋對此很疑惑並且也在找尋答案,那麼,這裡就把耶誕節一天的成果來和各位童鞋分享;
1.因為我們的SurfaceView是個View對於添加的元件其實也是View,如果我們只是一味的想在SurfaceView中添加View元件其實是錯誤的思想,當然我一開始也是想著直接在SurfaceView中定義或者去使用元件,但是結果肯定是不成功的,因為View不能添加View!
2.既然第一條肯定是錯誤的,那麼我們就應該想到把我們的SurfaceView和元件都放在一個Layout裡面,畢竟我們的的SurfaceView也是一個view和其他元件一同放在我們的layout裡,那麼這樣一來肯定就能完成在SurfaceView中添加元件的目的啦。先上截圖、
大家看到中間白色區域就是我們的SurfaceView啦,最上方是組件TextView ,最下方是Button 、對的,要的就是這個效果!而不是像前面文章中多個Activity切換,這樣都在一個介面中啦。哇哈哈啊。好、下面來看代碼吧:
先放上Xml 代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?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>
|
那麼,xml我們定義好了,看看代碼中如何實現的,這裡先說下Activity類中代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
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被触发");
}
}
}
|
先分析:
1.SurfaceView類的創建和實現等等和之前都是一樣的,該怎麼去寫還怎麼去寫,但是!構造函數一定要注意 !
1
2
3
4
|
/*
* public MySurfaceView(Context context) { super(context); }//备注1(这里一定要引起注意,仔细看下文对备注1的解释 )
*/
public MySurfaceView(Context context, AttributeSet attrs) {//备注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中去綁定,這裡需要注意下;
沒有留言:
張貼留言