1.window
window標(biāo)示一個(gè)窗口的概念,是一個(gè)抽象類蜂筹。具體實(shí)現(xiàn)為phonewindow兵扬。android中所有的視圖都是通過(guò)window來(lái)實(shí)現(xiàn)的(activity dialog toast等),window并不是實(shí)際存在,而是以view的形式存在穆役。在實(shí)際使用中,無(wú)法直接訪問(wèn)window,對(duì)window的訪問(wèn)必須通過(guò)windowmanager。
注:每一個(gè)Activity都包含一個(gè)Window對(duì)象拢肆,Window對(duì)象通常由PhoneWindow實(shí)現(xiàn)刊橘。
2.windowmanager
windowmanager是外界訪問(wèn)window的入口攒庵。
3.windowmanagerservice
window最終的添加,更新败晴,刪除會(huì)通過(guò)ipc調(diào)用浓冒,交給wms去處理。
4.viewrootimpl
viewrootimpl是鏈接windowmanager和view的紐帶尖坤。它實(shí)現(xiàn)了View與WindowManager之間所需要的協(xié)議稳懒。
addView,removeView,update調(diào)用順序
WindowManagerImpl -> WindowManagerGlobal -> ViewRootImpl
5.activity中window的創(chuàng)建過(guò)程
前面是模糊的概念,下面分析下acticity中window的創(chuàng)建過(guò)程慢味。
注釋1:
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer, IVoiceInteractor voiceInteractor) {
//ContextImpl的綁定
attachBaseContext(context);
//在當(dāng)前Activity創(chuàng)建Window
mWindow = new PhoneWindow(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
...
//為Window設(shè)置WindowManager
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
//創(chuàng)建完后通過(guò)getWindowManager就可以得到WindowManager實(shí)例
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
}
在act的attach生命周期中场梆,創(chuàng)建window,windowmanager纯路。
注釋2:
在act的oncrete中或油,setcontenview主要做的事情:
1.里面創(chuàng)建了DecorView,根據(jù)Theme驰唬,F(xiàn)eature添加了對(duì)應(yīng)的布局文件
2.將r.id.xxxlayout的布局文件添加到decorview的mcontentparent中顶岸。(即decorview布局id為content的view中)
3.回調(diào)act的oncontentchanged方法通知act視圖已經(jīng)發(fā)生了變化腔彰。
注釋3:這時(shí)候需要將decorview添加到window中。通過(guò)windowmanager添加辖佣。
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
上面把DecorView添加到WindowManager萍桌,調(diào)用到的是WindowManagerGlobal.addView方法,而該方法中真正把View傳遞給WindowManager的是通過(guò)ViewRoot的setView()方法凌简,ViewRoot實(shí)現(xiàn)了View和WindowManager之間的消息傳遞上炎。
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
...
// 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();
...
try {
...
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
mAttachInfo.mOutsets, mInputChannel);
}
}
其中requestlayout完成view的三大流程(測(cè)量 布局 繪制)。
mWindowSession.addToDisplay通過(guò)ipc方式向session發(fā)送消息雏搂,最終通過(guò)mws完成window的添加流程藕施。
至此,act中window的創(chuàng)建過(guò)程分析結(jié)束凸郑。
經(jīng)過(guò)上訴分析裳食,除了知道window,windowmanager芙沥,wms诲祸,viewrootimpl等之間的聯(lián)系,也能知道日常開(kāi)發(fā)中的一些問(wèn)題而昨。
1.oncreate為什么可以在子線程刷新ui
viewrootimpl在oncreatre時(shí)還沒(méi)有創(chuàng)建起來(lái)(在onresume之后)救氯,所以不會(huì)檢查線程。
2.requestFeature 方法的位置需要放在setContentView方法之前歌憨。
在setContentView就會(huì)根據(jù)act的主題來(lái)加載布局着憨。
3.oncreate onresume中獲取view寬高為0
另外,wms添加還會(huì)驗(yàn)證token务嫡,通過(guò)對(duì)dialog中window創(chuàng)建的分析(自己去分析)甲抖,dialog需要依附act的token。所以創(chuàng)建dialog只能傳遞act的context心铃,否則將會(huì)報(bào)錯(cuò)准谚。
4.拓展: view的事件傳遞機(jī)制(如何從硬件==》mws==》viewrootimpl==》phonewindow==》decorview==》act)