关于MeasureSpeac的理解


*****这也是被问得比较多的一个点,它本身其实不难理解,但是MeasureSpeac参与到了View绘制,需要从整体来讨论它的作用*****

1. 概念

测量规格’,决定了View的尺寸,本质是一个32位int值,高两位是测量模式(SpeacMode),低30位是测量值(SpeacSize),内部源码将这两个值打包成一个值,提供了getMode和getSize方法。

SpeacMode 有三个值

UNSPECIFIED:父容器不做限制,View要多大给多大,一般是系统内部使用。

EXACTLY:父容器已经检测出View所需大小,具体值由SpeacSize决定。我们定义View的Layout_width,layout_height 为match_parent 或者具体数值时,就是EXACTLY。

AT_MOST:父容器不知道View要多大,根据View实际情况取值,但是最大不超过父容器的宽高。即使用wrap_content属性时,view的内容不确定,可以根据内容改变宽高值,但是最多只能和父容器一样大。


2.开发遇到的相关问题

自定义View中,在我们继承View的时候,会出现wrap_content属性失效。

这个问题一开始做自定义View的时候基本都会遇到,我们需要在onMeasure方法中给View指定一个默认宽高,在wrap_content时设置默认宽高就可以了;

造成这个原因就需要详细分析MeasureSpeac的原理了。

3.针对问题的原理分析

MeasureSpeac包含了两个值,它由父容器和View本身的LayoutParams共同决定,在ViewGroup中,有一个measureChildwithMargins方法

 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 = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                        + heightUsed, lp.height);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }
这个方法对子元素进行了measure,在measure之前获取了子元素的MeasureSpeac

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) {//子view 有具体dp/px值
                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;
        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;
     	......
        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
    }
UNSPECIFIED时我们暂不考虑,根据这个方法我们可以总结一下View的MeasureSpec创建规则:

父View specMode为EXACTLY时:(1)子View layoutParams 有精确值,specMode为EXACTLY,specSize为具体值,最大不超过父View
(2) 子View LayoutParams为match_parant  specMode为EXACTLY,specSize为 父容器size 

(3)子View LayoutParams为wrap_content specMode为AT_MOST,specSize为 父容器size  

父View specMode为AT_MOST时:(1)子View layoutParams 有精确值,specMode为EXACTLY,specSize为具体值,最大不超过父View
(2) 子View LayoutParams为match_parant  specMode为AT_MOST,specSize为 父容器size 

(3)子View LayoutParams为wrap_content specMode为AT_MOST,specSize为 父容器size  

根据上面总结,在自定义View中,我们设置wrap_content属性时,默认的specSize都会是父容器的size,View得宽高等于父容器宽高,布局中效果与match_parant完全一致。这时候就需要针对具体情况为View指定一个默认宽高。在系统TextView等控件源码中,针对wrap_content 方法都作出了处理,可以自己看一下源码。











































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值