Camera preview with a transparent image above

≯℡__Kan透↙ 提交于 2020-01-12 01:58:25

问题


I would like to see an image on top of Camera SurfaceView\preview. How do I do that? Any examples?


回答1:


A great example can be found in ZXing library, Barcode Scanner application.

What they do is they use FrameLayout for SurfaceView and their custom ViewfinderView so that both SurfaceView and ViewfinderView are covering full screen:

<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>

Note the fill_parent values in layout_height and layout_width for both views.

And then they draw custom things in ViewfinderView.onDraw() method, just on top of what is being displayed by camera preview.

If you don't want to draw anything, but just use predefined image, then use ImageView instead of ViewfinderView. You might consider calling setAlpha method to make your image transparent (if drawable wasn't already transparent by itself).


Here is a screenshot from Barcode Scanner:



来源:https://stackoverflow.com/questions/6181165/camera-preview-with-a-transparent-image-above

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!