在Activity的onCreate()方法中加載了布局,那么什么時(shí)候開始繪制UI的呢叉袍?

APP從啟動(dòng)到顯示界面始锚,前面分析APP程序啟動(dòng)流程以及加載布局的流程刽酱,那么是什么時(shí)候開始繪制UI界面的呢喳逛?其實(shí)在APP程序啟動(dòng)流程里面,當(dāng)Activity a = performLaunchActivity(r, customIntent);返回的a!=null時(shí)棵里,接著會執(zhí)行handleResumeActivity(r.token, false, r.isForward,!r.activity.mFinished && !r.startsNotResumed, r.lastProcessedSeq, reason);這句代碼润文。

1、找到handleResumeActivity()里面

這句代碼最終會調(diào)用Activity的onResume()方法殿怜,接著往下走

// TODO Push resumeArgs into the activity for consideration
r = performResumeActivity(token, clearHide, reason);

接著往下看源碼

        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 (r.mPreserveWindow) {
                    a.mWindowAdded = true;
                    r.mPreserveWindow = false;
                    // Normally the ViewRoot sets up callbacks with the Activity
                    // in addView->ViewRootImpl#setView. If we are instead reusing
                    // the decor view we have to notify the view root that the
                    // callbacks may have changed.
                    ViewRootImpl impl = decor.getViewRootImpl();
                    if (impl != null) {
                        impl.notifyChildRebuilt();
                    }
                }
                if (a.mVisibleFromClient) {
                    if (!a.mWindowAdded) {
                        a.mWindowAdded = true;
                        wm.addView(decor, l);
                    } else {
                        // The activity will get a callback for this {@link LayoutParams} change
                        // earlier. However, at that time the decor will not be set (this is set
                        // in this method), so no action will be taken. This call ensures the
                        // callback occurs with the decor set.
                        a.onWindowAttributesChanged(l);
                    }
                }

            // If the window has already been added, but during resume
            // we started another activity, then don't yet make the
            // window visible.
            } else if (!willBeVisible) {
                if (localLOGV) Slog.v(
                    TAG, "Launch " + r + " mStartedActivity set");
                r.hideForNow = true;
            }

注意 wm.addView(decor, l);wm是什么典蝌?其實(shí)是一個(gè)WindowManager, WindowManager只是一個(gè)接口,需要找到他的實(shí)體類WindowManagerImpl,接著在WindowManagerImpl找到addView()方法。

@Override
    public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
        applyDefaultToken(params);
        mGlobal.addView(view, params, mContext.getDisplay(), mParentWindow);
    }

mGlobal又是什么头谜?private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

繼續(xù)找到WindowManagerGlobal這個(gè)類骏掀,再在這個(gè)類里面找到addView() 方法

public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow) {
        if (view == null) {
            throw new IllegalArgumentException("view must not be null");
        }
        if (display == null) {
            throw new IllegalArgumentException("display must not be null");
        }
        if (!(params instanceof WindowManager.LayoutParams)) {
            throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
        }

        final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
        if (parentWindow != null) {
            parentWindow.adjustLayoutParamsForSubWindow(wparams);
        } else {
            // If there's no parent, then hardware acceleration for this view is
            // set from the application's hardware acceleration setting.
            final Context context = view.getContext();
            if (context != null
                    && (context.getApplicationInfo().flags
                            & ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
                wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
            }
        }

        ViewRootImpl root;
        View panelParentView = null;

        synchronized (mLock) {
            // Start watching for system property changes.
            if (mSystemPropertyUpdater == null) {
                mSystemPropertyUpdater = new Runnable() {
                    @Override public void run() {
                        synchronized (mLock) {
                            for (int i = mRoots.size() - 1; i >= 0; --i) {
                                mRoots.get(i).loadSystemProperties();
                            }
                        }
                    }
                };
                SystemProperties.addChangeCallback(mSystemPropertyUpdater);
            }

            int index = findViewLocked(view, false);
            if (index >= 0) {
                if (mDyingViews.contains(view)) {
                    // Don't wait for MSG_DIE to make it's way through root's queue.
                    mRoots.get(index).doDie();
                } else {
                    throw new IllegalStateException("View " + view
                            + " has already been added to the window manager.");
                }
                // The previous removeView() had not completed executing. Now it has.
            }

            // If this is a panel window, then find the window it is being
            // attached to for future reference.
            if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
                    wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
                final int count = mViews.size();
                for (int i = 0; i < count; i++) {
                    if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
                        panelParentView = mViews.get(i);
                    }
                }
            }

            root = new ViewRootImpl(view.getContext(), display);

            view.setLayoutParams(wparams);

            mViews.add(view);
            mRoots.add(root);
            mParams.add(wparams);

            // do this last because it fires off messages to start doing things
            try {
                root.setView(view, wparams, panelParentView);
            } catch (RuntimeException e) {
                // BadTokenException or InvalidDisplayException, clean up.
                if (index >= 0) {
                    removeViewLocked(index, true);
                }
                throw e;
            }
        }
    }

注意這么一行代碼root.setView(view, wparams, panelParentView);接著找到ViewRootImpl類里面的setView()方法。

接著會看到requestLayout()-->scheduleTraversals()-->mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
注意mTraversalRunnable,是個(gè)Runnable

final TraversalRunnable mTraversalRunnable = new TraversalRunnable();

doTraversal()-->performTraversals()-->performMeasure(childWidthMeasureSpec, childHeightMeasureSpec)-->performLayout(lp, mWidth, mHeight)-->performDraw()

到此UI繪制流程是在Activity調(diào)用onResume之后截驮,依次執(zhí)行Measure笑陈、Layout、Draw流程逐步將UI繪制出來葵袭。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末涵妥,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子坡锡,更是在濱河造成了極大的恐慌蓬网,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鹉勒,死亡現(xiàn)場離奇詭異帆锋,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)禽额,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進(jìn)店門窟坐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人绵疲,你說我怎么就攤上這事哲鸳。” “怎么了盔憨?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵徙菠,是天一觀的道長。 經(jīng)常有香客問我郁岩,道長婿奔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任问慎,我火速辦了婚禮萍摊,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘如叼。我一直安慰自己冰木,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布笼恰。 她就那樣靜靜地躺著踊沸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪社证。 梳的紋絲不亂的頭發(fā)上逼龟,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天,我揣著相機(jī)與錄音追葡,去河邊找鬼腺律。 笑死奕短,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的匀钧。 我是一名探鬼主播篡诽,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼榴捡!你這毒婦竟也來了杈女?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤吊圾,失蹤者是張志新(化名)和其女友劉穎达椰,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體项乒,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡啰劲,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了檀何。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蝇裤。...
    茶點(diǎn)故事閱讀 39,769評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖频鉴,靈堂內(nèi)的尸體忽然破棺而出栓辜,到底是詐尸還是另有隱情,我是刑警寧澤垛孔,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布藕甩,位于F島的核電站,受9級特大地震影響周荐,放射性物質(zhì)發(fā)生泄漏狭莱。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一概作、第九天 我趴在偏房一處隱蔽的房頂上張望腋妙。 院中可真熱鬧,春花似錦讯榕、人聲如沸骤素。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谆甜。三九已至垃僚,卻和暖如春集绰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背谆棺。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工栽燕, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留罕袋,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓碍岔,卻偏偏與公主長得像浴讯,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子蔼啦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評論 2 354