iphone has a very useful white dot, today to study the suspension box in Android, here is the main implementation of a shortcut key function, of course, can also do in the suspension box to do things!
The implementation of the suspension window is mainly through the implementation of WindowManager, of course, WindowManager is just an interface, want to know the source of the comrades can go to see Windowmanagerimpl, suspended box is mainly through the WindowManager AddView , Updateview,removeview implements
Windowmanager.layoutparams This class is used to provide the required parameters for a suspended window
Windowmanager.layoutparams parameter Description:
- Type is used to determine the types of suspended Windows (window type, window has three types, the application window, Sub window, System window, where the suspended window is using the System window), generally using Type_phone, representing on all applications, Status bar, of course, there are many types, Type_status_bar (status bar) Type_search_bar (search box) Type_system_alert (System prompt, such as the low battery), etc., there are many, according to the needs to choose
- The flags are used to determine the behavior of the suspended window, flag_not_focusable (window does not need to get focus, and does not need to receive various input events) flag_not_touchable (not clickable) Flag_not_touch_ MODAL (The system passes a click event outside the current window area to the underlying window, and the click event within the current window area is handled by itself) flag_show_when_locked (displayed on the screen of the lock screen), etc.
- Gravity used to determine the alignment of a suspended window
- X to determine the horizontal axis of the suspended window
- Y is used to determine the ordinate of the suspended window
- Width value Specifies the length of the suspended window
- Height value specifies the altitude of the suspended window
Knowing these parameters will basically create a hover box.
private void Createfloatview () {Mwindowmanager = (WindowManager) getapplication (). Getsystemservice(Getapplication (). WINDOW_service);Wmparams = new WindowManager. Layoutparams();Wmparams. Type= WindowManager. Layoutparams. TYPE_phone;Wmparams. Flags= WindowManager. Layoutparams. FLAG_not_focusable;Wmparams. Gravity= Gravity. Left| Gravity. TOP;Wmparams. x= -;Wmparams. Y= -;Wmparams. Width= WindowManager. Layoutparams. WRAP_content;Wmparams. Height= WindowManager. Layoutparams. WRAP_content;Layoutinflater Inflater = Layoutinflater. from(Getapplication ());Mfloatlayout = (linearlayout) inflater. Inflate(R. Layout. Layout_float_window, NULL);Mwindowmanager. AddView(Mfloatlayout, Wmparams);Mfloatview = (ImageView) mfloatlayout. Findviewbyid(R. ID. IMG_float_window);Mfloatview. Setontouchlistener(New View. Ontouchlistener() {@Override public boolean onTouch (View V, motionevent event) {Wmparams. x= (int) event. GETRAWX()-Mfloatview. Getmeasuredwidth() /2;Wmparams. Y= (int) event. Getrawy()-Mfloatview. Getmeasuredheight() /2- -;Mwindowmanager. Updateviewlayout(Mfloatlayout, Wmparams);return False;} });Mfloatview. Setonclicklistener(New View. Onclicklistener() {@Override public void OnClick (View v) {Intent Intent = new Intent (floatwindowse Rvice. this, searchactivity. Class);Intent. Addflags(Intent. FLAG_activity_new_task);Getapplication (). StartActivity(Intent);} });}
ifnull) { mWindowManager.removeView(mFloatLayout); }
The above is the process of the suspension box, of course, using the System window must request permission:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Through this process you know how to create a hover box through WindowManager
Below you a demo, of which there are two suspended windows, the first suspension window is the normal suspension window, the second suspension window added some judgment, can be used directly in the app, the hover window in the desktop and your own app display
Address: Https://github.com/zimoguo/FloatWindow
Android Hover window