SurfaceView遮擋其餘控件的項目背景:html
最近在作播放器項目,因爲底層實現是用Surface和OpenGL切換渲染,因此在佈局裏面同時使用了GLSurfaceView和SurfaceView,同時播放控制按鈕是自定義的,也沒有使用Android本身提供的MediaCtroller控件。在這種背景下,問題出現了,若是有相關開發基礎的同窗應該知道,當SurfaceView和GLSurfaceView同時在一個佈局裏面,若是想讓SurfaveView顯示圖片或者視頻必需要調用SurfaceView.setZOrderOnTop(true),也就是說必須把SurfaceView置於Activity顯示窗口的最頂層才能正常顯示,而後調用了SurfaceView.setZOrderOnTop(true)又致使了其餘控件好比播放、快進等按鈕被遮擋。網上有不少解決方案,好比解決SurfaceView設置透明形成遮蓋其餘組件的替代方案,對於視頻播放的頁面都不夠完美,由於它是直接在SurfaceView上面繪製相關的控件,試想一下若是在SurfaceView的某些區域繪製了一些按鈕,勢必會擋住一部分視頻畫面,這樣對於用戶來講是很難接受的。ide
從SurfaceView源碼中尋找解決方案:佈局
因爲在網上找的解決方案都不能知足要求,沒辦法有折回來查看了下SurfaceView的源碼,在查看源碼的時候看到這個方法setZOrderMediaOverlay(boolean isMediaOverlay),下來咱們來看看源碼中對這個方法的描述:this
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..net
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}.htm
大概的意思就是說控制窗口中表面的視圖層是否放置在常規視圖層的頂部。blog
最終,我在調用setZOrderOnTop(true)以後調用了setZOrderMediaOverlay(true),OK,遮擋問題完美解決!圖片
出處:https://www.cnblogs.com/youlechang123/p/5693764.html開發