我有一个项目,其中有一个xml布局,其中包含按钮和所有按钮,并且我需要背景为camera,所以预览位于buttons的后面,我该怎么做?

最佳答案

这是我的项目中的xml:

<?xml version="1.0" encoding="UTF-8"?>
<!--
     Copyright (C) 2008 ZXing authors Licensed under the Apache License,
    Version 2.0 (the "License"); you may not use this file except in compliance
    with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software distributed
    under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
    OR CONDITIONS OF ANY KIND, either express or implied. See the License for
    the specific language governing permissions and limitations under the License.


-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF8090A0" >

    <SurfaceView
        android:id="@+id/preview_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <com.x09soft.scanner.zxing.ViewfinderView
        android:id="@+id/viewfinder_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/transparent" />

    <ImageButton
        android:id="@+id/btn_flash"
        android:background="@drawable/flash_off"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="right|center_vertical"/>

</FrameLayout>


CaptureActivity和ViewfinderView您可能会看到(如上所述)here

正如在CaptureActivity的文档中所述:


  此活动将打开相机并在背景上进行实际扫描
  线。它会绘制一个取景器,以帮助用户正确放置条形码,
  在图像处理过程中显示反馈,然后覆盖
  扫描成功时显示结果。


ViewFinderView:


  该视图覆盖在摄像机预览的顶部。它添加了取景器
  矩形和外部的部分透明性以及激光扫描仪
  动画和结果点。


如果您不想画任何锐利,那就不要使用ViewfinderView。

看一下CaptureActivity初始化相机方法,也许会对您有所帮助。

private void initCamera(SurfaceHolder surfaceHolder) {
        try {
            cameraManager.openDriver(surfaceHolder);
            // Creating the handler starts the preview, which can also throw a
            // RuntimeException.
            if (handler == null) {
                handler = new CaptureActivityHandler(this, decodeFormats,
                        characterSet, cameraManager);
            }
        } catch (IOException ioe) {
            Log.w(TAG, ioe);
            displayFrameworkBugMessageAndExit();
        } catch (RuntimeException e) {
            // Barcode Scanner has seen crashes in the wild of this variety:
            // java.?lang.?RuntimeException: Fail to connect to camera service
            Log.w(TAG, "Unexpected error initializing camera", e);
            displayFrameworkBugMessageAndExit();
        }
    }


SurfaceHadler是在resume()方法中创建的:

 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();


另外检查此link

10-08 06:17