Some time ago, the implementation of the android floating window was stirred up. Today, I wrote a demo of freely moving the floating window.
Brief description:
1. Move the floating window freely by overwriting the ontouchevent method in the floating view.
2. the moving of the floating window coordinates is actually the transformation of x and y in windowmananager. layoutparams, but you must set the corresponding gravity.
3. View created with windowmanager. When not needed,Be sure to rememberUse the removeview method of windowmanager to remove a scan task. Add a scan task in the activity lifecycle.
4,CodeDetailed notes are attached. For more information about the parameters, see the SDK.
Demo source code
====================================
Note the wmparams. format = 1 attribute in the demo (I used a double slash in the source code). If it is enabled, the image background will be transparent, as shown below:
======================================
Notes:
For some friends who haven't checked the SDK documentation for various reasons, they may not know the specific meaning of the 2002 statement in my source code such as wmparams. type = 2002, and give a statement with better readability.
As follows:
Wmparams. Type = Layoutparams. Type_phone ; // 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 ; /** the effect of the flags attribute is the same as that of "locked ". * The floating window cannot be touched. It does not accept any events and does not affect subsequent event responses. Wmparams. Flags = layoutparams. flag_not_touch_modal | layoutparams. flag_not_focusable | layoutparams. flag_not_touchable; */