1. View and Viewroot
Viewroot from the name seems to be "the root of the View tree", which can easily cause misunderstanding. Because Viewroot is not part of the view tree. From the source implementation point of view, Viewroot and the View object does not have any "blood" relationship, it is not a view of the subclass, nor the parent of the view. Viewroot can be understood as "the manager of the View tree"-it has a Mview member variable that points to the root of the view tree it manages.
Let's look at the following layout code:
<pre name= "code" class= "HTML" ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:id= "@+id/test" android:layout_width= "Match_parent" Android : layout_height= "match_parent" android:orientation= "vertical" ><button android:id= "@+id/start_service" Android Oid:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Start Service"/><Button Android:id= "@+id/stop_service" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android oid:text= "Stop Service"/><button android:id= "@+id/bind_service" android:layout_width= "Match_parent" an droid:layout_height= "Wrap_content" android:text= "Bind Service"/><button android:id= "@+id/unbind_service" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Unbind Service"/>&L T;/linearlayout>
The above layout code is just an arbitrary layout code, here the main look at its structure, the layout code of the view structure can be described as follows:
In this layout, the Mview member variable in viewroot points to the root of the view tree that it manages, that is, the linearlayout:test element in. Viewroot's core task is to communicate with Windowmanagerservice. 2. Activity and window relationships we know that activity is a support for displaying the UI, so does it directly manage the view tree or Viewroot? The answer is no, activity does not have a direct connection to the two, because there is also an object called "window" in the middle. We can find the following code in the activity Source: private Window Mwindow; Window literally means "windows", which is a good explanation of what it means to exist. window is the base class, depending on the product can be derived from different sub-categories-- This is determined by the system in Activity.attach call Policymanager.makenewwindow, the current version of the Android system is generated by default Phonewindo. 3, window and Windowmanagerimpl relationship in the Android source code with "window" beginning of the class has a lot, such as Window,windowmanager,windowmanagerimpl, Why do you need so many similar classes? First look at window, it is activity-oriented, the "UI interface of the outer frame", and "in the box" specific things, including layout and content, is the specific window subclass, such as Phonewindow to plan. Another layer of window means to communicate with windowmanagerservice, but it does not implement this function directly on its own. The reason: More than one window is likely to exist in an application. If they are all communicating with the WMS individually, it wastes resources and creates confusion in management. In other words, they need to be managed in a unified way. Then there is the WindowManager, which exists as a member variable of window Mwindowmanager. This windowmanager is an interface class whose real implementation is Windowmanagerimpl, which is also the manager of all windows in the entire application. Thus the relationship between WindowManager and Windowmanagerimpl is somewhat analogous to "local and central": The place provides an interface for implementing a central "policy", which is then aggregated to the central administration. In the window source code is related to mwindowmager the following sentence: private WIndowmanager Mwindowmanager; Mwindowmanager = ((Windowmanagerimpl)WM).Createlocalwindowmanager( This);
Public WindowManager Getwindowmanager () {
return Mwindowmanager;
}
then we go to Windowmanagerimpl code to see the code of the Createlocalwindowmanager method, the code is as follows:
Public Windowmanagerimpl Createlocalwidnowmanager (Window parentwindow) {
return New Windowmanagerimpl (mdisplay, parentwindow);
}
from these code, you can see that the Mwindowmanger reference in the window class is actually an instance of Windowmanagerimpl.
4. The relationship between Viewroot and Windowmanagerimpl
In earlier versions of the system, Windowmanagerimpl had only one instance in each process. Calling it must use the following statement: Windowmanagerimpl.getdefault ();
Within Windowmangerimpl, there are 3 global variables: (the source code in 2.3.5)
They are used to represent the properties of the root node, viewroot, and window of the view tree, respectively. It can also be seen that there is not only a viewroot in a process, but activity and Viewroot are a one-to-one relationship. Since Android4.3 began to modify this, Windowmanagerimpl no longer directly stored the above three array variables, but by a class called "Windowmangerglobal" unified management. 5, Viewroot and windowmanagerservice the relationship of each viewrootimpl internal, there is a global variable static iwindowsession swindowsession; This variable is used to viewroot the connection to the WMS, which is viewroot using the WMS's opensession () interface to create the resulting. Based on this, Viewroot also provides a Iwindow object through the Iwindowsession.add () method-allowing the WMS to communicate with Viewroot in two-way through this IBinder object. Here suddenly a Viewrootimpl class, in fact, the function of Viewroot and Viewrootimpl is the same, but the different versions of Android address. Do not believe to see:
Since this is the picture, then we recommend a website to see the android source online: http://grepcode.com/project/repository.grepcode.com/java/ext/ Com.google.android/android/in fact, each application has a Activitythread main thread and a mactivities global variable, which records all the activity objects running in the application. One activity corresponds to the only WindowManager and Viewrootimpl. Windowmanagerglobal as a global manager, its internal mroots and mviews record the activity's Viewrootimpl and top-level elements of the view tree. Another important role for VIEWROOTIMPL is to be responsible for communicating with the WMS. Communication from Viewrootimpl to WMS takes advantage of iwindowsession, while the reverse direction is done by Iwindow. A final note: There is a member variable in activity mwindow,mwindow a member variable Mwindowmanager, and Mwindowmanager is a reference to the Windowmangerimpl class instance. In addition, the Windowmangerimpl contains viewroot (either directly included in the old version or indirectly included in the new version). This post is only for the purpose of documenting my understanding of viewroot and some of the concepts associated with viewroot. Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/45798221
Some of the concepts associated with Viewroot in Android