1.引言
學(xué)習(xí)View這章節(jié),涉及到FramWork的WindowManagerService和ActivityManagerService。其實(shí)之前都學(xué)習(xí)過,不過在此寫一遍,增加印象。
2.正題
Window 有三種類型,分別是應(yīng)用 Window、子 Window 和系統(tǒng) Window。應(yīng)用類 Window 對(duì)應(yīng)一個(gè) Acitivity,子 Window 不能單獨(dú)存在,需要依附在特定的父 Window 中,比如常見的一些 Dialog 就是一個(gè)子 Window。系統(tǒng) Window是需要聲明權(quán)限才能創(chuàng)建的 Window,比如 Toast 和系統(tǒng)狀態(tài)欄都是系統(tǒng) Window。
Window 是分層的,每個(gè) Window 都有對(duì)應(yīng)的 z-ordered,層級(jí)大的會(huì)覆蓋在層級(jí)小的 Window 上面,這和 HTML 中的 z-index 概念是完全一致的。在三種 Window 中,應(yīng)用 Window 層級(jí)范圍是 1~99,子 Window 層級(jí)范圍是 1000~1999,系統(tǒng) Window 層級(jí)范圍是 2000~2999,我們可以用一個(gè)表格來直觀的表示:
Window | 層級(jí) |
---|---|
應(yīng)用層級(jí) | 1~99 |
子Window層級(jí) | 1000~1999 |
系統(tǒng) Window | 2000~2999 |
WindowManagerService的作用:
對(duì)Window進(jìn)行管理,例如Window上的view的增刪改查全都都需要通過WindowManagerService來做處理。
2.1 WindowManager
WindowManager是對(duì)Window操作的接口,對(duì)Window一系列的操作都是通過WindowManager操作的。下面是Window機(jī)制的UML圖解:
ViewRootImpl中的requestLayout() 方法中調(diào)用了performMeasure方法對(duì)View進(jìn)行測(cè)量,performLayout進(jìn)行布局,performDraw進(jìn)行繪制。
WindowManagerGlobal通過getWindowSession() 獲取WindowManagerService的IBinder接口。也就是返回的IWindowSession。通過調(diào)用addToDisplay 方法將mView添加到mWindow當(dāng)中。
try {
mOrigWindowType = mWindowAttributes.type;
mAttachInfo.mRecomputeGlobalAttributes = true;
collectViewAttributes();
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
} catch (RemoteException e) {
mAdded = false;
mView = null;
mAttachInfo.mRootView = null;
mInputChannel = null;
mFallbackEventHandler.setView(null);
unscheduleTraversals();
setAccessibilityFocus(null, null);
throw new RuntimeException("Adding window failed", e);
} finally {
if (restore) {
attrs.restore();
}
}
把UML圖搞懂了。那么整個(gè)Window流程基本上就差不多了。