在 SurfaceView 上设置背景



我正在使用SurfaceView开发一个Android绘图应用程序(更具体地说,它是一个扩展SurfaceView的类)。我已经在这个论坛上看到了很多主题,但我没有得到答案.

我的活动基本上是一个FrameLayout,其中包含我所有的观点。我想将SurfaceView设置在FrameLayout的最低级别,以便查看上部元素(按钮、片段等)。我还想用可绘制对象设置SurfaceView的背景,但我遇到了问题.

我首先尝试设置SurfaceView本身的背景,它可以工作。不幸的是,我的绘画内容(画布和位图)被这个背景超载了,所以我尝试了第二种解决方案。我将可绘制背景应用于我的FrameLayout,并使用setZOrderOnTopSurfaceView背景设置为透明。我的SurfaceView确实是透明的,但我的绘图内容在我的按钮和碎片上方.

所以我的第一个问题是:为什么我们需要setZOrderOnTop才能获得透明的背景?那么,如何设置一个简单的可绘制背景,保持结构层次结构?

感谢您的回答!

XML 视图:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/repeat_background"
    tools:context=".MyActivity">
    <!-- Option menu -->
    <fragment ... />
    <Button ... />
    <Button ... />
    <Button ... />
    ...
</FrameLayout>

@drawable/repeat_background

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/drawing_font_texture"
    android:tileMode="repeat" />

我的活动

@Override
protected void onCreate(Bundle p_savedInstanceState) {
    super.onCreate(p_savedInstanceState);
    setContentView(R.layout.main_controller_activity);
    // Getting my FrameLayout
    FrameLayout mainFrame   = (FrameLayout) findViewById(R.id.mainFrame);
    // Instantiating my SurfaceView
    this.drawableView       = new MCustomDrawableView(getApplicationContext());
    // Don't works
    //setZOrderMediaOverlay(true);
    this.drawableView.setZOrderOnTop(true);
    this.drawableView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    FrameLayout.LayoutParams style = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT);

    // Add the SurfaceView to the FrameLayout, on lowest position
    mainFrame.addView(this.drawableView, 0, style);
}

我认为您的解决方案是正确的。将背景设置为框架布局,或在曲面视图下为可绘制对象添加其他布局。然后增加表面视图的 z 索引和应该在表面视图上方的按钮/元素的 z 索引,否则它们位于其下方。如果使用材质设计,还可以以 dp 为单位设置高程以获得漂亮的阴影。

最新更新