“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Related articles: DecorView Android View DecorView Android View DecorView View rendering process (3) -Activity View -WindowManager Android View rendering process (4) -Activity View -ViewRootImpl Android View rendering process (5) -Measure Android View View drawing process (six) -Layout Android View drawing process (seven) -Draw

In the last article, we introduced Windows and Window management. Windows and WindowManager, once a DecorView is created, it is passed to the WindowManager, which manages it, eventually by calling the addView method, The ViewRootImpl is instantiated and the setView method of ViewRootImpl is called to hold the DecorView. At the end of the previous article, we also noted that View drawing is handled by the ViewRootImpl. Each application window’s DecorView has a ViewRootImpl object associated with it, and this association is maintained by The WindowManager. So we’re going to show you how the View file is drawn.

ViewRootImpl

Let’s take a look at the setView method in view Owl PL, there’s a lot of code, but here’s the important part

/** * We have one child */ public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) { synchronized (this) { if (mView == null) { mView = view; . mAdded = true; int res; /* = WindowManagerImpl.ADD_OKAY; */ // Schedule the first layout -before- adding to the window // manager, to make sure we do the relayout before receiving // any other events from the system. requestLayout(); . }}}Copy the code

As you can see in the code above, the DecorView passed in is saved using mView and the UI is updated using the requestLayout method

public void requestLayout() {
        if (!mHandlingLayoutInLayoutRequest) {
            checkThread();
            mLayoutRequested = true;
            scheduleTraversals();
        }
    }
Copy the code

The UI is drawn on the main thread, so the checkThread is used to perform a check. ScheduleTraversals are then called to plan the rendering.

void scheduleTraversals() { if (! mTraversalScheduled) { mTraversalScheduled = true; mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier(); mChoreographer.postCallback( Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null); if (! mUnbufferedInputDispatch) { scheduleConsumeBatchedInput(); } notifyRendererOfFramePending(); pokeDrawLockIfNeeded(); }}Copy the code

A mTraversalBarrier is used: the Handler’s synchronization barrier. It intercepts Looper’s attempts to get and distribute synchronous messages. After joining the synchronization barrier, Looper will only get and process asynchronous messages. If there are no asynchronous messages, Looper will block. In other words, rendering operations on the View can take precedence. MTraversalRunnable A Runnable, which will eventually call the run method, which will call the doTraversal method, which will be used to render the View:

final class TraversalRunnable implements Runnable { @Override public void run() { doTraversal(); } } void doTraversal() { if (mTraversalScheduled) { mTraversalScheduled = false; mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier); if (mProfile) { Debug.startMethodTracing("ViewAncestor"); } performTraversals(); if (mProfile) { Debug.stopMethodTracing(); mProfile = false; }}}Copy the code

As you can see from the above method, performTraversals will eventually be called internally to draw. The performTraversals method is used to render a View, and the familiar measure, Layout and draw methods will be called in turn.

// Private void performTraversals() { PerformMeasure (Child Wide Measure, Child Tall Measure); PerformLayout (lp, mWidth, mHeight); // Execute draw performDraw(); }Copy the code

Here is the activity View involved in the important classes are introduced, the rest is the View of the drawing, the following article will focus on the introduction, summary, as shown below:

View of the entire drawing process as shown in the figure above, which is the focus of the introduction of the above several articles, you can download the source code of the Activity, layer by layer into View, you will understand the overall process, the source code is really a good thing, read the source code, can help us solve a lot of problems, The following will introduce the View drawing process call three important methods, but also later we do custom View process, often used methods.

The above introduction has the wrong place also please correct, welcome to leave a comment.