Cover
What's the material?
You can obtain these materials from this article:
Know what happened after Setcontentview ()?
Do you know how Android actually displays the images we expect on the screen?
Have an overall grasp of the Android view architecture.
Learn to analyze the causes of the screen lag from the root point.
Learn tips on how to write a smooth app.
Learn from the source of Android's fine-thinking.
Get two homemade graphs to help you understand the Android view architecture.
Link
Speaking from Setcontentview ()
public class Analyzeviewframeworkactivity extends Activity {br/> @Override
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_analyze_view_framwork);
}
}
The above code is presumably androider more familiar than most. But do you know what happens after this is written? Where has this layout been added to? My God, the point of knowledge is coming!
Many students may also know that this layout is placed in a parent layout called Decorview, but I'll say it again. And see??
Image
This figure may not be the same as the ones that are common in books or on the Internet, why not? Because I drew it myself, hahaha ...
Here's a look at the most basic view frame of Android.
Phonewindow
It is estimated that many students know that each activity has an instance of a Window object. This instance is actually of type Phonewindow. Then Phonewindow from the name is easy to see, it should be the son of window (that is, sub-Class)!
Knowledge Points: Each activity has a Phonewindow object.
So what's the use of Phonewindow? What role does it play in activity? Let me just call the Phonewindow equivalent to window.
Image
Window literally looks like it's a window, meaning that the concept of Windows on the PC is a bit similar. But it's not that accurate. Look at the illustration. As you can see, the layout we want to show is placed in its property Mdecor, and this mdecor is an example of Decorview. The following will be dedicated to Decorview, now focus on the window. Window also has a more important property, Mwindowmanager, which is an instance of an implementation class for WindowManager (which is an interface). What we usually get through the Getwindowmanager () method is this mwindowmanager. As the name implies, it is the manager of window, responsible for managing the windows and the content displayed in them. Its actual implementation class is Windowmanagerimpl. Perhaps children's shoes are now looking for the Phonewindow in the Mwindowmanager is where to instantiate, is not rolling up and down the class are not found? stop! Mwindowmanager is a good example of his father. The following code is in Window.java.
public void Setwindowmanager (WindowManager wm,
IBinder Apptoken,
String AppName,
Boolean hardwareaccelerated) {
...
if (wm = = null) {
WM = (WindowManager) mcontext.getsystemservice (Context.window_service);
Gets a WindowManager
}
Mwindowmanager = ((Windowmanagerimpl) WM). Createlocalwindowmanager (this);
From here we can know that the WM obtained above is actually the Windowmanagerimpl type.
}
From the above, we already know that the window is responsible for hosting the layout of the Decorview, there is responsible for the management of the WindowManager (in fact, it is only a proxy, the latter will say who the agent is).
Decorview
As mentioned earlier, the layout set through Setcontentview () in the activity's OnCreate () is actually placed in Decorview. We found the Decorview in the picture.
As you can see, Decorview inherits the Framelayout, and in general, it adds a preset layout earlier. For example Decorcaptionview, it is placed from top to bottom of its own sub-layout, equivalent to a linearlayout. Usually it will have a title bar, and then there is a mcontentroot to hold the content, depending on the type of layout. The layout we want to show is put in the mcontentroot.
Knowledge Points: Layouts set through Setcontentview () are placed in Decorview, Decorview is the topmost layer of the view tree.
WindowManager
As mentioned earlier, WindowManager plays a very important role in window. Let's find it in the diagram first. It is important to note that the Mwindowmanager in Phonewindow is actually windowmanagerimpl type. Windowmanagerimpl Nature is an implementation class of interface WindowManager. This is something I did not reflect in the picture.
WindowManager is created when the activity executes attach (), and the Attach () method is called before OnCreate (). The creation of activity can be seen in my article: "may be the simplest in history!" A picture of 3 minutes to let you understand the activity to start the process, do not look regret! Http://www.jianshu.com/p/9ecea420eb52 ".
Activity.java
final void attach (context context, Activitythread Athread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent Intent, Activityinfo info,
Charsequence title, Activity parent, String ID,
Nonconfigurationinstances Lastnonconfigurationinstances,
Configuration config, String referrer, Ivoiceinteractor Voiceinteractor,
Window window) {
...
Mwindow = new Phonewindow (this, window);
Create window
...
Mwindow.setwindowmanager (
(WindowManager) Context.getsystemservice (Context.window_service),
Mtoken, mcomponent.flattentostring (),
(Info.flags & activityinfo.flag_hardware_accelerated)! = 0);
Attention! This is where you create the WindowManager.
This method has already been said in the previous.
if (mparent! = null) {
Mwindow.setcontainer (Mparent.getwindow ());
}
Mwindowmanager = Mwindow.getwindowmanager ();
}
Keep looking at the picture. Windowmanagerimpl holds a reference to the Phonewindow, so it can manage the Phonewindow. It also holds a very important reference to Mglobal. This mglobal points to a singleton object of type Windowmanagerglobal, which has only one single application. In the diagram, I explained that Windowmanagerglobal maintains the decorview of all windows within the application and Viewrootimpl associated with each decorview. This is why I mentioned earlier, WindowManager is just an agent, the actual management function is realized through Windowmanagerglobal. We look at a source code example is more clear. Here we go!
Image
Wimdowmanagerimpl.java
public void AddView (@NonNull view view, @NonNull viewgroup.layoutparams params) {
...
Mglobal.addview (view, params, Mcontext.getdisplay (), Mparentwindow);
is actually achieved through the Windowmanagerglobal.
}
As you can see from the code above, Windowmanagerimpl is really just a proxy for windowmanagerglobal. At the same time, the above method is very important in the entire Android view framework process. We know that the interface will start rendering after the activity executes Onresume (). The reason is that at Onresume (), the WindowManager's AddView () method is called (The AddView () method of the Windowmanagerglobal is actually called), and the view is added to the window. The combination of this article "may be the simplest in history!" A picture of 3 minutes to let you understand the activity to start the process, do not look regret! Http://www.jianshu.com/p/9ecea420eb52 "Look, can help you to better understand the Android view frame.
Activitythread.java
final void Handleresumeactivity (IBinder token,
Boolean Clearhide, Boolean Isforward, boolean reallyresume, int seq, String reason) {
...
Viewmanager wm = A.getwindowmanager ();
Get WindowManager, actually is Windowmanagerimpl
...
Wm.addview (decor, L);
Add a View
...
Wm.updateviewlayout (decor, L);
It's going to be here when it needs refreshing.
...
}
As you can see from above, when activity executes Onresume (), the view is added or the view is refreshed. One thing to explain: WindowManager implements the Viewmanager interface.
, Windowmanagerglobal calls AddView () to add Decorview to the array it maintains, and creates another key and extremely important object of type Viewrootimpl, which must be specifically stated. It is also stored in an array to be maintained.
Windowmanagerglobal.java
public void AddView (view view, Viewgroup.layoutparams params,
Display display, Window ParentWindow) {
...
root = new Viewrootimpl (View.getcontext (), display);
Important role debut
View.setlayoutparams (Wparams);
Mviews.add (view);
Mroots.add (root);
Save for maintenance.
Mparams.add (Wparams);
...
Root.setview (view, Wparams, Panelparentview);
Set the required properties view is Decorview,panelparentview is Phonewindow
...
}
It can be seen that Viewrootimpl is created when the activity executes Onresume (), and at this point the Decorview is passed in to let it manage.
Knowledge Point: WindowManager is created at OnCreate (). Its ability to manage Windows is actually achieved through Windowmanagerglobal. The Onresume () is the view that is added to the window by WindowManager.
Viewrootimpl
Viewrootimpl is able to interact with the system's Windowmanagerservice and manages the Decorview Drawing and window state. It's very important. Quickly find the corresponding position in the map!
Viewrootimpl is not a view, but is responsible for managing the view. It works with the system to manage the view tree within a window. You can also see that it holds a reference to the Decorview, and the view tree is the starting point for the view tree drawing. Therefore, Viewrootimpl will be a little more complicated, we need to understand more in-depth, in the diagram I marked its more important composition surface and choreographer, etc. will be mentioned later.
In this case, we have already taken the first picture together, and now the children's shoes have a general understanding of the Android view frame. The next step is to learn more about the Android drawing mechanism.
What is the reason the app is always stuck?
Here's a detailed explanation of why the view we set can be drawn to the screen? What kind of bizarre lies in the middle of this? After reading it, you'll naturally be able to understand why your app will be so stuck and start thinking about how to fix it.
Image
Also use a picture to show the process. Because the Android drawing mechanism is a little complicated, you may have 10,000 grass-mud horses in your heart at first sight.
Tell you in two pictures why your App will be stuck