Android110524: Window,View和WindowManager注記
Email: zcatt@163.com Blog http://zcatt.blog.chinaunix.net 內(nèi)容提要 Window, View和WindowManager的注記一點.以供備忘和參考。
聲明 僅限學習交流,禁止商業(yè)用途。轉(zhuǎn)載需注明出處。
版本記錄 Date Ver Note 2011-05-24 0.1 Draft. zcatt, Beijing 2011-05-25 0.2 add KeyguardViewHost.
Android中以Window為考察點的話, 涉及的主要接口和類有View, ViewGroup, ViewRoot, Window, PhoneWindow, WindowManagerPolice, PhoneWindowManager, WindowManager, 和WindowManagerImpl.
Window中的View ----------------------
Window是抽象類, PhoneWindow繼承實現(xiàn)Window. Android中使用的Window Object實際是PhoneWindow的實例.
Window PhoneWindow
Window中記錄了自己的View, 就是mDecor. DecorView是FrameLayout的子類. mDecor實際是Window中包含的所有View的頂層View.
- // This is the top-level view of the window, containing the window decor.
-
private DecorView mDecor;
-
-
// This is the view in which the window contents are placed. It is either
-
// mDecor itself, or a child of mDecor where the contents go.
-
private ViewGroup mContentParent;
-
-
//... ...
-
-
private final class DecorView extends FrameLayout {
-
//....
-
}
Window中還記錄了一個View, 就是mContentParent, 這就是Window的content view. 實際編程中打交道的都是這個mContentParent. Window在生成自己的mDecor時, 會根據(jù)window的屬性, 例如是否有title, 是不是dialog等等從預先定義好的layout資源中選擇一個載入. 在這些資源中都定義了一個android:id="@android:id/content"的<FrameLayout>. 而這個FrameLayout就是mContentParent指向的layout對象. 可參看installDecor()@PhoneWindow.java. 而setContentView(yourView,...)實際就是將yourView掛接到mContentParent.
ViewRoot, ViewGroup,和View ------------------------------
View的成員變量中有一個指向parent的變量. 而ViewGroup是View的子類,除了有parent, 還有指向自己容納的子view的mChildren. 而ViewRoot的mView則指向自己唯一的一個子view. 這3個類聯(lián)合使用就可以組建成一顆顆view的樹. ViewRoot是這個樹的根點, 各種ViewGroup是樹枝, 而各種view是樹葉. 在這個樹中, ViewRoot的作用很特殊需要特別說明. 對于樹的操作通常是從ViewRoot的根節(jié)點發(fā)動的, 例如requestLayout(), 實際是由ViewRoot的performTraversals()完成.
WindowManager, ViewRoot和View -------------------------------
WindowManagerImpl是WindowManager的的實現(xiàn). 它主要用于記錄一個3元對應關系<View, ViewRoot, WindowManager.LayoutParams>. 使用addView()加入WindowManger的yourView, 都會自動生成一個ViewRoot做為根點. 這通常是在activity的create后的第一次resume中完成, 而這個view就是window的decor view, 見前. 參考handleResumeActivity()@ActivityThread.java.
public final class ViewRoot extends Handler implements ViewParent , View.AttachInfo.Callbacks
ViewRoot的所有作用, 或者說它的作用, 就在于它是view樹的root. 而且ViewRoot是Handler, 在這里完成相關message的處理工作. 包括的message code如下, 引自源代碼.
- public final static int DO_TRAVERSAL = 1000;
-
public final static int DIE = 1001;
-
public final static int RESIZED = 1002;
-
public final static int RESIZED_REPORT = 1003;
-
public final static int WINDOW_FOCUS_CHANGED = 1004;
-
public final static int DISPATCH_KEY = 1005;
-
public final static int DISPATCH_POINTER = 1006;
-
public final static int DISPATCH_TRACKBALL = 1007;
-
public final static int DISPATCH_APP_VISIBILITY = 1008;
-
public final static int DISPATCH_GET_NEW_SURFACE = 1009;
-
public final static int FINISHED_EVENT = 1010;
-
public final static int DISPATCH_KEY_FROM_IME = 1011;
-
public final static int FINISH_INPUT_CONNECTION = 1012;
-
public final static int CHECK_FOCUS = 1013;
-
public final static int CLOSE_SYSTEM_DIALOGS = 1014;
Acitivity, Window和View ------------------------------
Acitivity在創(chuàng)建后的attach()@Activity.java中創(chuàng)建自己的Window, 繼而生成自己的WindowManager. 實際上這個WindowManager都是對process全局的一個singlton WindowManger的wrapper封裝. 我們暫稱這個singleton WindowManager為核心WindowManager. Activity在首次resume時會將自己的decor view加入到核心WindowManager的記錄中. (一個例外, 除了decor view, KeyguardViewHost也會調(diào)用WindowManager.addview()加入到核心WindowManager記錄中) 這樣, 核心WindowManager中記錄了process中所有已經(jīng)首次resume后activity的decore view. '首次resume'實際也是表征了activity應當擁有view了, 可以用于顯示了這個概念.
|