Here is the problem~ I tried to add an ImageView to the window by calling the WindowManager.addView() method. But I found that the ImageView is in the center of the screen, not the left-top corner. It appears in left-top corner only when I set the gravity property of the WindowManager.LayoutParams to Gravity.LEFT|Gravity.TOP, why? Is the default value of gravity property Gravity.CENTER? I looked into the android source code but got lost.
ImageView img = new ImageView(this);
img.setImageResource(android.R.drawable.ic_delete);
WindowManager manager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(60,60,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
//params.gravity= Gravity.LEFT|Gravity.TOP;
manager.addView(img, params);
In the source code of WindowManager.LayoutParams, I found that only when the gravity is set, the x and y properties will be taken into account:
/**
* 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;
However this is not enough, I want to konw what happens when gravity is not set(More specifically, I want to find the code drawing the ImageView).
Any tip will be appreciated.