background For Android development, during the interview, I am often asked, "Can you talk about the drawing process of View?" I also often ask the interviewer, "The drawing process of View". For developers with more than 3 years of experience, knowing the basics of onMeasure/onLayout/onDraw and what they do is enough? If you come to our company and I am your interviewer, I may examine what you have done in the past three years, what you know about View, and ask more detailed questions, such as LinearLayout's onMeasure and onLayout processes? When are they initiated and what is the execution order? If you know all the above questions, you may be ready to join our company (if you need an internal recommendation, you can contact me, both Android and IOS positions are needed). I may check where your draw canvas comes from, how it is created and displayed on the screen? See how deep you are. As the current mobile development market is gradually becoming mature and saturated, many companies that are not short of people need senior programmers. Besides, as we all know, the interview requires you to build airplanes and cannons, and then screw in the screws. For an Android developer with more than 3 or 5 years of experience, it is not easy to get along without knowing a little bit about Android. After talking about so many useless things, let's get back to today's topic, a brief analysis of Android's drawing principles. This article introduces the idea From a few relatively easy questions in the interview, we go deeper and deeper, until we get to the drawing principle of the screen. Before talking about the drawing principles of Android, let's first introduce the basic working principles of View in Android. This article will not introduce the event transmission process. How View Drawing Works Let's first understand a few important classes, which are also often asked in interviews. The relationship between Activity, Window (PhoneWindow) and DecorView To understand the relationship between the three, let's look at the code directly, starting with setContentView at the beginning of Activity (Note: the code deletes some codes that are not part of this analysis process to avoid being too long)
The setContentView of getWindow called inside is discussed next. So when is this mWindow created?
PhoneWindow is created in Activity's attach, and PhoneWindow is the implementation class of Window. Continue with the previous setContentView
In setContentView, if mContentParent is empty, installDecor will be called, and finally the layout will be infalte to mContentParent. Let's take a look at installDecor
In installDecor, a DecorView is created. Looking at the comment of mContentParent, we can know that it is mDecor itself or the contents part of mDecor. In summary, we roughly know the relationship between the three.
Understand the relationship between ViewRootImpl, WindowManager, and WindowManagerService (WMS) After looking at the relationship between the above three, we know that the layout is finally added to DecorView. So how is DecorView added to the system's Framework layer? When the Activity is ready, makeVisible in the Activity will be called and View will be added through WindowManager. The code is as follows
So what is the relationship between them? (The client and server mentioned below are the client and server concepts in Binder communication.) The following content is the key part that needs to be understood
Redrawing of View From the above relationship, ViewRootImpl is used to receive messages from WMS. Then let's take a look at some View drawing codes in ViewRootImpl. Here I would like to emphasize that ViewRootImpl has two important internal classes
Let's take a look at the ViewRootHandler class. (Take View's setVisible as an example.)
Take a look at mTraversalRunnable
In TraversalRunnable, execute doTraversal. And execute performTraversals() in doTraversal. Do you see the familiar performTraversals()? Yes, the drawing of View starts here. In ViewRootImpl's performTraversals(), this method code is very long (about 800 lines of code), the general process is
So what causes the View to be redrawn? Here are three main reasons:
View drawing process In the previous section, performTraversals() is called by WMS IPC. The drawing process of View is generally From performTraversals -> performMeasure() -> performLayout() -> performDraw(). Let's take a look at performMeasure()
Finally, the measure method of View is called, and the measure() method in View is defined as final type to ensure the execution of the entire process. performLayout() and performDraw() are similar processes. For programmers, customizing View only requires paying attention to the corresponding methods it provides, onMeasure/onLayout/onDraw. There are many online materials introducing this knowledge, and you can also easily see the code in View and ViewGroup. It is recommended to read the source code of LinerLayout to understand this part of knowledge, which will not be elaborated here. A Brief Analysis of Android's Drawing Principles Android screen drawing As for drawing, we have to start with performDraw(). Let's take a look at how this process is drawn.
Look at the code execution flow, 1—>2->3, finally get the Java layer canvas, and then perform a series of drawing operations. The canvas is obtained through Suface.lockCanvas(). So what is Surface? Here Surface is just an abstraction. When the APP creates a window, it will call WindowManager to initiate a request to the WMS service, carrying the surface object. Only when it is allocated a screen buffer can it actually correspond to a window on the screen. Let's take a look at the drawing architecture in the Framework. Better understanding of Surface Surface essentially represents only a plane. Drawing different patterns is obviously an operation, not a piece of data. Android uses the Skia drawing driver library to draw on the plane, and uses canvas to represent this function in the program. Introduction to Double Buffering Technology In ViewRootImpl, we see that after receiving the drawing message, it does not draw immediately but calls scheduleTraversals, and calls Choreographer.postCallback() in scheduleTraversals. Why is this? This actually involves the principle of screen drawing (other platforms are similar except Android). We all know that the display refreshes at a fixed frequency, such as 60Hz for iPhone and 120Hz for iPad Pro. When one frame of image is drawn and ready to draw the next frame, the display will send a vertical synchronization signal (VSync), so a 60Hz screen will send such a signal 60 times in one second. And generally speaking, in a computer system, the CPU, GPU, and display work together in a specific way: the CPU submits the calculated display content to the GPU, the GPU renders it and puts it into the frame buffer, and then the video controller takes the frame data from the frame buffer according to the VSync signal and passes it to the display for display. However, if the screen has only one buffer, when the VSync signal is sent, the screen starts to refresh, and what you see on the screen is data changing line by line. In order to make the screen look like frame by frame data, there are usually two buffers (also called double buffers). When the data is to be refreshed, the data in the other buffer is directly replaced. In double buffering technology, if the data in the buffer cannot be refreshed within a specific time (if it is 60HZ, it is within 16ms), the screen sends a VSync synchronization signal, and the switching between the two buffers cannot be completed, then it will cause a freeze. Back to scheduleTraversals(), this is where double buffering technology (or triple buffering technology) is used. Choreographer receives the synchronization signal of VSync, and when the screen is refreshed, it starts the screen refresh operation. |
>>: Comprehensive understanding of B-side product design: basic literacy
Recently, a Falcon 9 rocket from the United State...
Editor's note: When designing a UI, there are...
As an operator, event promotion (event operation)...
Since WeChat has taken over every aspect of our l...
A few years ago, the topic of legal personhood an...
The concept of the "second half of the Inter...
[[436573]] Some people think that mobile phones n...
Image @pinterest Providing value to customers usi...
Buy monthly pass at super low price First, let’s ...
Live streaming has long been the fastest and most...
Jack Trading Academy JTA: Introduction to the res...
On September 9, the eve of the traditional Chines...
2014 was a boom year for mobile healthcare. Accor...
Since the introduction of voice services on socia...
This article will analyze JD.com from the perspec...