【发布时间】:2014-03-09 10:25:15
【问题描述】:
我想在 Android CameraPreview 上添加按钮。 基本上层次结构如下:
- 活动
- 视图组
- 表面视图
- 视图组
ViewGroup 是 SurfaceView 的包装器并以 CameraPreview 为中心。
我当前在 Activity.java 中的代码正在运行(没有按钮):
mPreview = new Preview(this, mCamera, mJavaDetector);
setContentView(mPreview);
现在我已经创建了布局并想在那里放置一个按钮。 我的布局 main.xml 如下所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout">
<LinearLayout
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:visibility="visible"
android:layout_alignParentTop="true">
</LinearLayout>
<Button android:text="Freeze" android:id="@+id/someButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</RelativeLayout>
我已经改变了Activity.java:
setContentView(R.layout.main);
mPreview = new Preview(this, mCamera, mJavaDetector);
LinearLayout ll = (LinearLayout)findViewById(R.id.preview);
if(ll != null)
{
ll.addView(mPreview);
}
并在 Preview.java 中创建了 SurfaceView
mCameraPreview = new CameraPreview(context, camera, mJavaDetector);
addView(mCameraPreview);
现在屏幕上只有按钮,没有 CameraPreview。你能告诉我我做错了什么吗?谢谢。
编辑:
假设我有这样的 main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<fit.vutbr.faceswap.Preview
android:id="@+id/preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<fit.vutbr.faceswap.CameraPreview
android:id="@+id/camerapreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</fit.vutbr.faceswap.CameraPreview>
</fit.vutbr.faceswap.Preview>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/switch_100px_white" />
</LinearLayout>
Activity.java 内部:
setContentView(R.layout.main);
mPreview = (Preview) findViewById(R.id.preview);
mPreview.setPreview(this, mCamera, mJavaDetector);
Preview.java 内部:
mCameraPreview = (CameraPreview) findViewById(R.id.camerapreview);
mCameraPreview.setCameraPreview(context, camera, mJavaDetector);
这里的错误在哪里?
EDIT2:
好的,事实证明,如果我像这样使用 main.xml,我可以看到surfaceView:
<?xml version="1.0" encoding="utf-8"?>
<fit.vutbr.faceswap.Preview
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- <ImageButton
android:id="@+id/switch_camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/switch_100px_white" />
-->
<fit.vutbr.faceswap.CameraPreview
android:id="@+id/camerapreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</fit.vutbr.faceswap.CameraPreview>
</fit.vutbr.faceswap.Preview>
我还发现 ViewGroup 在添加新视图时使用相反的顺序而不是其他布局,所以第一个添加的是最上面的。
现在,如果我取消注释 ImageButton,我会在整个屏幕上获得按钮,并且 SurfaceView 中的 surfaceCreated() 方法永远不会被调用。
【问题讨论】:
-
所以您只希望 UI 元素位于
SurfaceView之上?像youtube.com/watch?v=kH9kCP2T5Gg#t=28 之类的东西(你可以看到几个TextViews 覆盖在左上角的相机预览以及微调器上)? -
是的,这正是我正在寻找的(确切地说,我希望 ImageButton 在表面上)。但问题是 ViewGroup 位于 SurfaceView 和 Activity 之间。事实证明,需要以与 *Layout 中相反的顺序向 ViewGroup 添加元素(首先添加的是最上面的)但是现在按钮覆盖了整个屏幕并且不调用 surfaceCreated()。
标签: android android-layout surfaceview viewgroup