艺术开发-View的测量

  • 先从ViewGroup的测量说起ViewGroup的测量是从measureChildren(int widthMeasureSpec,int heightMeasureSpec)这个方法开始的,我们先看下这个方法的实现;
 /**
 遍历所有的子view去测量自己(跳过GONE类型View)
 @param widthMeasureSpec 父视图的宽详细测量值
@param heightMeasureSpec 父视图的高详细测量值
 **/  
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
        final int size = mChildrenCount;
        final View[] children = mChildren;
        for (int i = 0; i < size; ++i) {
            final View child = children[i];
            if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
            }
        }
    }

再看下measureChild方法:

/**
测量单个视图,将宽高和padding加在一起后交给getChildMeasureSpec去获得最终的测量值
@param child 需要测量的子视图
@param parentWidthMeasureSpec 父视图的宽详细测量值
@param parentHeightMeasureSpec 父视图的高详细测量值
**/  
protected void measureChild(View child, int parentWidthM    easureSpec, 
     int parentHeightMeasureSpec) { 
 // 取得子视图的布局参数 
 final LayoutParams lp = child.getLayoutParams(); 

 // 通过getChildMeasureSpec获取最终的宽高详细测量值 
 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, 
         mPaddingLeft + mPaddingRight, lp.width); 
 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, 
         mPaddingTop + mPaddingBottom, lp.height); 

 // 将计算好的宽高详细测量值传入measure方法,完成最后的测量 
 child.measure(childWidthMeasureSpec, childHeightMeasureSpec); 
}  

分析下getChildMeasureSpec方法:

/** 
  在measureChildren中最难的部分:找出传递给child的MeasureSpec。 
  目的是结合父view的MeasureSpec与子view的LayoutParams信息去找到最好的结果 
  (也就是说子view的确切大小由两方面共同决定:1.父view的MeasureSpec 2.子view的LayoutParams属性) 
   
  @param spec 父view的详细测量值(MeasureSpec) 
  @param padding view当前尺寸的的内边距和外边距(padding,margin) 
  @param childDimension child在当前尺寸下的布局参数宽高值(LayoutParam.width,height) 
 **/  
public static int getChildMeasureSpec(int spec, int padding, int childDimension) {  
    //父view的模式和大小  
    int specMode = MeasureSpec.getMode(spec);     
    int specSize = MeasureSpec.getSize(spec);     
  
    //通过父view计算出的子view = 父大小-边距(父要求的大小,但子view不一定用这个值)   
    int size = Math.max(0, specSize - padding);  
  
    //子view想要的实际大小和模式(需要计算)  
    int resultSize = 0;  
    int resultMode = 0;  
  
    //通过1.父view的MeasureSpec 2.子view的LayoutParams属性这两点来确定子view的大小  
    switch (specMode) {  
    // 当父view的模式为EXACITY时,父view强加给子view确切的值  
    case MeasureSpec.EXACTLY:  
        // 当子view的LayoutParams>0也就是有确切的值  
        if (childDimension >= 0) {  
            //子view大小为子自身所赋的值,模式大小为EXACTLY  
            resultSize = childDimension;  
            resultMode = MeasureSpec.EXACTLY;  
        // 当子view的LayoutParams为MATCH_PARENT时(-1)  
        } else if (childDimension == LayoutParams.MATCH_PARENT) {  
            //子view大小为父view大小,模式为EXACTLY  
            resultSize = size;  
            resultMode = MeasureSpec.EXACTLY;  
        // 当子view的LayoutParams为WRAP_CONTENT时(-2)      
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
            //子view决定自己的大小,但最大不能超过父view,模式为AT_MOST  
            resultSize = size;  
            resultMode = MeasureSpec.AT_MOST;  
        }  
        break;  
  
    // 当父view的模式为AT_MOST时,父view强加给子view一个最大的值。  
    case MeasureSpec.AT_MOST:  
        // 道理同上  
        if (childDimension >= 0) {  
            resultSize = childDimension;  
            resultMode = MeasureSpec.EXACTLY;  
        } else if (childDimension == LayoutParams.MATCH_PARENT) {  
            resultSize = size;  
            resultMode = MeasureSpec.AT_MOST;  
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
            resultSize = size;  
            resultMode = MeasureSpec.AT_MOST;  
        }  
        break;  
  
    // 当父view的模式为UNSPECIFIED时,子view为想要的值  
    case MeasureSpec.UNSPECIFIED:  
        if (childDimension >= 0) {  
            // 子view大小为子自身所赋的值  
            resultSize = childDimension;  
            resultMode = MeasureSpec.EXACTLY;  
        } else if (childDimension == LayoutParams.MATCH_PARENT) {  
            // 因为父view为UNSPECIFIED,所以MATCH_PARENT的话子类大小为0  
            resultSize = 0;  
            resultMode = MeasureSpec.UNSPECIFIED;  
        } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
            // 因为父view为UNSPECIFIED,所以WRAP_CONTENT的话子类大小为0  
            resultSize = 0;  
            resultMode = MeasureSpec.UNSPECIFIED;  
        }  
        break;  
    }  
    return MeasureSpec.makeMeasureSpec(resultSize, resultMode);  
}  

public static int makeMeasureSpec(int size, int mode) {
            if (sUseBrokenMakeMeasureSpec) {
                return size + mode;
            } else {
                return (size & ~MODE_MASK) | (mode & MODE_MASK);
            }
        }

总结一下:在ViewGroup中首先会调用measureChildren(int widthMeasureSpec, int heightMeasureSpec)这个方法,来遍历子view,然后在这个方法内部会调用 measureChild(child, widthMeasureSpec, heightMeasureSpec);在这个方法中最终会通过child.measure(childWidthMeasureSpec, childHeightMeasureSpec); 测量每个view,在测量每个子view之前会调用getChildMeasureSpec(parentWidthMeasureSpec, mPaddingLeft + mPaddingRight, lp.width); 这个方法来得到子view的MeasureSpec,通过上面的代码我们可以看到子view的MeasureSpec是由父view的MeasureSpec和这个子view的LayoutParams以及padding,margin值共同决定的,当父view的SpecMode是EXACTLY,子view的LayoutParams是MATCH_PARENT,那么子view的SpecMode就是EXACTLY,子view的大小就是parentSize,如果子view的LayoutParams是WRAP_CONTENT,那么子view的SpecMode就是AT_MOST,子view的大小就是parentSize,如果父View的SpecMode是AT_MOST;不论子view的LayoutParams是WRAP_CONTENT,还是MATCH_PARENT,子view的SpecMode都是AT_MOST,大小都是parentSize;当子view的LayoutParams是精确值,也就是具体的数值,不管父view的SpecMode是什么子view的SpecMode都是EXACTLY,大小是自己的具体值的大小.由此可见,当我们自定义view的时,继承view时候要对当子view是WRAP_CONTENT时候做处理,如果不做处理,他的大小就是父view的大小.

  • View的测量过程:
    上面对viewGroup的测量过程基本上弄清楚了,这样view的测量就相对简单点,我们这里指的view全部都是ViewGroup中放的控件.
    view的测量我们只需要看一个方法就行,虽然大部分代码是在measure(int widthMeasureSpec, int heightMeasureSpec)这个方法中的,但是这个 方法是final类型的,所以我么无法更改,在这个方法内部调用了onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法,我们可以重写onMeasure方法,我们看下onMeasure
    方法的实现.
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
    }</pre>

分析:我们在上面分析了ViewGroup的测量过程,最终会调用子view的Measure方法来测量子view,在子view的Measure方法中调用了onMeasure,其中onMeasure的两个参数都是从父View中传过来的.
在onMeasure方法中只调用了setMeasuredDimension()方法,在这个方法中接收两个参数,这两个参数都是通过 getDefaultSize()得到的,我们先看下getDefaultSize();

<pre>/**
   作用是返回一个默认的值,如果MeasureSpec没有强制限制的话则使用提供的大小.否则在允许范围内可任意指定大小 
   第一个参数size为提供的默认大小,第二个参数为测量的大小 
   **/  
  public static int getDefaultSize(int size, int measureSpec) {  
      int result = size;  
      int specMode = MeasureSpec.getMode(measureSpec);  
      int specSize = MeasureSpec.getSize(measureSpec);  
  
      switch (specMode) {  
      // Mode = UNSPECIFIED,AT_MOST时使用提供的默认大小  
      case MeasureSpec.UNSPECIFIED:  
          result = size;  
          break;  
      case MeasureSpec.AT_MOST:  
      // Mode = EXACTLY时使用测量的大小   
      case MeasureSpec.EXACTLY:  
          result = specSize;  
          break;  
      }  
      return result;  
  }  

        通过这个方法我们更能清楚的看到为什么上面说的当view的Layoutparams是WRAP_CONTENT要在onMeasure里面做处理,如果不错处理返回的都是parentSize.

再看下setMeasuredDimension();


/** 
 * 这个方法必须由onMeasure(int, int)来调用,来存储测量的宽,高值。 
 */  

protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {  
    mMeasuredWidth = measuredWidth;  
    mMeasuredHeight = measuredHeight;  
  
    mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;  
}  

  • 这个方法必须由onMeasure(int, int)来调用,来存储测量的宽,高值。
 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
       boolean optical = isLayoutModeOptical(this);
       if (optical != isLayoutModeOptical(mParent)) {
           Insets insets = getOpticalInsets();
           int opticalWidth  = insets.left + insets.right;
           int opticalHeight = insets.top  + insets.bottom;
           measuredWidth  += optical ? opticalWidth  : -opticalWidth;
           measuredHeight += optical ? opticalHeight : -opticalHeight;
       }
       setMeasuredDimensionRaw(measuredWidth, measuredHeight);
   }

在5.0以下的源码中会调用setMeasuredDimensionRaw();5.0以上的没有这个方法,我们来看下setMeasuredDimensionRaw()这个方法的实现.

private void setMeasuredDimensionRaw(int measuredWidth, int measuredHeight) {
        mMeasuredWidth = measuredWidth;
        mMeasuredHeight = measuredHeight;
        mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
    }

其实setMeasuredDimension()最终的作用就是保存我们的测量值,当我么继承view在这个view的onMeasrue里面必须调用setMeasuredDimension()这个方法设置测量值,不然会报错.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容