如何使surfaceview透明

时间:2011-03-22 12:19:13

标签: 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>

4 个答案:

答案 0 :(得分:208)

在构造函数中尝试:

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

答案 1 :(得分:6)

sfvTrack.setZOrderOnTop(true); 

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

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

setZOrderMediaOverlay(true);

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

答案 2 :(得分:4)

您的DrawingSurface和SurfaceView的扩展名吗?

无法让SurfaceView执行您希望它执行的操作。表面视图的机制使得它后面不能有任何可见的东西。 (参见这里的答案http://groups.google.com/group/android-developers/browse_thread/thread/8d88ef9bb22da574

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

答案 3 :(得分:4)

TexttureViewSurfaceView更能满足您的需求,我相信。

相关问题