作者: ztelur
聯(lián)系方式:segmentfault,csdn,github
本文僅供個人學習,不用于任何形式商業(yè)目的,轉載請注明原作者、文章來源,鏈接,版權歸原文作者所有。
?最近一直在研究View
的繪制相關的機制,發(fā)現需要補充一下Android View Architecture的相關知識,所以就特地研究了一下這方面的代碼,寫成本篇文章
?為了節(jié)約你的時間,本篇文章內容大致如下:
-
Activity
,DecorView
,PhoneWindow
和ViewRoot
的作用和相關關系
Android View Architecture
?先來幾張圖,大致展現一下Android 視圖架構的大概。
Activity和Window
?總所周知,Activity并不負責視圖控制,它只是控制生命周期和處理事件,真正控制視圖的是Window
。一個Activity包含了一個Window,Window才是真正代表一個窗口,也就是說Activity可以沒有Window,那就相當于是Service了。在ActivityThread
中也有控制Service
的相關函數或許正好印證了這一點。
?Activity
和Window
的第一次邂逅是在ActivityThread
調用Activity
的attach()
函數時。
//[window]:通過PolicyManager創(chuàng)建window,實現callback函數,所以,當window接收到
//外界狀態(tài)改變時,會調用activity的方法,
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) {
....
mWindow = PolicyManager.makeNewWindow(this);
//當window接收系統(tǒng)發(fā)送給它的IO輸入事件時,例如鍵盤和觸摸屏事件,就可以轉發(fā)給相應的Activity
mWindow.setCallback(this);
.....
//設置本地窗口管理器
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
.....
}
?在attach()
中,新建一個Window
實例作為自己的成員變量,它的類型為PhoneWindow
,這是抽象類Window
的一個子類。然后設置mWindow
的WindowManager
。
Window,Activity和DecorView
?DecorView
是FrameLayout
的子類,它可以被認為是Android視圖樹的根節(jié)點視圖。DecorView
作為頂級View,一般情況下它內部包含一個豎直方向的LinearLayout
,在這個LinearLayout里面有上下兩個部分(具體情況和Android版本及主體有關),上面的是標題欄,下面的是內容欄。在Activity中通過setContentView所設置的布局文件其實就是被加到內容欄之中的,而內容欄的id是content,在代碼中可以通過ViewGroup content = (ViewGroup)findViewById(R.android.id.content)來得到content對應的layout。
?Window
中有幾個視圖相關的比較重要的成員變量如下所示:
-
mDecor
:DecorView
的實例,標示Window
內部的頂級視圖 -
mContentParent
:setContetView
所設置的布局文件就加到這個視圖中 -
mContentRoot
:是DecorView
的唯一子視圖,內部包含mContentParent
,標題欄和狀態(tài)欄。
?Activity中不僅持有一個Window
實例,還有一個類型為View
的mDecor
實例。這個實例和Window
中的mDecor
實例有什么關系呢?它又是什么時候被創(chuàng)建的呢?
?二者其實指向同一個對象,這個對象是在Activity
調用setContentView
時創(chuàng)建的。我們都知道Activity
的setContentView
實際上是調用了Window
的setContentView
方法。
@Override
public void setContentView(int layoutResID) {
if (mContentParent == null) { //[window]如何沒有DecorView,那么就新建一個
installDecor(); //[window]
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
....
//[window]第二步,將layout添加到mContentParent
mLayoutInflater.inflate(layoutResID, mContentParent);
.....
}
?代碼很清楚的顯示了布局文件的視圖是添加到mContentParent
中,而且Window
通過installDecor
來新建DecorView
。
//[window]創(chuàng)建一個decorView
private void installDecor() {
if (mDecor == null) {
mDecor = generateDecor(); //直接new出一個DecorView返回
....
}
if (mContentParent == null) {
//[window] 這一步也是很重要的.
mContentParent = generateLayout(mDecor); //mContentParent是setContentVIew的關鍵啊
.....
}
....
}
protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
.......
//[window] 根據不同的style生成不同的decorview啊
View in = mLayoutInflater.inflate(layoutResource, null);
// 加入到deco中,所以應該是其第一個child
decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
mContentRoot = (ViewGroup) in; //給DecorView的第一個child是mContentView
// 這是獲得所謂的content
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
}
.....
return contentParent;
}
?從上述的代碼中,我們可以清楚的看到mDecor
和mContentParent
和mContentRoot
的關系。
?那么,Activity
中的mDecor
是何時被賦值的?我們如何確定它和Widnow
中的mDecor
指向同一個對象呢?我們可以查看ActivityThread
的handleResumeActivity
函數,它負責處理Activity
的resume
階段。在這個函數中,Android直接將Window
中的DecorView
實例賦值給Activity
。
final Activity a = r.activity;
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;
Window,DecorView 和 ViewRoot
?ViewRoot
對應ViewRootImpl
類,它是連接WindowManagerService
和DecorView
的紐帶,View的三大流程(測量(measure),布局(layout),繪制(draw))均通過ViewRoot來完成。ViewRoot
并不屬于View樹的一份子。從源碼實現上來看,它既非View的子類,也非View的父類,但是,它實現了ViewParent
接口,這讓它可以作為View
的名義上的父視圖。RootView
繼承了Handler
類,可以接收事件并分發(fā),Android的所有觸屏事件、按鍵事件、界面刷新等事件都是通過ViewRoot進行分發(fā)的。ViewRoot可以被理解為“View樹的管理者”——它有一個mView成員變量,它指向的對象和上文中Window
和Activity
的mDecor
指向的對象是同一個對象。
?我們來先看一下ViewRoot
的創(chuàng)建過程。由于ViewRoot
作為WindowMangerService
和DecorView
的紐帶,只有在WindowManager
將持有DecorView
的Window
添加進窗口管理器才創(chuàng)建。我們可以查看WindowMangerGlobal
中的addView
函數。對WindowManager
不太熟悉的同學可以參考《Window和WindowManager解析》
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
// 創(chuàng)建ViewRootImpl,然后將下述對象添加到列表中
....
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews.add(view);
mRoots.add(root);
mParams.add(wparams);
....
try {
// 添加啦!!!!!!!!這是通過ViewRootImpl的setView來完成,這個View就是DecorView實例
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
....
}
....
}
?那么,Window
是什么時候被添加到WindowManager
中的呢?我們回到ActivityThread
的handleResumeActivity
函數。我們都知道Activity的resume階段就是要顯示到屏幕上的階段,在Activity也就是DecorView
將要顯示到屏幕時,系統(tǒng)才會調用addView
方法。
?我們在handleResumeActivity
函數中找到了下面一段代碼,它調用了Activity
的makeVisible()
函數。
// ActivityThread
r.activity.makeVisible();
//Activity
//[windows] DecorView正式添加并顯示
void makeVisible() {
if (!mWindowAdded) {
ViewManager wm = getWindowManager();
wm.addView(mDecor, getWindow().getAttributes());
mWindowAdded = true;
}
mDecor.setVisibility(View.VISIBLE);
}
?我們通過源代碼發(fā)現,正式在makeVisible
函數中,系統(tǒng)進行了Window
的添加。
引用
http://wiki.jikexueyuan.com/project/deep-android-v1/surface.html
http://blog.csdn.net/guxiao1201/article/details/41744107
http://forlan.iteye.com/blog/2269381