0x00
模拟手机位置的桌面悬浮清理内存的效果
0x01
mParams = new WindowManager.LayoutParams();
mParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mParams.gravity = Gravity.LEFT | Gravity.TOP;
mParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
mParams.format = PixelFormat.TRANSLUCENT;
mParams.type = WindowManager.LayoutParams.TYPE_PRIORITY_PHONE;
其中的 mParams.gravity = Gravity.LEFT | Gravity.TOP; 会改变显示位置的坐标系(姑且理解为改变)
如果不设置Gravity.TOP,打印出来的mParams.y在屏幕的y轴方向的中间处为0,导致总达不到想要的效果
0x02
查询源码:
gravity属性:
/**
* Placement of window within the screen as per {@link Gravity}. Both
* {@link Gravity#apply(int, int, int, android.graphics.Rect, int, int,
* android.graphics.Rect) Gravity.apply} and
* {@link Gravity#applyDisplay(int, android.graphics.Rect, android.graphics.Rect)
* Gravity.applyDisplay} are used during window layout, with this value
* given as the desired gravity. For example you can specify
* {@link Gravity#DISPLAY_CLIP_HORIZONTAL Gravity.DISPLAY_CLIP_HORIZONTAL} and
* {@link Gravity#DISPLAY_CLIP_VERTICAL Gravity.DISPLAY_CLIP_VERTICAL} here
* to control the behavior of
* {@link Gravity#applyDisplay(int, android.graphics.Rect, android.graphics.Rect)
* Gravity.applyDisplay}.
*
* @see Gravity
*/
public int gravity;
android.view.WindowManager.LayoutParams中的x,y属性:
/**
* 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;
0xff
想要做左上角为坐标原点,最好设置:mParams.gravity = Gravity.LEFT | Gravity.TOP;
另可参考:
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1105/509.html