概述
在熊貓直播,等視頻APP中,都有這樣的一個功能,當退出播放界面的時候,會有一個小窗口播放視頻,那么怎么實現的?
如下圖
image
視頻播放窗口是可以隨意拖動。
那么該如何實現呢?
關于 WindowManager.LayoutParams
為什么剛開始就要談 這個類,因為這個類非常的關鍵。
其中 gravity,x,y這三個屬性非常的重要。
從sdk的給出的信息看:
public static class LayoutParams extends ViewGroup.LayoutParams implements Parcelable {
/**
* X position for this window. With the default gravity it is ignored.
* When using {@link Gravity#LEFT} or {@link Gravity#START} or {@link Gravity#RIGHT} or
* {@link Gravity#END} it provides an offset from the given edge.
*/
@ViewDebug.ExportedProperty
public int x;
/**
* Y position for this window. With the default gravity it is ignored.
* When using {@link Gravity#TOP} or {@link Gravity#BOTTOM} it provides
* an offset from the given edge.
*/
@ViewDebug.ExportedProperty
public int y;
如果gravity 是默認值,那么x,y就不會起作用。對于其它的情況下:
it provides an offset from the given edge.
這個是什么意思?
比如,gravity 設置為 Gravity.LEFT | Gravity.TOP;
那么這個Window會顯示在屏幕的左上角位置,這個時候設置 x,y 那么基于左上角這個位置進行偏移。
image
如果設置為 Gravity.CENTER | Gravity.CENTER;
那么,偏移的起始位置就是中心位置。
image
==也就是說,x,y的偏移的起始位置,由Gravity決定的。==
(上面的圖用ps畫的比較丑。但意思就是那個)
還需要注意的是:
在 y 軸上,不包括標題欄的高度,在計算的時候需要特別注意。
關于WindowManger
掌握了 WindowManger相關方法的用法
- addView
- removeView
- updateViewLayout
那么做出這樣彈框一點都不難了。
demo 的托管地址為: