天天看點

解決SurfaceView調用setZOrderOnTop(true)遮擋其他控件的問題

  1. SurfaceView遮擋其他控件的項目背景:

    最近在做播放器項目,由于底層實作是用Surface和OpenGL切換渲染,是以在布局裡面同時使用了GLSurfaceView和SurfaceView,同時播放控制按鈕是自定義的,也沒有使用Android自己提供的MediaCtroller控件。在這種背景下,問題出現了,如果有相關開發基礎的同學應該知道,當SurfaceView和GLSurfaceView同時在一個布局裡面,如果想讓SurfaveView顯示圖檔或者視訊必須要調用SurfaceView.setZOrderOnTop(true),也就是說必須把SurfaceView置于Activity顯示視窗的最頂層才能正常顯示,然後調用了SurfaceView.setZOrderOnTop(true)又導緻了其他控件比如播放、快進等按鈕被遮擋。網上有很多解決方案,比如解決SurfaceView設定透明造成遮蓋其他元件的替代方案,對于視訊播放的頁面都不夠完美,因為它是直接在SurfaceView上面繪制相關的控件,試想一下如果在SurfaceView的某些區域繪制了一些按鈕,勢必會擋住一部分視訊畫面,這樣對于使用者來說是很難接受的。

  2. 從SurfaceView源碼中尋找解決方案:

    由于在網上找的解決方案都不能滿足要求,沒辦法有折回來檢視了下SurfaceView的源碼,在檢視源碼的時候看到這個方法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 {@link #setZOrderOnTop}.

  大概的意思就是說控制視窗中表面的視圖層是否放置在正常視圖層的頂部。

  最終,我在調用setZOrderOnTop(true)之後調用了setZOrderMediaOverlay(true),OK,遮擋問題完美解決!

繼續閱讀