【问题标题】:how to make surfaceview transparent如何使surfaceview透明
【发布时间】:2011-07-20 10:49:22
【问题描述】:

大家好,我想让我的 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>

【问题讨论】:

    标签: android surfaceview


    【解决方案1】:

    在构造函数中试试这个:

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

    【讨论】:

    • 哇,我正在努力解决这个问题,已经在 XML 中添加了透明像素格式和透明背景。事实证明,“setZOrderOnTop(true)”调用很关键。感谢您发布此内容,否则我永远不会尝试。
    • 这很好用,除了 setZOrderOnTop() 意味着你不能在这个 SurfaceView 上放置任何图形,这是不幸的。 =\
    • 但它涵盖了布局上的所有其他视图,这意味着如果我在表面视图上绘制,则将绘制任何图像/按钮。我怎样才能摆脱这个问题?
    • 在我身边试过。在 4.0 但 2.3 上运行良好。
    • Grafika 中的“多表面测试”演示了三个透明重叠的 SurfaceView 与 View UI 层混合。 github.com/google/grafika/blob/master/src/com/android/grafika/… 。 FWIW,“在构造函数中尝试这个”应该是“在 onCreate() 中尝试这个”,并且将像素格式从 RGB565 更改为 RGB888 也是非常必要的。
    【解决方案2】:
    sfvTrack.setZOrderOnTop(true); 
    

    会将surfaceView放在窗口的顶部,这意味着你不能在surfaceView的顶部添加项目。

    如果你想在表面视图之上添加视图;你应该考虑使用:

    setZOrderMediaOverlay(true);
    

    相反。这会将这个 surfaceView 置于其他 surfaceView 之上,但仍位于窗口之后,从而允许您在表面视图之上添加其他可见视图。

    【讨论】:

      【解决方案3】:

      是你的 DrawingSurface 和 SurfaceView 的扩展吗?

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

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

      【讨论】:

      • 感谢 slund,你是对的,但它需要把相机预览放在后面,它是我的绘图表面,所以我想在相机预览上绘图
      【解决方案4】:

      我相信TexttureView 会比SurfaceView 更适合您的需求。

      【讨论】:

      • 是的,前提是我们不需要早于 14 岁!
      猜你喜欢
      • 2012-01-13
      • 1970-01-01
      • 2011-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 2014-06-01
      • 1970-01-01
      相关资源
      最近更新 更多