今天写游戏的时候碰到一个非常诡异的问题,之前我的游戏GameView是继承自View的,当是View时没有一点问题
但是后来为了能够让画面流畅,我就把View换成了带有双缓冲机制的SurfaceView了,这时候问题就出现了,原本显示在
SurfaceView上层的一个Button绘制的时候绘制不全了,如下图:
看图片左下方的一个方向按钮,显示不全了,这个方向按钮放在一个RelativeLayout中,该RelativeLayout放在SurfaceView上层。具体布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="#000000" > <com.oysb.rpg.GameView android:id="@+id/game_view" android:layout_width="fill_parent" android:layout_height="fill_parent"> </com.oysb.rpg.GameView> <RelativeLayout android:background="#01000000" android:id="@+id/relative_dir" android:layout_width="200px" android:layout_height="200px" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" > <ImageView android:layout_width="100px" android:layout_height="100px" android:layout_centerInParent="true" android:src="@drawable/btn_direction_bg" /> <ImageView android:id="@+id/img_dir" android:layout_width="100px" android:layout_height="100px" android:layout_centerInParent="true" android:src="@drawable/btn_direction" /> </RelativeLayout> </RelativeLayout>
百思不得其解为什么会少一块,后来发现为RelativeLayout加上一个background属性后发现图片显示全了,但是加上background属性后会使得游戏界面不美观,后来
无奈之下设置background="#01000000"设置一个肉眼看不出来的颜色,来达到来图片显示完全的目的。
这回方向控制按钮的图片显示完全了:
虽然搞不清楚确切的原因,但是我猜想这和surfaceview的机制有关,以前当游戏层用view时不会出现这种问题,我在想,是不是当View中含有surfaceView时是不是在
绘图的时候进行什么优化处理,优化的手段就是当前区域颜色为透明的时候就不写入屏幕的绘制缓冲区中,导致图片绘制不全?
以上博客只是记录一下问题,如果有哪位高人知道是什么原因,恳请指教~
作者在游戏中使用SurfaceView替换View后,发现上层Button显示不全。通过调整RelativeLayout背景颜色解决了问题,但原因不明。
503

被折叠的 条评论
为什么被折叠?



