invalidate()
public void invalidate() {
invalidate(true);
}
void invalidateInternal(int l, int t, int r, int b, boolean invalidateCache,
boolean fullInvalidate) {
......
// Propagate the damage rectangle to the parent view.
final AttachInfo ai = mAttachInfo;
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);
}
.......
}
调到ViewParent的 invalidateChild(),ViewParent的实现类有ViewGroup和ViewRootIml,先看ViewGroup的
public final void invalidateChild(View child, final Rect dirty) {
.......
//这是一个do while 循环 最终会调用到根View的Parent ViewRootImpl
do {
View view = null;
......
parent = parent.invalidateChildInParent(location, dirty);
......
} while (parent != null);
........
}
接下来进入ViewRootImpl
public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
.......
invalidateRectOnScreen(dirty);
return null;
}
private void invalidateRectOnScreen(Rect dirty) {
.....
if (!mWillDrawSoon && (intersected || mIsAnimating)) {
scheduleTraversals();
}
}
void scheduleTraversals() {
if (!mTraversalScheduled) {
mTraversalScheduled = true;
mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
// mChoreographer 中维护一个Handler 发送重新绘制消息,调用mTraversalRunnable的run()方法
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
if (!mUnbufferedInputDispatch) {
scheduleConsumeBatchedInput();
}
notifyRendererOfFramePending();
pokeDrawLockIfNeeded();
}
}
final class TraversalRunnable implements Runnable {
@Override
public void run() {
doTraversal();
}
}
void doTraversal() {
if (mTraversalScheduled) {
mTraversalScheduled = false;
mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);
if (mProfile) {
Debug.startMethodTracing("ViewAncestor");
}
performTraversals();
if (mProfile) {
Debug.stopMethodTracing();
mProfile = false;
}
}
}
最终调起 performTraversals(),此方法很长主要调用三个View的绘制流程的三个方法,最终在performDraw中调用起View的onDraw方法
private void performTraversals() {
......
performMeasure()
......
performLayout()
......
performDraw()
}