在SurfaceView中添加系统控件,并且相互交互数据!

转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/android-game/306.html

          ☞ 点击订阅 ☜
 本博客最新动态!及时将最新博文通知您!

———————————————————————

多童鞋说我的代码运行后,点击home或者back后会程序异常,如果你也这样遇到过,那么你肯定没有仔细读完Himi的博文,第十九篇Himi专门写了关于这些错误的原因和解决方法,这里我在博客都补充说明下,省的童鞋们总疑惑这一块;请点击下面联系进入阅读:

【Android游戏开发十九】(必看篇)SurfaceView运行机制详解—剖析Back与Home按键及切入后台等异常处理! 

——————————————————————-

昨天圣诞节,没有出去,而是一天时间全部纠结在如何在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 >

以上代码很简单,都是一些布局方式和各个组件一些属性及显示方式的设定,当然主要看如何对我们的SurfaceView如何注册在xml中的,那么每个组件都有id这样为了对后面其交互数据用到,因为我们要对每个组件操作,所以这里都索引了id方面从R文件中取出其对象。

那么,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被触发" );
         }
     }
}

该有的备注在代码后面都备注了,MySurfaceView.button_str,这个是自己的SurfaceView中定义的一个static 的变量用来交互数据用到;在那么下面就要看我们的SurfaceView,当在Xml注册需要注意什么了,我半天的时候都花在了这里!!!一定要引起注意,这也是在SurfaceView中并显示组件完成最重要的一步。

先分析:

1.SurfaceView类的创建和实现等等和之前都是一样的,该怎么去写还怎么去写,但是!构造函数一定要注意!

1
2
3
4
/*
      * 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中去绑定,这里需要注意下;

本章源码下载: ”SurfaceView添加组件且交互数据.rar”        下载地址:  http://vdisk.weibo.com/s/hq3c0

 

我们已经可以在同一界面中既显示我们的surfaceview和button、textview等组件,那么基本算是成功了,但是身为游戏开发人员,如果不是想故意要这种类似电影形式的展现方式(我们的surfaceview在中间 – -.很想播放电影似的。。),是绝对不允许游戏的分量只是占了中间那么一部分,肯定需要全屏显示,别的组件只是一个配角的角色。那么下面先上一张截图看下为什么修改与调整。

(图1):

 

看到我们画出来的字体了吧,很悲剧被覆盖了!只要有button就会有一块长条,即使我们修改button中布局的颜色也只是把长条的颜色变成白色,当然好看是好看了,但是仍旧遮挡我们的字体!这可不是我们想要的结果。我们想要的效果应该是下图这样的:

(图2):

娃哈哈,这效果就对啦,我们的view占满全屏,而组件本身才会对我们的view中的内容有遮挡,不会多出一些无用的长条遮挡….

当时虽然想的方法就是布局xml的问题,我一开始想在我们xml中定义的surfaceview中直接添加按钮,但是view不能添加view!所以没办法,就想到是否是布局的问题。经过多次尝试才终于成功做到。

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
<? 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"
     >
     < RelativeLayout
             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" />
         < Button
          android:layout_width = "wrap_content"
                 android:layout_height = "wrap_content"
                 android:layout_alignParentBottom = "true"
                 android:text = "Himi Button_1"
                  android:id = "@+id/button1" /> 
 
         < Button android:layout_width = "wrap_content"
                 android:layout_height = "wrap_content"
                 android:layout_alignParentBottom = "true"
                 android:layout_toRightOf = "@id/button1"
                 android:text = "Himi Button_2"
                   android:id = "@+id/button2" />
                      < 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" />
     </ RelativeLayout >
</ LinearLayout >

xml 修改的不大,主要将之前的线性布局改成了相对布局。虽然改动不大,但是也真的费了不少时间去调整、这样一来大家就可以在自己的游戏Surfaceview中随意添加组件啦,娃哈哈~~~

源码在上一篇已经给出下载地址,这里也只是对xml的修改大家需要可以去下载上一篇的源码,将xml调整即可、

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值