View rendering process of Android GUI, androidgui

Source: Internet
Author: User

View rendering process of Android GUI, androidgui

In the previous article, we learned the relationship between Activity, Window, DecorView and View by tracking the source code (View article: http://www.cnblogs.com/jerehedu/p/4607599.html#gui ). How is the entire Activity interface drawn? Since DecorView is the top-level interface view of the Activity, the entire interface should be drawn from it. Next we will continue to track the source code to see if this is the case.

During the Activity startup process, mlaunchactivity and handleResumeActivity in the ActivityThread of the main thread are called. In the handleResumeActivity method, the created DecorView and WindowManagerImpl object are associated. The key source code is as follows:

public final class ActivityThread {   ……    final void handleResumeActivity(IBinder token,            boolean clearHide, boolean isForward, boolean reallyResume) {      ……            if (r.window == null && !a.mFinished && willBeVisible) {                r.window = r.activity.getWindow();                View decor = r.window.getDecorView();                decor.setVisibility(View.INVISIBLE);                ViewManager wm = a.getWindowManager();                WindowManager.LayoutParams l = r.window.getAttributes();                a.mDecor = decor;                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;                l.softInputMode |= forwardBit;                if (a.mVisibleFromClient) {                    a.mWindowAdded = true;                    wm.addView(decor, l);                }            } ……    }}

WindowManagerImpl key code:

    public final class WindowManagerImpl implements WindowManager {private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();……@Overridepublic void addView(View view, ViewGroup.LayoutParams params) {      mGlobal.addView(view, params, mDisplay, mParentWindow);}……}

WindowManagerGlobal key code:

public final class WindowManagerGlobal {    ……    public void addView(View view, ViewGroup.LayoutParams params,            Display display, Window parentWindow) {           ……        ViewRootImpl root;        View panelParentView = null;……        root.setView(view, wparams, panelParentView);           ……    }}

Based on the source code call relationship, you can obtain:

We can see that the DecorView generated in ActivityThread passes through WindowManagerImpl and WindowManagerGlobal, and finally calls the setView method in ViewRootImpl to assign the DecorView settings to the mView attribute in ViewRootImpl. By tracking ViewRootImpl, we found that the performTraversals method was finally called. The key code of this method is as follows:

private void performTraversals() {        // cache mView since it is used so much below...        final View host = mView;        ……        performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);        ……        performLayout(lp, desiredWindowWidth, desiredWindowHeight);        ……        performDraw();        ……}

From the source code above, we can see that rabbitmtraversals actually calls three key methods in turn, namely, rabbitmmeasure, rabbitmlayout, and mongomdraw.

1. The objective mmeasure method calls the mView. measure (childWidthMeasureSpec, childHeightMeasureSpec) internally. Do not forget that the mView here is the DecorView we passed in. This method is used to measure the View size. The key source code is as follows:

        private void performMeasure(int childWidthMeasureSpec, int childHeightMeasureSpec) {        Trace.traceBegin(Trace.TRACE_TAG_VIEW, "measure");        try {            mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);        } finally {            Trace.traceEnd(Trace.TRACE_TAG_VIEW);        }    }

2. When the method implements mlayout, host. layout (0, 0, host. getMeasuredWidth (), host. getMeasuredHeight () is actually called internally. This method is used to determine the position of the view. The key source code is as follows:

    private void performLayout(WindowManager.LayoutParams lp, int desiredWindowWidth,            int desiredWindowHeight) {        mLayoutRequested = false;        mScrollMayChange = true;        mInLayout = true;        final View host = mView;        ……        try {            host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());            mInLayout = false;            int numViewsRequestingLayout = mLayoutRequesters.size();            if (numViewsRequestingLayout > 0) {              ……                if (validLayoutRequesters != null) {                    // Set this flag to indicate that any further requests are happening during                   ……                    host.layout(0, 0, host.getMeasuredWidth(), host.getMeasuredHeight());                   ……                    }                }            }        } finally {            Trace.traceEnd(Trace.TRACE_TAG_VIEW);        }        mInLayout = false;    }

3. The objective mdraw method is used to draw a view, trace the source code discovery, and finally call the mView. draw (canvas) method for drawing.

After the above process, you can determine the process of drawing the View. The flowchart is as follows:

 

For questions or technical exchanges, please join the official QQ group: (452379712)

 

Author: Jerry Education
Source: http://www.cnblogs.com/jerehedu/
The copyright of this article belongs to Yantai Jerry Education Technology Co., Ltd. and the blog Park. You are welcome to repost it. However, you must keep this statement without the author's consent and provide the original article connection on the article page, otherwise, you are entitled to pursue legal liability.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.