1. 程式人生 > >Android兩個Surface重疊顯示

Android兩個Surface重疊顯示

專案需求,相機預覽做背景surfaceView1,播放其VideoView在其上,即:1個surfaceview獲取相機預覽資料作為背景(CustomCameraView),1個surfaceview在前一surfaceview之上作為繪圖層(GamePanelView)。
佈局使用framelayout,大小一致。由於surfaceview本身為透明的,本人認為直接層疊2個surfaceview就行了。結果無論在繪圖層怎樣繪圖,圖形都不會出現。查閱資料找到了解決方案。

在繪圖層surfaceview初始化時,設定以下2個引數:
//surfaceview透明
holder.setFormat(PixelFormat.TRANSPARENT);
//surfceview放置在頂層,即始終位於最上層
setZOrderOnTop(true);

videoView.getHolder().setFormat(PixelFormat.TRANSPARENT);
videoView.setZOrderOnTop(true);
VideoView本身就是SurfaceView+MediaPlayer

當場景中有多個SurfaceView的時候,上層的SurfaceView可能會被下層的遮擋,這個時候需要使用setZOrderOnTop(true)或者setZOrderMediaOverlay(true)來控制SurfaceView的顯示層次。

Android SDK對兩個函式的描述:

public void setZOrderOnTop (boolean onTop)

Control whether the surface view's surface is placed on top of its window. Normally it is placed behind the window, to allow it to (for the most part) appear to composite with the views in the hierarchy. By setting this, you cause it to be placed above the window. This means that none of the contents
of the window this SurfaceView is in will be visible on top of its surface. Note that this must be set before the surface view's containing window is attached to the window manager. Calling this overrides any previous call to setZOrderMediaOverlay(boolean). 控制這個surfaceView是否被放在視窗頂層。通常,為了使它與繪圖樹整合,它被放在視窗之後。通過這個函式,你可以使SurfaceView被放在視窗頂層。這意味著它所在的視窗的其他內容都不可見。(注:可以設定surfaceView透明來使其他內容可見) 這個函式必須在視窗被新增到視窗管理器之前設定。 呼叫這個函式會使之前呼叫的setZOrderMediaOverlay(boolean)無效;

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.

Calling this overrides any previous call to setZOrderOnTop(boolean).

控制這個surfaceView是否被放在另一個普通的surfaceView上面(但是仍然在視窗之後)。這個函式通常被用來將覆蓋層至於一個多媒體層上面。

這個函式必須在視窗被新增到視窗管理器之前設定。

呼叫這個函式會使之前呼叫的setZOrderOnTop(boolean)無效。