美文网首页Android
如何获得MeasureSpec

如何获得MeasureSpec

作者: 吃葡萄皮不吐葡萄 | 来源:发表于2016-06-19 00:06 被阅读206次

    已知系统内部是根据每个View的MeasureSpec来得到View得宽和高,那么我们是怎么获得到每个View对应的MeasureSpec呢?

    DecorView

    对于DecorView,其MeasureSpec由窗口的尺寸和其自身的LayoutParams来共同决定的。已知,ViewRootImpl类是连接WindowManager和DecorView的纽带。在ViewRootImpl中的measureHierarchy方法中有如下的代码,它展示了DecorView的MeasureSpec的创建过程:

    childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);     //desireWindowWidth是屏幕的宽度
    childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
    performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
    

    上面代码调用的getRootMeasureSpec()方法的代码如下

    private static int getRootMeasureSpec(int windowSize, int rootDimension) {
          int measureSpec;
          switch (rootDimension){
          case ViewGroup.LayoutParams.MATCH_PARENT:
                mesureSpec = MeasureSpec.makeMeasureSpec(windowSize,MeasureSpec.EXACTLY);
                break;
          case ViewGroup.LayoutParams.WRAP_CONTENT:
                measureSpec = MeasureSpec.makeMeasureSpec(windowSize,MeasureSpec.AT_MOST);
                break;
          default:
                measureSpec = MeasureSpec.makeMeasureSpec(rootDimension,MeasureSpec.EXACTLY);
                break;
          }
          return measureSpec;
    }
    

    普通View

    普通View是指布局中的View,View的measure过程由ViewGroup传递而来,下面是ViewGroup的measureChildWidthMargins()方法

    protected void measureChildWithMargins( View child,int parentWidthMeasureSpec, int widthUsed,
                                                       int parentHeightMeasureSpec,int heightUsed){
          final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
          final int childWidthMeasureSpec=getChildMeasureSpec(parentWidthMeasureSpec,mPaddingLeft+mPaddingRight+lp.leftMargin+lp.rightMargin+widthUsed,lp.width);
          final int childHeightMeasureSpec=getHeightMeasureSpec(parentHeightMeasureSpec,mPaddingTop+mPaddingBottom+lp.topMargin+lp.bottomMargin+heightUsed,lp.height);
          child.measure(childWidthMeasureSepc, childHeightMeasureSpec);
    }
    

    在上面ViewGroup的的measureChildWithMargins()方法中,首先通过getChildMeasureSpec()方法获得子View的MeasureSpec,然后,调用child.measure()方法。那么ViewGroup中的getChildMeasureSpec()方法具体是什么样的呢?

    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){
          case MeasureSpec.EXACTLY:
                if (childDimension >= 0){
                      resultSize = childDimension;
                      resultMode = MeasureSpec.EXACTLY;
                } else if (childDimension == LayoutParams.MATCH_PARENT){
                      resultSize = size;
                      resultMode = MeasureSpec.EXACTLY;
                } else if (childDimension == LayoutParams.WRAP_CONTENT){
                      resultSize = size;
                      resultMode = MeasureSpec.AT_MOST;
                }
                break;
          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 == LayoutParam.WRAP_CONTENT){
                      resultSize = size;
                      resultMode = MeasureSpec.AT_MOST;
                }
                break;
          case MeasureSpec.UNSPECIFIED:
                if (childDimension >= 0){
                      resultSize = childDimension;
                      resultMode = MeasureSpec.EXACTLY
                } else if (childDimension == LayoutParams.MATCH_PARENT) {
                      resultSize = 0;
                      resultMode = MeasureSpec.UNSPECIFIED;
                } else if (childDimension ==== LayoutParams.UNSPECIFIED) {
                      result = 0;
                      resultMode = MeasureSpec.UNSPECIFIED;
                }
                break;
          }
          return MeasureSpec.makeMeasureSpec(resultSize,resultMode);
    }
    

    相关文章

      网友评论

        本文标题:如何获得MeasureSpec

        本文链接:https://www.haomeiwen.com/subject/mhhbdttx.html