2011-05-30 44 views

回答

13

一个很好的例子可以在ZXing库,Barcode Scanner应用程序中找到。

他们做什么是他们使用FrameLayoutSurfaceView和他们的自定义ViewfinderView这样既SurfaceViewViewfinderView被覆盖全屏幕:

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

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

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

</FrameLayout> 

注意fill_parentlayout_heightlayout_width两种意见。

然后他们在ViewfinderView.onDraw()方法中绘制自定义事件,只是在摄像头预览显示内容的顶部。

如果你不想画任何东西,但只是使用预定义的图像,然后使用ImageView而不是ViewfinderView。您可能会考虑调用setAlpha方法来使图像透明(如果drawable本身不是透明的)。


这里是从条码扫描仪的屏幕截图:

enter image description here