目錄:
在之前看書(shū)的時(shí)候,就知道了invalidate方法最終會(huì)去調(diào)用onDraw()方法,但是具體怎么調(diào)用卻一直沒(méi)深究過(guò)
我追尋源碼進(jìn)去呻顽,發(fā)現(xiàn)我們其實(shí)調(diào)用的是View中的invalidate()
ViewGroup.java
invalidate()
public void invalidate(boolean invalidateCache) {
invalidateInternal(0, 0, mRight - mLeft, mBottom - mTop, invalidateCache, true);
}
我們繼續(xù)跟蹤下去雹顺,
invalidateInternal()
void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
boolean fullInvalidate) {
//...
final ViewParent p = mParent;
if (p != null && ai != null && l < r && t < b) {
final Rect damage = ai.mTmpInvalRect;
damage.set(l, t, r, b);
p.invalidateChild(this, damage);
}
//...
}
這里可以看到,我們mParent對(duì)象調(diào)用了invalidateChild()方法廊遍,
invalidateChild()
public final void invalidateChild(View child, final Rect dirty) {
//...
do {
parent = parent.invalidateChildInParent(location, dirty);
} while (parent != null);
//...
}
這個(gè)方法里面嬉愧,do~while循環(huán)中一直在循環(huán)調(diào)用invalidateChildInParent()方法,通過(guò)這個(gè)操作喉前,可以一直向上查找父布局没酣,直到找到最頂層的View,當(dāng)他沒(méi)有parentView時(shí)卵迂,就會(huì)退出這個(gè)循環(huán)
invalidateChildInParent()
public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
//...
return mParent;
}
在內(nèi)部的這些View都是調(diào)用的是ViewGroup的invalidateChildInParent()方法裕便,但是當(dāng)我們向上查找到最外層時(shí),最外層View是沒(méi)有父布局的见咒,所以這個(gè)時(shí)候闪金,最外層View會(huì)調(diào)用View中的invalidateChildInParent()方法。我們現(xiàn)在就去找最外層的View,在ViewRootImpl類中
ViewRootImpl.java
invalidateChildInParent()
public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
checkThread();
//...
invalidateRectOnScreen(dirty);
}
追到這里時(shí),我們發(fā)現(xiàn)了另一個(gè)知識(shí)點(diǎn)角虫,==為什么不能在子線程中更新UI?==
//TextView.java
private void setText(CharSequence text, BufferType type,
boolean notifyBefore, int oldlen) {
//...
if (mLayout != null) {
checkForRelayout();
}
//...
}
private void checkForRelayout() {
if (//...){
//...
invalidate();
} else {
//...
invalidate();
}
}
//ImageView.java
public void setImageResource(@DrawableRes int resId) {
//...
invalidate();
}
因?yàn)槲覀兏耈I時(shí)漏设,如setText()、setImageResource()等等今妄,都會(huì)先去調(diào)用invalidate()方法郑口,然后最終去調(diào)用checkThread()這個(gè)方法,而這個(gè)方法就是用來(lái)檢測(cè)線程
checkThread()
void checkThread() {
if (mThread != Thread.currentThread()) {
throw new CalledFromWrongThreadException(
"Only the original thread that created a view hierarchy can touch its views.");
}
}
上面的if判斷中盾鳞,mThread是在ViewRootImpl的構(gòu)造函數(shù)中創(chuàng)建的犬性,而Thread.currentThread()是返回的當(dāng)前線程,所以如果是在在一個(gè)線程中(如主線程)開(kāi)的子線程腾仅,那么Thread.currentThread()就代表的子線程乒裆,而mThread就代表的原始線程,所以就會(huì)拋出異常
好推励,現(xiàn)在回到ViewRootImpl的invalidateChildInParent()方法中鹤耍,除了checkThread()方法外,invalidateRectOnScreen()方法是值得我們關(guān)注的验辞,
invalidateRectOnScreen()
private void invalidateRectOnScreen(Rect dirty) {
//...
if (!mWillDrawSoon && (intersected || mIsAnimating)) {
scheduleTraversals();
}
}
在他里面稿黄,我們看到有scheduleTraversals()方法,繼續(xù)追
scheduleTraversals()
void scheduleTraversals() {
if (!mTraversalScheduled) {
//...
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
//...
}
}
到了這里跌造,我們看mTraversalRunnable這個(gè)對(duì)象杆怕,
final TraversalRunnable mTraversalRunnable = new TraversalRunnable();
final class TraversalRunnable implements Runnable {
@Override
public void run() {
doTraversal();
}
}
doTraversal()
void doTraversal() {
if (mTraversalScheduled) {
//...
performTraversals();
//...
}
}
經(jīng)過(guò)一些列的追蹤,我們到了這里,我們找到了performTraversals()這個(gè)方法陵珍,這個(gè)方法在UI繪制里面是非常重要的寝杖,我們看一下這個(gè)方法的方法體
performTraversals()
private void performTraversals() {
//...
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
//...
performLayout(lp, mWidth, mHeight);
//...
performDraw();
//...
}
它內(nèi)部的這三個(gè)方法正是我們UI繪制流程中的三個(gè)流程:measure、layout撑教、draw朝墩,所以說(shuō)他是UI繪制流程中非常重要的方法
我們這里主要看一下performDraw()方法
performDraw()
private void performDraw() {
//...
boolean canUseAsync = draw(fullRedrawNeeded);
//...
}
它里面有一個(gè)draw()方法
draw()
private boolean draw(boolean fullRedrawNeeded) {
//...
if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset,
scalingRequired, dirty, surfaceInsets)) {
return false;
}
//...
}
可以看到醉拓,這里有一個(gè)drawSoftware()方法
drawSoftware()
private boolean drawSoftware(Surface surface, AttachInfo attachInfo, int xoff, int yoff,
boolean scalingRequired, Rect dirty, Rect surfaceInsets) {
// Draw with software renderer.
final Canvas canvas;
//...
canvas = mSurface.lockCanvas(dirty);
//...
mView.draw(canvas);
//...
}
到這里伟姐,我們終于看到了Canvas對(duì)象,它的實(shí)現(xiàn)亿卤,也是在surface上面實(shí)現(xiàn)的愤兵,并且他所展示的區(qū)域也是我們?cè)谕饷鎮(zhèn)鬟M(jìn)來(lái)的dirty
最后,我們看到了mView.draw(canvas)
這里的mView不是我們最開(kāi)始向上查找父布局的View排吴,而是最外層的View秆乳,即沒(méi)有父布局的View
draw()
public void draw(Canvas canvas) {
/*
* 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)
* 7. If necessary, draw the default focus highlight
*/
}
它內(nèi)部的注釋說(shuō)得很清楚,它先會(huì)去繪制background钻哩,然后繪制自己的內(nèi)容屹堰,最后繪制自己的子View,就這么個(gè)流程
總結(jié)
最后的最后街氢,我們?cè)賮?lái)梳理一下invlidate()的繪制流程:
我們先找到當(dāng)前的View扯键,然后它通過(guò)一個(gè)do~While循環(huán)一直向上查找最外層View,當(dāng)我們找到最外層View后珊肃,會(huì)調(diào)用最外層View的Draw方法荣刑,而draw方法流程中的dispatchDraw(canvas)就會(huì)一直往下畫,最終畫到最開(kāi)始調(diào)用invlidate()的當(dāng)前的View