Viewgroup measure child

原创
2016/05/24 16:52
阅读数 130

理解MeasureSpec:

  • spec(规格要求,分为 width spec 和 height spec) 是一个int型的**组合值**,包括spec mode 和 size两个值;
  • MeasureSpec 是产生spec 的工具;
  • child 的 spec 是由其自身的布局参数(layout_width,layout_height等)以及parent 的 spec 共同决定的

以宽度计算为例:

childDimension_代表_layout_width

  • 如果parent的 spec mode 是 exactly(即 parent的宽度是确定的值):

    • 如果child布局参数layout_width 是 一个具体的值(大于 0),那么child 的 spec mode 就是 exactly, 而且size 是_childDimension_;
    • 如果child布局参数layout_width 是 match_content,那么child 的 spec mode 就是 exactly, 而且size 是parent 的size
    • 如果child布局参数layout_width 是 wrap_content,那么child 的 spec mode 就是 at_most, 而且size 是parent 的size(不同的布局容器策略会不同,这里以默认的ViewGroup 为例);
  • 如果parent的 spec mode 是 exactly(即 parent的宽度是确定的值): * 看代码..

  • 如果parent的 spec mode 是 UNSPECIFIED(即 parent的宽度是确定的值): * 看代码..

参数解释:

  • spec 是这个vg 的 parent 为此 vg 赋予的 MeasureSpec
  • padding 是此 vg 的padding;
  • childDimension 是child 的 layout模式(wrap_content,即-2、match_parent,即-1) 或具体的值;

返回值是为child 产生的 MeasureSpec

//ViewGroup.java
     * Does the hard part of measureChildren: figuring out the MeasureSpec to
     * pass to a particular child. This method figures out the right MeasureSpec
     * for one dimension (height or width) of one child view.
     *
     * The goal is to combine information from our MeasureSpec with the
     * LayoutParams of the child to get the best possible results. For example,
     * if the this view knows its size (because its MeasureSpec has a mode of
     * EXACTLY), and the child has indicated in its LayoutParams that it wants
     * to be the same size as the parent, the parent should ask the child to
     * layout given an exact size.
     *
     * @param spec The requirements for this view
     * @param padding The padding of this view for the current dimension and
     *        margins, if applicable
     * @param childDimension How big the child wants to be in the current
     *        dimension
     * @return a MeasureSpec integer for the child
     */
    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);
    }

展开阅读全文
打赏
2
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
2
分享
返回顶部
顶部