调用流程:ActivityThread.handleResumeActivity-->WindowManager.addView(DecorView,LayoutParams)-->WindowManagerGlobal.addView-->ViewRootImpl.setView-->requestLayout()-->scheduleTraversals()
void scheduleTraversals() {
if (!mTraversalScheduled) {
mTraversalScheduled = true;
// 发送同步屏障
mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
// 监听,等待下一次vsync信号到来
mChoreographer.postCallback(
Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
notifyRendererOfFramePending();
pokeDrawLockIfNeeded();
}
}
postSyncBarrier()
方法用于在消息队列中插入一个同步屏障。这个屏障会暂时暂停MessageQueue中的消息处理,直到屏障被清除。确保doTraversal()
优先执行
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");
}
// 这里面主要干了三件事,也就是View三大流程measure、layout、draw
performTraversals();
if (mProfile) {
Debug.stopMethodTracing();
mProfile = false;
}
}
}
看一些draw的细节
private boolean draw(boolean fullRedrawNeeded, boolean forceDraw) {
...
if (!dirty.isEmpty() || mIsAnimating || accessibilityFocusDirty) {
if (isHardwareEnabled()) {
...
// 硬件draw
mAttachInfo.mThreadedRenderer.draw(mView, mAttachInfo, this);
} else {
...
// 软件draw
if (!drawSoftware(surface, mAttachInfo, xOffset, yOffset,
scalingRequired, dirty, surfaceInsets)) {
return false;
}
}
}
...
}