Android 中View的绘制机制源码分析 二

尊重原创:http://blog.csdn.NET/yuanzeyao/article/details/46842891

本篇文章接着上篇文章的内容来继续讨论View的绘制机制,上篇文章中我们主要讲解了View的measure过程,今天我们就来学习ViewGroup的measure过程,由于ViewGroup只是一个抽象类,所以我们需要以一个具体的布局来分析measure过程,正如我上篇文章说的,我打算使用LinearLayout为例讲解measure过程,如果你还没有读过上篇文章,那么建议你先浏览一下上篇文章吧:Android中View的绘制机制源码分析 一

 

在进行今天的主题之前,我来给大家分享一下我最近看到并且非常喜欢的两句话吧:

1、把生命浪费在美好的事物上

2、应该有一份不以此为生的职业

喜欢第一句话的原因是因为里面包含了一种乐观的生活态度,只要一件事情你在进行的过程中能够给你带来快乐,那么我们就值得花时间做,喜欢第二句话的原因是作为程序员这个职业以后转型的问题也是值得我们考虑的,相信大家也都听说过程序员是吃青春饭的职业,当你到35-40岁已经年老色衰的时候,你不得不考虑转型了,有部分转型为管理人才,有些人完全转型,干着和IT毫无关系的职业,所以我们是不是现在就要想想我们有没有一份不以此为生的职业呢?好吧  扯淡就扯到这里吧,下面我们步入正题。

 

我们来分析今天的第一个问题:你对layout_weight属性知多少?

相信大多数同学会说这个属性就是标明一个View在父View中所占的权重(比例),这个解释对吗?我们暂且不做评论,我们使用两个例子来验证一下:

example 1:

 

[html] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.  >  
  7.     <TextView  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="0dip"  
  10.         android:layout_weight="2"  
  11.         android:text="New Text"  
  12.         android:background="#998877"  
  13.         android:id="@+id/textView"  
  14.         />  
  15.   
  16.     <TextView  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="0dip"  
  19.         android:layout_weight="4"  
  20.         android:text="New Text"  
  21.         android:background="#334455"  
  22.         android:id="@+id/textView2"  
  23.          />  
  24. </LinearLayout></span>  


效果图如下:

 

example 2:

 

 

[html] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.  >  
  7.     <TextView  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:layout_weight="2"  
  11.         android:text="New Text"  
  12.         android:background="#998877"  
  13.         android:id="@+id/textView"  
  14.         />  
  15.   
  16.     <TextView  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="match_parent"  
  19.         android:layout_weight="4"  
  20.         android:text="New Text"  
  21.         android:background="#334455"  
  22.         android:id="@+id/textView2"  
  23.          />  
  24. </LinearLayout>  
  25. </span>  

效果图如下:

 



在第一张图片中,上面的TextView的weidht是2,下面的TextView的weight是4,所以上面的TextView的高度是下面TextView高度的一半,注意此时两个TextView的layout_height都是0dip,再看下面的一张图,同样上面的TextView和下面TextView的weight分别是2和4,唯一不同的是它们的layout_height变为了match_parent,此时上面的高度确实下面的两倍

所以从第一张图片看来,layout_weight好像是代表比例的,但是从第二张图片看,刚好是相反的,我们今天就带着这个疑问开始分析LinearLayout的measure源码吧

 

LinearLayout的measuer调用的是View中的measure方法,从上篇文章中我们知道measure会调用onMeasure方法,所以直接从LinearLayout的onMeasure开始分析:

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">    @Override  
  2.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  3.         if (mOrientation == VERTICAL) {  
  4.             measureVertical(widthMeasureSpec, heightMeasureSpec);  
  5.         } else {  
  6.             measureHorizontal(widthMeasureSpec, heightMeasureSpec);  
  7.         }  
  8.     }</span>  


看了源码是不是觉得so easy!,在onMeasure主要根据当前的LinearLayout是横向还是纵向,分别调用measureVertical方法和measureHorizontal方法,这里我们以纵向为例,看看measureVertical代码,由于代码比较长,我们分段分析:

 

Section one:

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">    void measureVertical(int widthMeasureSpec, int heightMeasureSpec) {  
  2.         //用来存储所有的子View使用的高度  
  3.         mTotalLength = 0;  
  4.         int maxWidth = 0;  
  5.         int alternativeMaxWidth = 0;  
  6.         int weightedMaxWidth = 0;  
  7.         boolean allFillParent = true;  
  8.         //所有View的weight的和  
  9.         float totalWeight = 0;  
  10.         //获得子View的个数  
  11.         final int count = getVirtualChildCount();  
  12.         //widthMeasureSpec和heightMeasureSpec就是父View传递进来的,这里拿到父View的mode  
  13.         final int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  14.         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);</span>  



 

这里定义了几个重要的变量,mTotalLength,用来存储所有子View的高度,count存在子View的个数,widthMode和heightMode用来存储父View的mode(如果对于mode不熟,我以看我前面的一篇文章)。

 

Section Two:

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">    //遍历所有的子View,获取所有子View的总高度,并对每个子View进行measure操作  
  2.         for (int i = 0; i < count; ++i) {  
  3.             final View child = getVirtualChildAt(i);  
  4.   
  5.             if (child == null) {  
  6.                 //如果child 是Null,则mTotalLength加0  
  7.                 mTotalLength += measureNullChild(i);  
  8.                 continue;  
  9.             }  
  10.   
  11.             if (child.getVisibility() == View.GONE) {  
  12.                //如果child不可见,则跳过  
  13.                i += getChildrenSkipCount(child, i);  
  14.                continue;  
  15.             }  
  16.             //拿到child的LayoutParams  
  17.             LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();  
  18.             //将weight的值加到totalWeight,weight的值就是xml文件中的layout_weight属性的值  
  19.             totalWeight += lp.weight;  
  20.               
  21.             if (heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0) {  
  22.                 /** 
  23.                   如果父View的mode是EXACTLY,并且height==0 并且lp.weight>0(就是我们上面的例子中的第一张图的情况) 
  24.                   那么就先不measure这个child,直接把topMargin和bottoMargin等属性加到totaoLength中 
  25.                 */  
  26.                 final int totalLength = mTotalLength;  
  27.                 mTotalLength = Math.max(totalLength, totalLength + lp.topMargin + lp.bottomMargin);  
  28.             } else {  
  29.                 int oldHeight = Integer.MIN_VALUE;  
  30.                 //如果父View不是EXACLTY,那么将子View的height变为WRAP_CONTENT  
  31.                 if (lp.height == 0 && lp.weight > 0) {  
  32.                     // heightMode is either UNSPECIFIED or AT_MOST, and this  
  33.                     // child wanted to stretch to fill available space.  
  34.                     // Translate that to WRAP_CONTENT so that it does not end up  
  35.                     // with a height of 0  
  36.                     oldHeight = 0;  
  37.                     lp.height = LayoutParams.WRAP_CONTENT;  
  38.                 }  
  39.   
  40.                 // Determine how big this child would like to be. If this or  
  41.                 // previous children have given a weight, then we allow it to  
  42.                 // use all available space (and we will shrink things later  
  43.                 // if needed).  
  44.                 measureChildBeforeLayout(  
  45.                        child, i, widthMeasureSpec, 0, heightMeasureSpec,  
  46.                        totalWeight == 0 ? mTotalLength : 0);  
  47.   
  48.                 if (oldHeight != Integer.MIN_VALUE) {  
  49.                    lp.height = oldHeight;  
  50.                 }  
  51.   
  52.                 final int childHeight = child.getMeasuredHeight();  
  53.                 final int totalLength = mTotalLength;  
  54.                 mTotalLength = Math.max(totalLength, totalLength + childHeight + lp.topMargin +  
  55.                        lp.bottomMargin + getNextLocationOffset(child));  
  56.   
  57.                 if (useLargestChild) {  
  58.                     largestChildHeight = Math.max(childHeight, largestChildHeight);  
  59.                 }  
  60.             }</span>  


这段代码是整个measureVertical最核心的部分了,我已经在代码中加入了相应的注释,这里我只想说的是为什么child.getLayoutParam能够直接强制转换为LinearLayout.LayoutParams,这个问题我们先保留吧,我打算后面的文章中专门分析一下这个LayoutParams这个对象。

 

 

我们发现在measureVertical中调用了一个measureChildBeforeLayout方法,我们先看看它传入的几个参数,我们发现最后一个参数听奇怪的,totalWeight==0?mTotalLength:0,也就是说对于一个View,如果这个View之前的View没有设置过layout_weight属性,那么这个参数等于mTotalLength,如果有设置过,那么传0,我们先进入measureChildBeforeLayout方法看看:

 

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">   void measureChildBeforeLayout(View child, int childIndex,  
  2.             int widthMeasureSpec, int totalWidth, int heightMeasureSpec,  
  3.             int totalHeight) {  
  4.         measureChildWithMargins(child, widthMeasureSpec, totalWidth,  
  5.                 heightMeasureSpec, totalHeight);  
  6.     }</span>  



 

其实就是调用父类ViewGroup的measureChildWidthMargins方法,这个方法我们在前篇文章已经分析过了,这里我们就不分析了,它就是对子View进行measure方法,只不过我们这里需要注意,如果前面有View设置了layout_weight属性,那么这里的totalHeight就是0,在执行完了measureChildBeforeLayout方法后,child的高度就知道了,就将child的高度累加到mTotalHeight中。

 

Section Three:

 

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">//将所有View的高度赋值给heightSize;  
  2.         int heightSize = mTotalLength;  
  3.   
  4.           
  5.         heightSize = Math.max(heightSize, getSuggestedMinimumHeight());  
  6.           
  7.         //这里对heightSize再次赋值,不过如果LinearLayout是xml文件的根标签,并且设置到Activity的话  
  8.         //此时heightSize的大小就是屏幕的高度,我们暂时就考虑等于屏幕高度的情况,其他情况类似  
  9.         heightSize = resolveSize(heightSize, heightMeasureSpec);  
  10.           
  11.         //屏幕的高度还剩下delta,如果对于我们上面第一张图,delta>0,对于第二张图则<0  
  12.         int delta = heightSize - mTotalLength;  
  13.         if (delta != 0 && totalWeight > 0.0f) {  
  14.             //如果设置了weightsum属性,这weightSum等于weightsum的属性,否则等于totalWeight  
  15.             float weightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight;  
  16.   
  17.             mTotalLength = 0;  
  18.             //重新遍历所有的子View  
  19.             for (int i = 0; i < count; ++i) {  
  20.                 final View child = getVirtualChildAt(i);  
  21.                 //如果子View不可见,直接跳过  
  22.                 if (child.getVisibility() == View.GONE) {  
  23.                     continue;  
  24.                 }  
  25.                   
  26.                 LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) child.getLayoutParams();  
  27.                   
  28.                 float childExtra = lp.weight;  
  29.                 //如果设置了weight属性  
  30.                 if (childExtra > 0) {  
  31.                     // Child said it could absorb extra space -- give him his share  
  32.                     //从delta中分到(weight/weightSum)*delta,注意这里delta可能<0  
  33.                     int share = (int) (childExtra * delta / weightSum);  
  34.                     weightSum -= childExtra;  
  35.                     delta -= share;  
  36.   
  37.                     final int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec,  
  38.                             mPaddingLeft + mPaddingRight +  
  39.                                     lp.leftMargin + lp.rightMargin, lp.width);  
  40.   
  41.                     // TODO: Use a field like lp.isMeasured to figure out if this  
  42.                     // child has been previously measured  
  43.                     if ((lp.height != 0) || (heightMode != MeasureSpec.EXACTLY)) {  
  44.                         /** 
  45.                         记得heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0吗 
  46.                         这个是Section Two的一个判断条件,也就是说如果走到这里,说明这个View前面已经measure过 
  47.                         现在要将share的值加入到高度上,所以要重新measure 
  48.                          
  49.                         */  
  50.                         int childHeight = child.getMeasuredHeight() + share;  
  51.                         if (childHeight < 0) {  
  52.                             childHeight = 0;  
  53.                         }  
  54.                           
  55.                         child.measure(childWidthMeasureSpec,  
  56.                                 MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));  
  57.                     } else {  
  58.                         /** 
  59.                         由于走到Section Two中走到heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0时,是直接跳过的 
  60.                         所以没有测量过,所以在这里对View进行测量 
  61.                         */  
  62.                         child.measure(childWidthMeasureSpec,  
  63.                                 MeasureSpec.makeMeasureSpec(share > 0 ? share : 0,  
  64.                                         MeasureSpec.EXACTLY));  
  65.                     }  
  66.                 }  
  67.   
  68.                 final int margin =  lp.leftMargin + lp.rightMargin;  
  69.                 final int measuredWidth = child.getMeasuredWidth() + margin;  
  70.                 maxWidth = Math.max(maxWidth, measuredWidth);  
  71.   
  72.                 boolean matchWidthLocally = widthMode != MeasureSpec.EXACTLY &&  
  73.                         lp.width == LayoutParams.MATCH_PARENT;  
  74.   
  75.                 alternativeMaxWidth = Math.max(alternativeMaxWidth,  
  76.                         matchWidthLocally ? margin : measuredWidth);  
  77.   
  78.                 allFillParent = allFillParent && lp.width == LayoutParams.MATCH_PARENT;  
  79.   
  80.                 final int totalLength = mTotalLength;  
  81.                 mTotalLength = Math.max(totalLength, totalLength + child.getMeasuredHeight() +  
  82.                         lp.topMargin + lp.bottomMargin + getNextLocationOffset(child));  
  83.             }  
  84.   
  85.             // Add in our padding  
  86.             mTotalLength += mPaddingTop + mPaddingBottom;  
  87.             // TODO: Should we recompute the heightSpec based on the new total length?  
  88.         } else {  
  89.             alternativeMaxWidth = Math.max(alternativeMaxWidth,  
  90.                                            weightedMaxWidth);  
  91.         }  
  92.   
  93.         if (!allFillParent && widthMode != MeasureSpec.EXACTLY) {  
  94.             maxWidth = alternativeMaxWidth;  
  95.         }  
  96.           
  97.         maxWidth += mPaddingLeft + mPaddingRight;  
  98.   
  99.         // Check against our minimum width  
  100.         maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());  
  101.         //所有的孩子View测量完毕,为自己设置大小  
  102.         setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec), heightSize);</span>  


这段代码主要是来重新绘制设置有layout_weight属性的子View,首先计算LinearLayout能够提供的高度大小heightSize,正如注释里面说的,在上面的两个例子中,heightSize都是屏幕的高度,然后通过heightSize和mTotalLenght计算还剩下的高度,然后将这些高度按照weight的比例分配给相应的View,然后调用View的measure方法,我们现在来解释上面的两个例子吧:

 

第一个例子:两个TextView的高度都是0dip,layout_weight分别是2 和 4,LinearLayout的mode=EXACTLY

从Section Two开始,条件满足heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0 所以在SectionTwo执行完后两个TextView是没有执行measure的,所以mTotalLenght等于0。

进入Section Three,此时heightSize等于屏幕的高度,所以delta=heightSize-mTotalLenght=屏幕高度。weightSum=2+4=6.在遍历子View的时候,通过计算第一个TextView的高度是:屏幕高度*(2/6),并且delta=delta-屏幕高度*(2/6).weightSum=6-2=4.

由于第一个TextView不满足条件(lp.height != 0) || (heightMode != MeasureSpec.EXACTLY),所以执行else里面的逻辑:

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">child.measure(childWidthMeasureSpec,  
  2.                                 MeasureSpec.makeMeasureSpec(share > 0 ? share : 0,  
  3.                                         MeasureSpec.EXACTLY));</span>  

所以第一个TextView的高度就是屏幕的1/3. 

 

 

遍历完第一个TextView之后,遍历第二个TextView,同样的道理第二个 TextView的高度等于delta*(4/4),也就是等于delta的值,其实也就是 屏幕高度*(4/6)。

 

第二个例子:两个TextView的高度都是match_parent,layout_weight分别是2和4 ,LinearLayout的mode=EXACTLY

从Section Two开始,条件不满足heightMode == MeasureSpec.EXACTLY && lp.height == 0 && lp.weight > 0 ,所以执行到了else里面的逻辑

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">measureChildBeforeLayout(  
  2.                        child, i, widthMeasureSpec, 0, heightMeasureSpec,  
  3.                        totalWeight == 0 ? mTotalLength : 0);</span>  


此时totalWeight明显不等0,所以measureChildBeforeLayout最后一个参数明显是0,所以导致第一个View的高度绘制出来及时heightMeasureSpec的size,也就是屏幕的高度(原因见我上篇文章的分析)。同样的道理对于第二个TextView测量后高度也是整个屏幕的高度。所以导致这里算出的delta=(-屏幕的高度),也就是说是个负数,进入Section Three,很明显满足了(lp.height != 0) || (heightMode != MeasureSpec.EXACTLY)这个条件,所以执行如下代码:

 

 

[java] view plain copy
 
 print?
  1. <span style="font-family:Comic Sans MS;font-size:18px;">int childHeight = child.getMeasuredHeight() + share;  
  2.                         if (childHeight < 0) {  
  3.                             childHeight = 0;  
  4.                         }  
  5.                           
  6.                         child.measure(childWidthMeasureSpec,  
  7.                                 MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));</span>  


通过Section Two的分析child.getMeasureHeight等于屏幕高度,share=-屏幕高度*(2/6),也就是说第一个TextView的高度变为 屏幕的高度*(4/6),同样的道理可以得出第二个TextView的高度 屏幕的高度*(2/6)。

 

 

对于layout_weight属性的理解应该是这样的:在SectionTwo中测量完所有的View后,将delta的值按照weight的比例给相应的 View,如果delta>0,那么那么就是在原来大小上加上相应的值,否则就是减去相应的值。

 

最后调用setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec), heightSize) 设置自身的大小。

 

相信到这里你应该已经对LinearLayout的测量过程有了很深刻的理解了吧,如果还有觉得描述不清楚的地方,欢迎留言讨论...

posted @ 2017-03-12 11:56  天涯海角路  阅读(106)  评论(0)    收藏  举报