SurfaceView Overlay研究

本文介绍如何在Android项目中实现在SurfaceView上叠加控件,通过使用FrameLayout作为容器,将SurfaceView和LinearLayout(包含Button)结合,并通过设置背景透明度实现叠加效果。在ApiDemos中找到的SurfaceView Overlay例子提供了灵感,需要注意的是drawable资源可能在colors.xml中定义,如translucent_background和transparent_background。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中需要实现这样的功能,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即可。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值