Java android - 何我将活动的背景设置为相机预览?

时间:2013-04-27 09:21:34

标签: android background camera

我是Java的新手,我正在尝试创建一个应用程序,您可以使用陀螺仪在屏幕上移动图像,这样看起来图像就会粘在相机上的物体上。图像完成了,我把它移动了,就像它应该的那样。现在我只需要相机作为xml文件的背景。有没有简单的方法可以做到这一点?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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!  (:" );

}