網(wǎng)上對(duì)于View的文章可謂是'一抓一大把',也足夠說明了View在Android開發(fā)中的地位了。現(xiàn)在就準(zhǔn)備梳理一下View的工作流程,順便填一下以前理解上的坑些。
理解之前####
在正式梳理(zhuangbi)之前,我們還是先說一下一些簡單的概念,以便后面能夠更好的理解。
View工作的主要流程: Measure-->Layout-->>Draw。然后我們是分別在
onMeasure,onLayout,onDraw這三個(gè)函數(shù)中來控制View的測(cè)量,布局和繪制的。-
對(duì)于一個(gè)單個(gè)的
View,它的測(cè)量,布局和繪制都是在它的父容器被調(diào)用而進(jìn)行的,簡單的說就是,單的View的流程其實(shí)是由父容器進(jìn)行調(diào)用的。而最終其實(shí)就是頂級(jí)View的流程所分發(fā)給布局中每個(gè)View,就和下面的圖所示。
View的工作流程.png MeasureSpec的理解,這個(gè)是我認(rèn)為想要理解Measure過程必須要弄懂的。首先MeasureSpec表示的是一個(gè)32位的int值,其中高兩位表示的是SpecMode(測(cè)量模式),低的30位表示的是SpecSize(測(cè)量的具體大小)。
public static class 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(int size, int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
}
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
}
}
關(guān)于MeasureSpec的代碼主要就是上面。這里主要說兩點(diǎn):
a. << 是移位運(yùn)算,3<<30表示的是首先把3變成二進(jìn)制的11然后右邊補(bǔ)30個(gè)0所組成的一個(gè)二進(jìn)制的數(shù)。
b. MeasureSpec中其實(shí)就是保存了一個(gè)32位的int,但是利用了算法使其一個(gè)數(shù)中保存了兩個(gè)數(shù)據(jù)。也就是上面的makeMeasureSpec,getMode,getSize這三個(gè)函數(shù)。
c. 上面一共有三種模式AT_MOST,EXACTLY,UNSPECIFIED。
AT_MOST:表示的是使用的是wrap_content。
EXACTLY:表示的是使用的是match_parent和具體的數(shù)值。
UNSPECIFIED:表示的不做限制,一般不考慮。
d. 一個(gè)View或ViewGroup的MeasureSpec決定了它的Size。
Measure####
由上面我們可以知道,View測(cè)量是由最初的mDecroView開始的,測(cè)量之前,首先會(huì)根據(jù)屏幕尺寸和mDecroView的LayoutParams來確定一個(gè)屬于mDecroView的MeasureSpec,也就決定了頂級(jí)View的大小尺寸。然后就會(huì)遍歷View樹來measure所有的View了,就是一個(gè)ViewGroup``measure的過程中會(huì)遍歷測(cè)量所有的子View。
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
final int size = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < size; ++i) {
final View child = children[i];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}
上面代碼很簡單,值得注意的是,當(dāng)view處于GONE的時(shí)候是不測(cè)量它的。接著繼續(xù)看看measureChild(child, widthMeasureSpec, heightMeasureSpec)。
protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
在這個(gè)方法中,我們主要看的就是getChildMeasureSpec這個(gè)方法,從參數(shù)中可以的到,通過父容器的MeasureSpec和子元素的一些LayoutParams來確定了子元素的MeasureSpec。不過這里還是有個(gè)需要注意的地方,就是ViewGroup還提供了一個(gè)測(cè)量子View的方法measureChildWithMargins,從方法名也可以看出,就是計(jì)算子View的時(shí)候考慮了子View的Margins的存在,當(dāng)然如果你想你的自定義View(從View或者ViewGroup繼承)支持Margins,就必須重寫generateLayoutParams(AttributeSet attrs)方法。
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;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}
可以看到getChildMeasureSpec,這個(gè)方法還是比較長的,主要就是通過父容器的MeasureSpec和子元素的一些LayoutParams來計(jì)算子元素的MeasureSpec。但是這里的邏輯還是很清晰的,就是通過父容器的Mode和子元素的size來確定的。具體的計(jì)算規(guī)則從代碼中也是很容易就可以看清楚的,但是這一切規(guī)則也是建立在事實(shí)的邏輯基礎(chǔ)之上的。
比如,當(dāng)父容器的Mode為AT_MOST,子View的Layout_width為MATCH_PARENT的時(shí)候,子View的size將會(huì)成為父容器的size(沒有pading的情況下),子View的Mode將會(huì)設(shè)置成AT_MOST。
接著我們繼續(xù)看,在measureChild()方法中,其實(shí)是調(diào)用了 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);的,也就是先算出child的MeasureSpec,再調(diào)用measure(),在measure()中回調(diào)onmeasure()方法。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
在這里,主要就是獲取開始計(jì)算出的MeasureSpec中獲取數(shù)值,然后再調(diào)用setMeasuredDimension()進(jìn)行設(shè)置。不過這里需要注意的是getDefaultSize(int size,, int measureSpec)這個(gè)方法,如下所示:
public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}
在這里面,無論Mode為AT_MOST還是EXACTLY都會(huì)把值設(shè)置成specSize的值,這樣就會(huì)造成無論你使用MATCH_PARENT還是使用WRAP_CONTENT他都會(huì)直接使用specSize。相當(dāng)于設(shè)置的WRAP_CONTENT也會(huì)和MATCH_PARENT效果一樣。要想把這里理清,得注意上面 getChildMeasureSpec方法中,有一點(diǎn)規(guī)則就是:如果你的子View的高和寬不是設(shè)置的確定的數(shù)值,那么對(duì)應(yīng)子View的 MeasureSpec的size就會(huì)被設(shè)置成父容器中MeasureSpec的size(也就是父容器的大?。?。
直到這里,當(dāng)我們進(jìn)行了setMeasuredDimension()這個(gè)函數(shù)后,我們的Measure就基本結(jié)束了。
Layout####
從前面我們明白Layout也是從頂級(jí)Veiw開始的,然后在一個(gè)一個(gè)Layout,然后我們需要了解的是所謂的Layout其實(shí)就是為View確定一塊屬于它的地。
public void layout(int l, int t, int r, int b) {
if ((mPrivateFlags3 & PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT) != 0) {
onMeasure(mOldWidthMeasureSpec, mOldHeightMeasureSpec);
mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
}
int oldL = mLeft;
int oldT = mTop;
int oldB = mBottom;
int oldR = mRight;
boolean changed = isLayoutModeOptical(mParent) ?
setOpticalFrame(l, t, r, b) : setFrame(l, t, r, b);
if (changed || (mPrivateFlags & PFLAG_LAYOUT_REQUIRED) == PFLAG_LAYOUT_REQUIRED) {
onLayout(changed, l, t, r, b);
mPrivateFlags &= ~PFLAG_LAYOUT_REQUIRED;
ListenerInfo li = mListenerInfo;
if (li != null && li.mOnLayoutChangeListeners != null) {
ArrayList<OnLayoutChangeListener> listenersCopy =
(ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();
int numListeners = listenersCopy.size();
for (int i = 0; i < numListeners; ++i) {
listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
}
}
}
mPrivateFlags &= ~PFLAG_FORCE_LAYOUT;
mPrivateFlags3 |= PFLAG3_IS_LAID_OUT;
}
看一下這代碼,其實(shí)沒什么大的功能,僅僅就是調(diào)用SetFrame()來確定了mLeft,mTop,mBottom,mRight這四個(gè)值(其實(shí)就是兩對(duì)坐標(biāo)),接著又調(diào)用了onLayout()來確定子View的坐標(biāo)而已。所以這里需要我們注意的是,自定義View的時(shí)候并不需要重寫Layout方法,而是復(fù)寫onLayot()方法。
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {}
可以看到,onLayout()其實(shí)就是一個(gè)空方法。???其實(shí)也是很好理解的,因?yàn)椴煌?code>ViewGroup對(duì)其子View的布局是不相同的,就像LinearLayout和RelativeLayout一樣。
嗯....是比Measure簡單多了。
draw####
這里就更加簡單了,首先我們明白draw也是從頂級(jí)View傳遞下來的。
/*
* Draw traversal performs several drawing steps which must be executed
* in the appropriate order:
*
* 1. Draw the background
* 2. If necessary, save the canvas' layers to prepare for fading
* 3. Draw view's content
* 4. Draw children
* 5. If necessary, draw the fading edges and restore layers
* 6. Draw decorations (scrollbars for instance)
*/
// Step 1, draw the background, if needed
int saveCount;
if (!dirtyOpaque) {
drawBackground(canvas);
}
// skip step 2 & 5 if possible (common case)
final int viewFlags = mViewFlags;
boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
if (!verticalEdges && !horizontalEdges) {
// Step 3, draw the content
if (!dirtyOpaque) onDraw(canvas);
// Step 4, draw the children
dispatchDraw(canvas);
// Overlay is part of the content and draws beneath Foreground
if (mOverlay != null && !mOverlay.isEmpty()) {
mOverlay.getOverlayView().dispatchDraw(canvas);
}
// Step 6, draw decorations (foreground, scrollbars)
onDrawForeground(canvas);
// we're done...
return;
}
這里就分為6步,我是個(gè)英語渣渣,就不敢班門弄斧了。主要注意的是在這里調(diào)用了onDraw()方法,也就是我們自定義View的時(shí)候主要接觸的方法。
最后####
這里就View工作流程做了一個(gè)簡單的分析,網(wǎng)上類似的也很多,所以只是想把走過的坑給填一下
