WindowManager is an important Service in Android and is globally unique. WindowManager inherits from ViewManager.
WindowManager is mainly used to manage the status, attributes, view addition, deletion, update, window order, and message Collection and Processing of windows. In Android, windows and views are displayed to users. activity is mainly used to handle logic problems, such as lifecycle management and window creation.
There is also an important static class LayoutParams in WindowManager. It can be used to set and obtain some properties of the current window.
1. Implement floating window:
1. Obtain the WindowManager service:
WindowManager wmManager = (WindowManager) getSystemService (Context. WINDOW_SERVICE );
2. Set WindowManager. LayoutParams Parameters
WindowManager. LayoutParams wmParams = new WindowManager. LayoutParams ();
WmParams. type = LayoutParams. TYPE_PHONE; // set the window type.
WmParams. format = PixelFormat. RGBA_8888; // set the image format. The effect is transparent to the background.
WmParams. flags = LayoutParams. FLAG_NOT_TOUCH_MODAL | LayoutParams. FLAG_NOT_FOCUSABLE | LayoutParams. FLAG_NOT_TOUCHABLE;
WmParams. gravity = Gravity. RIGHT | Gravity. CENTER_VERTICAL; // adjust the floating window to the middle of the RIGHT
WmParams. x = 0; // set the initial values of x and y in the upper-left corner of the screen.
WmParams. y = 0;
WmParams. width = WindowManager. LayoutParams. WRAP_CONTENT; // you can specify the length and width of the floating window.
WmParams. height = WindowManager. LayoutParams. WRAP_CONTENT;
3. Add view to screen
WmManager. addView (view, wmParams );
4. delete a view from the screen
WmManager. removeView (view );
5. You need to add permissions to the floating window.
<Uses-permission android: name = "android. permission. SYSTEM_ALERT_WINDOW"/>
2. Obtain the screen size through Display in WindowManager:
WmManager. getdefadisplay display (). getWidth ();
WmManager. getdefadisplay display (). getHeight ();
3. Change the Dialog background transparency:
Dialog dg = new Dialog (this );
Window window = dg. getWindow ();
WindowManager. LayoutParams lp = window. getAttributes ();
Lp. alpha = 0.5f;
Window. setAttributes (lp );