在项目中需要实现这样的功能,SurfaceView提供绘图功能,同时在上面还要求能够绘制控件。上网搜索了一番资料,无意中看到ApiDemos中的SurfaceView Overlay例子,发现这正是我所需要的。看看SurfaceView Overlay的布局文件:
<!-- Here is where we put the SurfaceView, in a frame so that we can
stack other views on top of it. -->
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1">
<android.opengl.GLSurfaceView android:id="@+id/glsurfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout android:id="@+id/hidecontainer"
android:orientation="vertical"
android:visibility="gone"
android:background="@drawable/translucent_background"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/hideme1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:text="@string/hide_me"/>
<Button android:id="@+id/hideme2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:text="@string/hide_me"/>
</LinearLayout>
</FrameLayout>
布局中采用了FrameLayout,这样GLSurfaceView和LinearLayout就可以同时显示,通过设置LinearLayout的background为透明就可以达到GLSurfaceView和控件叠加显示的效果了。
但是在复制代码到我的工程中时,发现
android:background="@drawable/translucent_background"
这一句总是提示找不到资源。确实,在ApiDemos的drawable目录下也找不到translucent_background资源,那这个"@drawable/translucent_background"又是定义在哪里呢?原来在colors.xml中,在colors.xml中有这样的定义:
<drawable name="translucent_background">#e0000000</drawable>
<drawable name="transparent_background">#00000000</drawable>
看来drawable资源并非都在res/drawable目录下啊。
例子中给的是半透明的效果,如果要透明效果,将translucent_background换成transparent_background即可。