主要介紹一下View的工作原理,還有自定義View的實現方法,在Android知識體系中,View是一個很重要的角色,簡單來說,View是Android中視覺的呈現。在界面上Android提供了一套完整的GUI庫,裏面有很多控件,但是有時候往往並不能滿足於需求,所以只有自定義View了。
除了View的三大流程之外,View常見的回調方法也是必須掌握的,比如構造方法,onAttach,onVisibilityChanged,onDetach,另外對於一些有滑動效果的自定義View,還要處理滑動事件和滑動衝突,總的來說,自定義View有幾種固定的類型,View或者ViewGroup,有的直接重寫原生控件。
1、初識ViewRoot和DecorView
在正式介紹View的三大流程之前,我們還是要了解一些基本的概念,本節主要介紹ViewRoot和DecorView的概念。
ViewRoot對應於ViewRootImpl類,他是連接WindowManager和DecorView的紐帶,View的三大流程都是通過ViewRoot來完成的,在ActivityThread中,當Activity被創建完畢後,會將DecorView添加到Window中,同時會創建ViewRootImpl對象,並將ViewRootImpl對象和DecorView建立聯繫,這個可以參照如下源碼:
root = new ViewRootImpl(view.getContext(),display);
root.setView(view,wparams,panelParentView);
View的繪製流程從ViewRoot的perfromTraversals方法開始,它經過measure,layout和draw三個過程才能最終將View繪製出來,其中measure測量view的寬和高,layout確定view在父容器的位置,draw負責將view繪製在屏幕上,針對perfromTraversals的大致流程,可以用如下圖表示:
2、理解MeasureSpec
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
public static final int EXACTLY = 1 << MODE_SHIFT;
public static final int AT_MOST = 2 << MODE_SHIFT;
public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
@MeasureSpecMode int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
public static int getMode(int measureSpec) {
//noinspection ResourceType
return (measureSpec & MODE_MASK);
}
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
- UNSPECIFIED
父容器不對View有任何的限制,要多大給多大,這種情況一般用於系統內部,表示一種測量的狀態
- EXACTLY
父容器已經檢測出View所需要的精度大小,這個時候View的最終大小就是SpecSize所指定的值,它對應於LayoutParams中的match_parent和具體的數值這兩種模式。
- AT_MOST
父容器指定了一個可用大小,即SpecSize,view的大小不能大於這個值,具體是什麼值要看不同view的具體實現。它對應於LayoutParams中wrap_content。
3、MeasureSpec 和 LayoutParams 的對應關係
下面看下DecorView的MeasureSpec創建過程:
對於DecorView來說,在ViewRootImpl中的measureHierarchy方法中有這麼一段代碼。他展示了DecorViwew的MeasureSpec創建過程,其中desiredWindowWidth和desiredWindowHeight是屏幕的尺寸
childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
接下來看下getRootMeasureSpec方法的實現:
private static int getRootMeasureSpec(int windowSize, int rootDimension) {
int measureSpec;
switch (rootDimension) {
case ViewGroup.LayoutParams.MATCH_PARENT:
// Window can't resize. Force root view to be windowSize.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
break;
case ViewGroup.LayoutParams.WRAP_CONTENT:
// Window can resize. Set max size for root view.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
break;
default:
// Window wants to be an exact size. Force root view to be that size.
measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
break;
}
return measureSpec;
}
通過上述代碼,DecorView的MesourSpec的產生過程就很明確了,具體來說其遵守瞭如下格式,根據layoutparams的寬/高的參數來劃分
- LayouParams.MATCH_PARENT:精確模式,大小就是窗口的大小;
- LayouParams.WRAP_CONTENT:最大模式,大小不定,但是不能超出屏幕的大小;
- 固定大小(比如100dp):精確模式,大小爲LayoutParams中指定的大小。
下面看下普通View的MeasureSpec創建過程:
對於普通的View來說,這裏是指我們佈局中的View,View的measure過程由ViewGroup傳遞而來,先看下ViewGroup的measureChildWithMargis方法
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec);
int size = Math.max(0, specSize - padding);
int resultSize = 0;
int resultMode = 0;
switch (specMode) {
// Parent has imposed an exact size on us
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent has imposed a maximum size on us
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
// Child wants a specific size... so be it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size, but our size is not fixed.
// Constrain child to not be bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break;
// Parent asked to see how big we want to be
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// Child wants a specific size... let him have it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should
// be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how
// big it should be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
//noinspection ResourceType
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}