Android 浅谈View的测量measure

golj9515 7年前
   <p>一般一个View的呈现基本需要三大流程measure、layout、draw,measure作为View的三大工作流程之一,也是三大流程中第一个流程,主要用于确定View的测量宽/高,该流程的执行情况将直接影响后续的两个流程,可谓是重中之重,不可不察也。其余的两个流程layout用于确定View的最终宽高和四个顶点的位置,Draw则将View绘制到屏幕上。</p>    <p style="text-align:center"><img src="https://simg.open-open.com/show/5a9007e8b91d000c3855fc99efc327d0.png"></p>    <p>讲到View的measure测量,一般会涉及到两个方法和一个类,两个方法分别是measure和onMeasure,一个类是MeasureSpec。在自定义View中MeasureSpec在measure和onMeasure两个方法中都有使用,所以为了更好地理解View的测量过程,MeasureSpec是我们首先需要理解的东西。</p>    <h3><strong>MeasureSpec</strong></h3>    <p>MeasureSpec是View的一个静态内部类,MeasureSpec类封装了父View传递给子View的布局(layout)要求,每个MeasureSpec实例代表宽度或者高度(只能是其一)要求。MeasureSpec字面意思是测量规格或者测量属性,在measure方法中有两个参数widthMeasureSpec和heightMeasureSpec,如果使用widthMeasureSpec,我们就可以通过MeasureSpec计算出宽的模式Mode和宽度的实际值,当然了也可以通过模式Mode和宽度获得一个MeasureSpec,下面是MeasureSpec的部分核心逻辑。</p>    <pre>  <code class="language-java">public class MeasureSpec {      // 进位大小为2的30次方(int的大小为32位,所以进位30位就是要使用int的最高位和倒数第二位也就是32和31位做标志位)      private static final int MODE_SHIFT = 30;            // 运算遮罩,0x3为16进制,10进制为3,二进制为11。3向左进位30,就是11 00000000000(11后跟30个0)      // (遮罩的作用是用1标注需要的值,0标注不要的值。因为1与任何数做与运算都得任何数,0与任何数做与运算都得0)      private static final int MODE_MASK  = 0x3 << MODE_SHIFT;         // 0向左进位30,就是00 00000000000(00后跟30个0)      public static final int UNSPECIFIED = 0 << MODE_SHIFT;      // 1向左进位30,就是01 00000000000(01后跟30个0)      public static final int EXACTLY    = 1 << MODE_SHIFT;      // 2向左进位30,就是10 00000000000(10后跟30个0)      public static final int AT_MOST    = 2 << MODE_SHIFT;         /**       * 根据提供的size和mode得到一个详细的测量结果       */      // measureSpec = size + mode; (注意:二进制的加法,不是10进制的加法!)      // 这里设计的目的就是使用一个32位的二进制数,32和31位代表了mode的值,后30位代表size的值      // 例如size=100(4),mode=AT_MOST,则measureSpec=100+10000...00=10000..00100      public static int makeMeasureSpec(int size, int mode) {          return size + mode;      }         /**       * 通过详细测量结果获得mode       */      // mode = measureSpec & MODE_MASK;      // MODE_MASK = 11 00000000000(11后跟30个0),原理是用MODE_MASK后30位的0替换掉measureSpec后30位中的1,再保留32和31位的mode值。      // 例如10 00..00100 & 11 00..00(11后跟30个0) = 10 00..00(AT_MOST),这样就得到了mode的值      public static int getMode(int measureSpec) {          return (measureSpec & MODE_MASK);      }         /**       * 通过详细测量结果获得size       */      // size = measureSpec & ~MODE_MASK;      // 原理同上,不过这次是将MODE_MASK取反,也就是变成了00 111111(00后跟30个1),将32,31替换成0也就是去掉mode,保留后30位的size      public static int getSize(int measureSpec) {          return (measureSpec & ~MODE_MASK);      }     }  </code></pre>    <p>MeasureSpec实际上是对int类型的整数进行位运算的一个封装,其中前2位是Mode,后面30位是实际宽或高,Mode就三种情况:</p>    <ul>     <li><strong>UNSPECIFIED(未指定)</strong> 父元素不会对子元素施加任何束缚,子元素可以得到任意想要的大小;</li>     <li><strong>EXACTLY(完全)</strong> 父元素决定子元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;</li>     <li><strong>AT_MOST(至多)</strong> 子元素至多达到指定大小的值。</li>    </ul>    <p>三种模式中最常用的是EXACTLY和AT_MOST两种模式,这两种模式分别对应layout布局文件中的match_parent和wrap_content,而布局文件会转化为中layout相关属性会转换为LayoutParams,接下来我们看一下LayoutParams是如何与MeasureSpec进行逻辑交互的。</p>    <h3><strong>LayoutParams与MeasureSpec关系</strong></h3>    <p>系统内部通过MeasureSpec对View进行测量,但是我们可以通过给View设置LayoutParams来影响MeasureSpec,有关LayoutParams的更多内容可以查看 <a href="/misc/goto?guid=4959719749326134701" rel="nofollow,noindex">Android浅谈LayoutParams</a> 。在View测量的时候,系统会将LayoutParams在父ViewGroup的作用下转化为MeasureSpec,这里需要注意一点子View的MeasureSpec不是唯一有LayoutParams决定而是与父ViewGroup的MeasureSpec一起决定。在ViewGroup中无论是measureChild还是measureChildWithMargins方法中都有一个getChildMeasureSpec方法,代码如下:</p>    <pre>  <code class="language-java">protected void measureChild(Viewchild, int parentWidthMeasureSpec,   int parentHeightMeasureSpec) {   final LayoutParamslp = child.getLayoutParams();      final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,   mPaddingLeft + mPaddingRight, lp.width);   final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,   mPaddingTop + mPaddingBottom, lp.height);      child.measure(childWidthMeasureSpec, childHeightMeasureSpec);  }     public static int getChildMeasureSpec(int spec, int padding, int childDimension) {   int specMode = MeasureSpec.getMode(spec);   int specSize = MeasureSpec.getSize(spec);      int size = Math.max(0, specSize - padding);      int resultSize = 0;   int resultMode = 0;      switch (specMode) {   // Parent has imposed an exact size on us   case MeasureSpec.EXACTLY:   if (childDimension >= 0) {   resultSize = childDimension;   resultMode = MeasureSpec.EXACTLY;   } else if (childDimension == LayoutParams.MATCH_PARENT) {   // Child wants to be our size. So be it.   resultSize = size;   resultMode = MeasureSpec.EXACTLY;   } else if (childDimension == LayoutParams.WRAP_CONTENT) {   // Child wants to determine its own size. It can't be   // bigger than us.   resultSize = size;   resultMode = MeasureSpec.AT_MOST;   }   break;      // Parent has imposed a maximum size on us   case MeasureSpec.AT_MOST:   if (childDimension >= 0) {   // Child wants a specific size... so be it   resultSize = childDimension;   resultMode = MeasureSpec.EXACTLY;   } else if (childDimension == LayoutParams.MATCH_PARENT) {   // Child wants to be our size, but our size is not fixed.   // Constrain child to not be bigger than us.   resultSize = size;   resultMode = MeasureSpec.AT_MOST;   } else if (childDimension == LayoutParams.WRAP_CONTENT) {   // Child wants to determine its own size. It can't be   // bigger than us.   resultSize = size;   resultMode = MeasureSpec.AT_MOST;   }   break;      // Parent asked to see how big we want to be   case MeasureSpec.UNSPECIFIED:   if (childDimension >= 0) {   // Child wants a specific size... let him have it   resultSize = childDimension;   resultMode = MeasureSpec.EXACTLY;   } else if (childDimension == LayoutParams.MATCH_PARENT) {   // Child wants to be our size... find out how big it should   // be   resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;   resultMode = MeasureSpec.UNSPECIFIED;   } else if (childDimension == LayoutParams.WRAP_CONTENT) {   // Child wants to determine its own size.... find out how   // big it should be   resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;   resultMode = MeasureSpec.UNSPECIFIED;   }   break;   }   return MeasureSpec.makeMeasureSpec(resultSize, resultMode);  }  </code></pre>    <p>从上面可以看出,只要给出父ViewGroup的MeasureSpec和子View的LayoutParams就可以很快的确定出子View的MeasureSpec,有了MeasureSpec就可以很快确定出子View测量后的大小了。讲到这里会发现还有一种模式没有说明呢,UNSPECIFIED这种模式在下文结合代码再继续讲解,该模式主要用于系统内部多次measure的情形。</p>    <h3><strong>measure方法</strong></h3>    <p>如果只是一个View直接通过measure就可以完成测量过程,但是如果是一个ViewGroup,除了完成自己的测量外,还需要遍历测量自己的所有孩子,各个子元素都需要递归调用该过程直至所有孩子都测量完毕。</p>    <p>在直接继承自ViewGroup中自定义View中,一般我们都需要重写一个onMeasure方法,但是该方法不是必须的,通过代码可以很容易发现,因为需要我们强制重写的方法中并没有onMeasure方法,这是因为如果我们的自定义ViewGroup中子View的大小是ViewGroup直接分配的,并没有考虑子View自身大小因素,比如我们需要自定义一个相册View,每一行显示三个图片,这时候只要三个图片平均分配占满一行就可以了,不用考虑子View大小,由父ViewGroup直接赋值就可以了。但是在自定义ViewGroup时,如果想要测量子View,都是直接调用的measure方法,但是当前类中需要重写的确是onMeasure方法,这是为什么呢?先看一下View中measure方法的定义:</p>    <pre>  <code class="language-java">public final void measure(int widthMeasureSpec, int heightMeasureSpec) {   if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||                  widthMeasureSpec != mOldWidthMeasureSpec ||                  heightMeasureSpec != mOldHeightMeasureSpec) {      //...                 int cacheIndex = (mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ? -1 :                      mMeasureCache.indexOfKey(key);              if (cacheIndex < 0 || sIgnoreMeasureCache) {                  // measure ourselves, this should set the measured dimension flag back                  onMeasure(widthMeasureSpec, heightMeasureSpec);              } else {                  long value = mMeasureCache.valueAt(cacheIndex);                  // Casting a long to int drops the high 32 bits, no mask needed                  setMeasuredDimensionRaw((int) (value >> 32), (int) value);              }          }   //...          mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 |                  (long) mMeasuredHeight & 0xffffffffL); // suppress sign extension      }  }  </code></pre>    <p>从measure方法定义格式就可以知道,我们想重写该方法都不行因为是最终方法。再看修饰符是 <strong>public</strong> ,这也就意味着我们可以在任意的地方View都可以直接调用measure方法,这也是为什么有时候在一些demo代码会看到measure(0,0)这种奇怪的调用方式了,因为View只有被测量过可以知道其大小,还没被测量之前如果想知道View大小怎么办呢,那么手动测量一下就可以了,那么如果我多次调用measure方法会不会测量多次呢,这个不一定,有上面代码可以知道,当测量完成以后View的宽高值会存入一个mMeasureCache的变量中,当我们再次传入的MeasureSpec相同,,此时变回直接从mMeasureCache中将上一次存入的值直接取出来赋值到View中。</p>    <p>measure(0,0)中0代表的是什么?从measure方法的传参类型可以知晓0其实就是一个值为0的MeasureSpec,该MeasureSpec对应的模式就是UNSPECIFIED,上面说了该模式父View不会对子View添加任何限制,子View可以任意大小,这个任意大小就是子View不受父View空间约束的实际大小。下面我们通过onMeasure方法中的逻辑梳理一下measure(0,0)。</p>    <h3><strong>onMeasure方法</strong></h3>    <pre>  <code class="language-java">protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),   getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));  }     protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {   boolean optical = isLayoutModeOptical(this);   if (optical != isLayoutModeOptical(mParent)) {   Insetsinsets = getOpticalInsets();   int opticalWidth  = insets.left + insets.right;   int opticalHeight = insets.top  + insets.bottom;      measuredWidth  += optical ? opticalWidth  : -opticalWidth;   measuredHeight += optical ? opticalHeight : -opticalHeight;   }   setMeasuredDimensionRaw(measuredWidth, measuredHeight);  }     private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {   mMeasuredWidth = measuredWidth;   mMeasuredHeight = measuredHeight;      mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;  }  </code></pre>    <p>setMeasuredDimension就是设置View的宽高值,核心还是看getDefaultSize方法。</p>    <pre>  <code class="language-java">public static int getDefaultSize(int size, int measureSpec) {   int result = size;   int specMode = MeasureSpec.getMode(measureSpec);   int specSize = MeasureSpec.getSize(measureSpec);      switch (specMode) {   case MeasureSpec.UNSPECIFIED:   result = size;   break;   case MeasureSpec.AT_MOST:   case MeasureSpec.EXACTLY:   result = specSize;   break;   }   return result;  }  </code></pre>    <p>通过getDefaultSize代码可以知道,如果传入的measureSpec的模式是UNSPECIFIED,那么View的大小就是传入值size的大小,计算size代码如下:</p>    <pre>  <code class="language-java">protected int getSuggestedMinimumWidth() {   return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());  }  </code></pre>    <p>从代码可以看出如果View没有设置背景,那么View的大小就是mMinWidth,mMinWidth是在layout布局文件中设置的android:minWidth指定的值,如果这个值没有指定,则最总返回0。</p>    <p>通过getDefaultSize的实现可以知道,View的宽高由specSize决定,我们可以得出结论:直接继承View的自定义控件需要重写onMeasure方法并设置wrap_content时的自身大小,否则在布局中使用wrap_content就相当于使用match_parent,为什么是这样呢?当我们在布局中使用wrap_content时,那么它的specMode相当于AT_MOST,而在这种模式下它的宽高等于specSize,而这个specSize是通过父View传入的MeasureSpec获取到的,事实上就是父View的可以使用的大小,也是父View剩余空间的大小。很显然这种情况下View的宽高等于父View剩余空间的大小,跟在布局中使用match_parent效果完全一致。这个问题也容易解决,通过效仿getSuggestedMinimumWidth方法,给View设置一个内部的默认的宽高,当设置为wrap_content直接使用设置的默认宽高即可。对于非wrap_content,我们仍然使用系统内部的测量值即可。处理wrap_content时示例代码如下:</p>    <pre>  <code class="language-java">public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){   int widthSize = MeasureSpec.getSize(widthMeasureSpec);   int widthMode = MeasureSpec.getMode(widthMeasureSpec);   int heightSize = MeasureSpec.getSize(heightMeasureSpec);   int heightMode = MeasureSpec.getMode(heightMeasureSpec);   if(widthMode== MeasureSpec.AT_MOST&&heightMode==MeasureSpec.AT_MOST){   setMeasuredDimension(mWidth,mHeight);   }else if(widthMode== MeasureSpec.AT_MOST){   setMeasuredDimension(mWidth,heightSize);   }else if(heightMode==MeasureSpec.AT_MOST){   setMeasuredDimension(widthSize,mHeight);   }  }  </code></pre>    <p> </p>    <p>来自:http://www.sunnyang.com/585.html</p>    <p> </p>