Android中onMesure研究(2)

        接着对上篇文章进行谈论。Android中onMesure研究(1) 。上一篇文章介绍,每一个View在显示的时候,对onMeasure方法的调用,是从最里层的View开始measure。从数据结构上讲述,是对二叉树最外层的儿子(View)开始measure。上篇博文提到是因为递归调用。

      在一个Activity中,调用SetContentView后,最终就会触发ViewRoot中的scheduleTraversals这个函数。

    public void scheduleTraversals() {
        if (!mTraversalScheduled) {
            mTraversalScheduled = true;
            sendEmptyMessage(DO_TRAVERSAL);
        }
    }

       该函数会发送一个DO_TRAVERSAL消息,在handleMessage中处理,调用到performTraversals函数,这个函数涉及到measure和onLayout方法。该函数有600行,并且是一个递归函数。     

	 private void performTraversals() {
	        // cache mView since it is used so much below...
	        final View host = mView;
	        if (DBG) {
	            System.out.println("======================================");
	            System.out.println("performTraversals");
	            host.debug();
	        }
	        if (host == null || !mAdded)
	            return;

	        mTraversalScheduled = false;
	        mWillDrawSoon = true;
			... ... ... 
			host.measure(childWidthMeasureSpec, childHeightMeasureSpec);
			... ... ... 
			host.layout(0, 0, host.mMeasuredWidth, host.mMeasuredHeight);
			} else {
	            // We were supposed to report when we are done drawing. Since we canceled the
	            // draw, remember it here.
	            if ((relayoutResult&WindowManagerImpl.RELAYOUT_FIRST_TIME) != 0) {
	                mReportNextDraw = true;
	            }
	            if (fullRedrawNeeded) {
	                mFullRedrawNeeded = true;
	            }
	            // Try again
	            scheduleTraversals();
		}
     }
       从上述代码中,我们可以看到,onLayout方法的调用,与onMeasure调用顺序是一致的。 接下来,我们开始研究View类中的measure和layout方法了。      

你可能感兴趣的:(android,traversal,数据结构,report,layout,cache)