【发布时间】:2015-01-15 08:47:28
【问题描述】:
我正在开发一个 Android 绘图应用程序,使用 SurfaceView(更具体地说,它是一个扩展 SurfaceView 的类)。我已经在这个论坛上看到了很多主题,但我没有得到我的答案。
我的活动基本上是一个FrameLayout,其中包含我所有的观点。我想将我的SurfaceView 设置在我的FrameLayout 的最低级别,以便查看上面的元素(按钮、片段等)。我还想用可绘制对象设置我的 SurfaceView 的背景,但我遇到了问题。
我首先尝试设置我的SurfaceView 本身的背景并且它可以工作。不幸的是,我的绘画内容(画布和位图)在此背景下超载,因此我尝试了第二种解决方案。我将可绘制背景应用到我的FrameLayout,并使用setZOrderOnTop 将我的SurfaceView 背景设置为透明。我的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);
}
【问题讨论】:
-
SurfaceView 有两个部分,“Surface”和“View”。 View 部分应该只是一个用于布局的透明区域。 Surface 是一个完全独立的层,位于所有 View 元素的上方或下方。如果这不是您想要的,只需使用自定义视图——它会更好地与其他视图混合,并且画布渲染可能会被硬件加速。 developer.android.com/training/custom-views/index.html
-
感谢您的解释。
标签: android background surfaceview android-framelayout