0

我对 Java 很陌生,我正在尝试创建一个应用程序,您可以在其中使用陀螺仪在屏幕上移动图像,以使图像看起来像是粘在相机中的对象上。图像完成了,我让它动起来,就像它应该的那样。现在我只需要相机作为 xml 文件的背景。有什么简单的方法可以做到这一点吗?

4

2 回答 2

0

本质上,您想让相机预览 XML 文件中第一个 Layout 对象中的第一个元素。

您可以根据您希望如何布局其他控件来使用任何布局类型。我在这里使用 RelativeLayout 作为布局的基础。id 为“camera_preview”的 FrameLayout 用作相机预览类的容器。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".YourMainActivity" >

    <FrameLayout
        android:id="@+id/camera_preview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <!-- ... other display controls ... -->

</RelativeLayout>

然后,在 YourMainActivity 的 onCreate() 中创建相机预览类的新实例并将其添加到布局中的 camera_preview 容器中:

private static YourCameraPreviewClass mPreview;

@Override
protected void onCreate(Bundle savedInstanceState) {

    // ... get camera instance ...

    mPreview = new YourCameraPreviewClass( this, cameraInstance );  
    FrameLayout previewFrameLayout = (FrameLayout) findViewById( R.id.camera_preview );
    previewFrameLayout.addView( mPreview );

    Log.d( LOGTAG, "Camera preview background set!  (:" );

}
于 2013-04-27T13:53:33.537 回答
0

使用 FrameLayout 作为活动的父布局。然后放置一个 Surfaceview 以显示自定义相机。然后您可以在其上放置任何布局以显示其他组件。这看起来像背景中的相机。有关在 SufaceView 上放置相机的更多信息,请查看

于 2013-04-27T09:29:09.357 回答