Many people know how to achieve a simple floating window, but few people go deep into the process behind the mechanism, because the project in the window of the interaction is more complex, encountered some pits to view a lot of information, it summarizes the floating window involved in the Knowledge points:
- Window hierarchy (How is the floating window "floating")?
- What are the restrictions on floating windows, and how can I pass the user authorization to realize the floating window function?
- The window and the user input system (how the activity receives the touch event?).
In this chapter we examine the first question: why floating windows float. Floating windows are called floating windows because they can be suspended from the application or the Desktop window, and can be separated from the activity and exist. To study the differences, let's take a look at how our most familiar activity is displayed.
How did the activity show up?
To find out the answer to this question, we first from the activity of the Setcontentview () This method of the source to start, in the activity to see the source of Setcententview:
public void setContentView(int layoutResID) { getWindow().setContentView(layoutResID); initWindowDecorActionBar(); }
GetWindow is the Mwindow variable that returns the activity, pointing to the object of a window, which is an abstract class that returns a Phonewindow object (Phonewindow is a subclass of window). Phonewindow has a Decorview object, Decorview member, which is a Framelayout,setcontentview sub-layout that will eventually be added to Decorview, This decorview is the root view of the current window, how is this root view ultimately drawn? There is a piece of code in Activitythread:
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION; l.softInputMode |= forwardBit; if (a.mVisibleFromClient) { a.mWindowAdded = true; wm.addView(decor, l); }
This decorview will eventually be added to the drawing system by Windowmanager.addview, and the type is WindowManager.LayoutParams.TYPE_BASE_APPLICATION, This parameter determines the level of the z-axis of the window to be drawn, in order to avoid the thought stack too deep, here does not post the detailed source tracking process, directly to the conclusion.
Let's look at the relationship between activity and window:
Then the relationship between window and view:
Activity window Display Process:
It says how activity is displayed, in fact, how the view of activity management is displayed. Finally, let's summarize:
The view that activity is set through Setcontentview is added to Phonewindow's root view decor.
Second, window is an abstract concept, window closes a view (root view), ultimately managed by WindowManager is a view (root view) and its layoutparams, The view drawing refreshes are drawn from the underlying WindowManager (Windowmanagerglobal) with the WINDOWMANAGERSERVICEIPC interaction call.
Activity is the only component in the four components that is closely linked to the form (this is why beginners have a direct understanding of activity as the reason for drawing the interface), all the views in charge are just a window and dialog, Toast, The wallpaper does not have the same window type.
Why does the floating window "float"?
In fact, the display of activity has revealed the general interface display process, the display process of the floating window is more simple:
The students who have done the floating window should understand, why the floating window can be separated from activity and show, in essence, we are a view to the WindowManager to manage, The Layoutparams.type type determines the type of the View display window, and the level of the window (z-axis) displayed in different types is not the same. In large terms can be divided into the application window (Application_window), Sub-window (sub_window), System window (System_window) three types, the application window Z-axis range is 1~99, the child window is the range of 1001~1999, The System window is (2000~2999), so to implement the floating window we can only be implemented in the scope of the system window.
Type |
Constant range |
Sub-class |
Constant value |
Description |
Example |
Application_window |
1~99 |
Type_base_application |
1 |
|
|
|
|
Type_application |
2 |
application window |
Most of the application windows |
|
|
Type_application_starting |
3 |
The activity of the application displays the windows that were previously displayed by the system |
|
|
|
Last_application_window |
99 |
|
|
Sub_window |
1000~1999 |
First_sub_window |
1000 |
|
|
|
|
Type_application_panel |
1000 |
Appears above the parent window, obscuring the application window below it. |
|
|
|
Type_application_media |
1001 |
Appears below the parent window and is not visible if the application window does not burrow. |
Surfaceview, set as media when the small window is displayed, and set as panel when full screen is displayed |
|
|
Type_application_sub_panel |
1002 |
|
|
|
|
Type_application_attached_dialog |
1003 |
|
|
|
|
Type_application_meida_overlay |
1004 |
For the synthesis of two surfaceview, if set to media, the above Surfaceview block the surfaceview below |
|
System_window |
2000~2999 |
Type_status_bar |
2000 |
Status bar at the top |
|
|
|
Type_search_bar |
2001 |
Search window, only one search window can be found in the system |
|
|
|
Type_phone |
2002 |
Phone window |
|
|
|
Type_system_alert |
2003 |
Warning window, displayed on top of all other windows |
Low Battery Alert window |
|
|
Type_keyguard |
2004 |
Lock Screen Interface |
|
|
|
Type_toast |
2005 |
Short Text Reminder Small window |
|
|
|
Type_system_overlay |
2006 |
Floating window with no focus |
|
|
|
Type_priority_phone |
2007 |
Emergency Phone window, can be displayed on the screen saver |
|
|
|
Type_system_dialog |
2008 |
System Information pop-up window |
such as the operator Information window that pops up after SIM plug-in |
|
|
Type_keyguard_dialog |
2009 |
Popup dialog box bound to Keyguard |
Sliding unlock window when lock screen |
|
|
Type_system_error |
2010 |
System error Prompt window |
ANR window |
|
|
Type_input_method |
2011 |
IME window, crowding out the current app's space |
|
|
|
Type_input_method_dialog |
2012 |
Pop-up IME window will not squeeze the current application window space, above it display |
|
|
|
Type_wallpaper |
2013 |
Wallpaper |
|
|
|
Type_status_bar_panel |
2014 |
The window that is pulled from the status bar |
|
|
|
Type_secure_system_overlay |
2015 |
Only overlay windows that system users can create |
|
|
|
Type_drag |
2016 |
Floating, draggable windows |
360 security guard's floating Genie |
|
|
Type_status_bar_panel |
2017 |
|
|
|
|
Type_pointer |
2018 |
Cursor |
|
|
|
Type_navigation_bar |
2019 |
|
|
|
|
Type_volume_overlay |
By 2020 |
Volume Adjustment window |
|
|
|
Type_boot_progress |
2021 |
Start progress, above all windows |
|
|
|
Type_hidden_nav_consumer |
2022 |
Hidden navigation Bar |
|
|
|
Type_dream |
2023 |
Screensaver animations |
|
|
|
Type_navigation_bar_panel |
2024 |
Navigation Bar pop-up window |
For example, apply a collection bar |
|
|
Type_universal_background |
2025 |
|
|
|
|
Type_display_overlay |
2026 |
For simulating a second display device |
|
|
|
Type_magnification |
2027 |
Used to enlarge Local |
|
|
|
Type_recents_overlay |
2028 |
Current application window, multi-user case only displayed in user program |
Windows Hierarchy of Android window development