1. 程式人生 > >兩個SurfaceView顯示預覽的問題 setZOrderMediaOverlay(true)

兩個SurfaceView顯示預覽的問題 setZOrderMediaOverlay(true)

工作中遇見過一個這樣的需求:

啟動後置相機,用一個SurfaceView (稱其物件為SV1)顯示預覽,

拍照,然後關閉後置相機,

啟動前置相機,用另一個SurfaceView(稱其物件為SV2)顯示前置預覽

這個時候問題來了,前置預覽顯示不出來,startPreview也沒有任何異常,

如果把SV1從layout中remove掉,SV2就可以正常顯示前置預覽。。。 。。。

如何才能不把SV1從layout中remove掉,也讓SV2顯示前置預覽呢???

答案是隻需要一個api,英語不好真心傷不起呀,開始沒有看太懂。就沒有太在意

public void setZOrderMediaOverlay
(boolean isMediaOverlay)

Control whether the surface view's surface is placed on top of another regular surface view in the window (but still behind the window itself). This is typically used to place overlays on top of an underlying media surface view.

Note that this must be set before the surface view's containing window is attached to the window manager.

大致意思就是

setZOrderMediaOverlay

不論這個SurfaceView是否在其他SurfaceView的上面,這個api的作用通常就是將其覆蓋在其他媒體上面

注意的大致意思就是 執行此api請確定在 layout.addView(thisSurface)之後。

所以,SV2.setZOrderMediaOverlay(true);

問題得解!!!