android界面的measure详解

举报
程序员历小冰 发表于 2021/08/28 00:49:55 2021/08/28
6.1k+ 0 0
【摘要】 想必大家都遇到过在onCreate中调用getMeasuredHeight和getMeasuredHeight这两个函数得到的返回值是0,0的情况吧,这是为什么呢?当然是android界面初始化的相关机制导致的这个“问题”啦,我们现在就来看一下android的view和viewgroup两个类在初始化中measure相关的机制吧。 我...

想必大家都遇到过在onCreate中调用getMeasuredHeight和getMeasuredHeight这两个函数得到的返回值是0,0的情况吧,这是为什么呢?当然是android界面初始化的相关机制导致的这个“问题”啦,我们现在就来看一下android的view和viewgroup两个类在初始化中measure相关的机制吧。

我们都知道视图的绘制过程要经历三个过程,分别是onMeasure(确定大小),onLayout(确定位置)和onDraw三个方法,我们先从View类中的ViewRootImpl类中开始的,ViewRootImpl对象是view的静态内部类AttachInfo的一个成员变量,上述的绘制过程的三个方法都会在它的performTraversals()中被调用。

那我们就先分步看一下这个函数,由于这个函数比较长,这里就只给出部分代码啦。


           int childWidthMeasureSpec = getRootMeasureSpec(mWidth, lp.width);
           int childHeightMeasureSpec = getRootMeasureSpec(mHeight, lp.height);
           //TODO:measureSpec!!!!第一步啊,下一步是OnLayout
            performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
  
 

看一下getRootMeasureSpec方法,那么我们就要先介绍一下View类中的另一个静态内部类MeasureSpec啦,MeasureSpec类封装啦从父节点传递到子节点的布局的需求,每个MeasureSpec既代表了width的需求也代表了height的需求,每个measureSpec有size和mode组成,一共有三种mode:

  1. UNSPECIFIED,代表父节点不对子节点做任何限制,子节点想多大就多大
  2. EXACTLY,代表父节点已经决定了准确的大小,子节点必须遵守
  3. AT_MOST,代表子节点的大小要小于特定的大小,但是具体大小自己决定。


         private static int getRootMeasureSpec(int windowSize, int rootDimension) {
             int measureSpec;
             switch (rootDimension) {
             //TODO:Match_Parent,Wrap_content和exactly,at_most的对应关系
             case ViewGroup.LayoutParams.MATCH_PARENT:
                 // Window can't resize. Force root view to be windowSize.
                  measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
                 break;
             case ViewGroup.LayoutParams.WRAP_CONTENT:
                 // Window can resize. Set max size for root view.
                  measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
                 break;
             default:
                 // Window wants to be an exact size. Force root view to be that size.
                  measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
                 break;
              }
             return measureSpec;
          }
  
 

在performMeasure中其实并没有神马复杂的逻辑,只是调用了对象的一个成员变量mView的measure方法,将childWidthMeasureSpec,childHeightMeasureSpec传递过去,也就是MeasureSpec类中所说的一样,将父节点对子节点的布局要求传递过去。

然后我们就来到了VIew.measure(int,int)这个方法啦,我们可以注意到这是个final方法,也就是说我们不能重载这个方法,在文档中也说明啦,这个方法是为了得出这个view需要多大,而且父节点给出了宽和高的限制信息,但是实际的measure工作是onMeasure方法完成的,子类可以重载这个方法。这就是说明,view的设计师不希望用户重载measure方法,而是重载onMeasure方法。


         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
              setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                      getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
          }
  
 

         protected int getSuggestedMinimumWidth() {
             return (mBackground == null) ? mMinWidth : max(mMinWidth, mBackground.getMinimumWidth());
          }
  
 


         public static int getDefaultSize(int size, int measureSpec) {
             //TODO:onMeasure真正做事情的地方
             int result = size;
             int specMode = MeasureSpec.getMode(measureSpec);
             int specSize = MeasureSpec.getSize(measureSpec);
             switch (specMode) {
             case MeasureSpec.UNSPECIFIED:
                  result = size;  //这是原size
                 break;
             case MeasureSpec.AT_MOST:
             case MeasureSpec.EXACTLY:
                  result = specSize; //
                 break;
              }
             return result;
          }
  
 


         protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
             //TODO: getMeasuredWith==0的原因啊,在这个方法调用之前都是为0的。
              mMeasuredWidth = measuredWidth;
              mMeasuredHeight = measuredHeight;
              mPrivateFlags |= PFLAG_MEASURED_DIMENSION_SET;
          }
  
 



         protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
             //这是测量子view的大小的一个方法
             final int size = mChildrenCount; //用于for循环啊
             final View[] children = mChildren;
             for (int i = 0; i < size; ++i) {
                 final View child = children[i];
                 if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {  //针对VISIBILITY的view
                      measureChild(child, widthMeasureSpec, heightMeasureSpec); //所有的spec都是一样的
                  }
              }
          }
  
 

         protected void measureChild(View child, int parentWidthMeasureSpec,
                 int parentHeightMeasureSpec) {
             final LayoutParams lp = child.getLayoutParams();  //获得view的params
             final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                      mPaddingLeft + mPaddingRight, lp.width);//用Spec和viewGroup的paddingleft,right
             //和子view的width来计算
             final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                      mPaddingTop + mPaddingBottom, lp.height);
              child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
          }
  
 

在getChildMeasureSeec中和RootViewImpl中的getRootMeasureSpec方法类似,只不过是要考虑布局的实际大小,我在代码中有注释,大家可以自己研究。


        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;
             //通过viewGroup的Spec和子view的Spec和设定的w,h来决定子view的size和Spec
             switch (specMode) {
             // Parent has imposed an exact size on us
             case MeasureSpec.EXACTLY:  //if is exactly
                 if (childDimension >= 0) {  //这是自己设定了大小
                     //这是结果的
                      resultSize = childDimension;
                      resultMode = MeasureSpec.EXACTLY;
                  } else if (childDimension == LayoutParams.MATCH_PARENT) {  //values is -1
                     // 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) {  //只要是子view设定了specific的值,那么就一定是EXACTLY
                     // 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;  //继承viewgroup的
                  } 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: //完全让子view决定
                 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 = 0;
                      resultMode = MeasureSpec.UNSPECIFIED;
                  } else if (childDimension == LayoutParams.WRAP_CONTENT) {
                     // Child wants to determine its own size.... find out how
                     // big it should be
                      resultSize = 0;
                      resultMode = MeasureSpec.UNSPECIFIED;
                  }
                 break;
              }
             return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
          }
  
 


文章来源: blog.csdn.net,作者:程序员历小冰,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/u012422440/article/details/44706055

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。