如何使surfaceview透明

Sun*_*dey 81 android surfaceview

大家好我想让我的DrawingSurface视图透明.我试了很多东西,但它不起作用.

这是我的表面视图透明的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/icon" >
        </ImageView>

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#00000000" >

            <codewalla.android.drawings.DrawingSurface
                android:id="@+id/drawingSurface"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >
            </codewalla.android.drawings.DrawingSurface>
        </LinearLayout>
    </FrameLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/colorRedBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="R" />

        <Button
            android:id="@+id/colorBlueBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="G" />

        <Button
            android:id="@+id/colorGreenBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="B" />

        <Button
            android:id="@+id/undoBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="U" />

        <Button
            android:id="@+id/redoBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:onClick="onClick"
            android:text="R" />
    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

小智 208

在构造函数中尝试这个:

SurfaceView sfvTrack = (SurfaceView)findViewById(R.id.sfvTrack);
sfvTrack.setZOrderOnTop(true);    // necessary
SurfaceHolder sfhTrackHolder = sfvTrack.getHolder();
sfhTrackHolder.setFormat(PixelFormat.TRANSPARENT);
Run Code Online (Sandbox Code Playgroud)

  • 这很好用,除了setZOrderOnTop()意味着你不能在这个SurfaceView上放置任何图形,这是不幸的.= \ (40认同)
  • 哇,我正在努力,已经在XML中添加了透明像素格式和透明背景.事实证明'setZOrderOnTop(true)'调用是关键的.感谢发布这个,我从来没有尝试过. (8认同)
  • 而已!这应该收到答案标签! (4认同)
  • 但它涵盖了布局上的所有其他视图,这意味着如果我在表面视图上绘制,那么任何图像/按钮都将被绘制.我怎么能摆脱这个问题? (2认同)
  • Grafika中的"多表面测试"演示了与View UI图层混合的三个透明重叠SurfaceView.https://github.com/google/grafika/blob/master/src/com/android/grafika/MultiSurfaceActivity.java.FWIW,"在构造函数中尝试这个"应该读作"在onCreate()`中试试这个",并且将像素格式从RGB565更改为RGB888也是非常必要的. (2认同)

Mik*_*erg 6

sfvTrack.setZOrderOnTop(true); 
Run Code Online (Sandbox Code Playgroud)

将surfaceView放在窗口顶部,这意味着您无法在surfaceView顶部添加项目.

如果要在曲面视图的顶部添加视图; 你应该考虑使用:

setZOrderMediaOverlay(true);
Run Code Online (Sandbox Code Playgroud)

代替.这会将此surfaceView放在其他surfaceViews的顶部,但仍然在窗口后面,允许您在曲面视图的顶部添加其他可见视图.


slu*_*und 5

是你的 DrawingSurface 和 SurfaceView 的扩展吗?

没有办法让 SurfaceView 做你想要它做的事情。表面视图的机制是这样的,它后面不能有任何可见的东西。(请参阅此处的答案http://groups.google.com/group/android-developers/browse_thread/thread/8d88ef9bb22da574

我使用扩展 SurfaceView 的自定义视图测试了您的层次结构,我看到了您的问题。如果我切换到一个简单地扩展 View 的自定义视图,我可以获得透明度。


VJ *_*ons 5

TexttureView我相信会比 更好地满足您的需求SurfaceView