关于getChildMeasureSpec()

来源:互联网 发布:java上传文件夹到ftp 编辑:程序博客网 时间:2024/05/10 20:52
  1. /** 
  2.     * Does the hard part of measureChildren: figuring out the MeasureSpec to 
  3.     * pass to a particular child. This method figures out the right MeasureSpec 
  4.     * for one dimension (height or width) of one child view. 
  5.     * 
  6.     * The goal is to combine information from our MeasureSpec with the 
  7.     * LayoutParams of the child to get the best possible results. For example, 
  8.     * if the this view knows its size (because its MeasureSpec has a mode of 
  9.     * EXACTLY), and the child has indicated in its LayoutParams that it wants 
  10.     * to be the same size as the parent, the parent should ask the child to 
  11.     * layout given an exact size. 
  12.     * 
  13.     * @param spec The requirements for this view 
  14.     * @param padding The padding of this view for the current dimension and 
  15.     *        margins, if applicable 
  16.     * @param childDimension How big the child wants to be in the current 
  17.     *        dimension 
  18.     * @return a MeasureSpec integer for the child 
  19.     */  
  20.    public static int getChildMeasureSpec(int spec, int padding, int childDimension) {  
  21.        int specMode = MeasureSpec.getMode(spec);  
  22.        int specSize = MeasureSpec.getSize(spec);  
  23.   
  24.        int size = Math.max(0, specSize - padding);  
  25.   
  26.        int resultSize = 0;  
  27.        int resultMode = 0;  
  28.   
  29.        switch (specMode) {  
  30.        // Parent has imposed an exact size on us  
  31.        case MeasureSpec.EXACTLY:  
  32.            if (childDimension >= 0) {  
  33.                resultSize = childDimension;  
  34.                resultMode = MeasureSpec.EXACTLY;  
  35.            } else if (childDimension == LayoutParams.MATCH_PARENT) {  
  36.                // Child wants to be our size. So be it.  
  37.                resultSize = size;  
  38.                resultMode = MeasureSpec.EXACTLY;  
  39.            } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
  40.                // Child wants to determine its own size. It can't be  
  41.                // bigger than us.  
  42.                resultSize = size;  
  43.                resultMode = MeasureSpec.AT_MOST;  
  44.            }  
  45.            break;  
  46.   
  47.        // Parent has imposed a maximum size on us  
  48.        case MeasureSpec.AT_MOST:  
  49.            if (childDimension >= 0) {  
  50.                // Child wants a specific size... so be it  
  51.                resultSize = childDimension;  
  52.                resultMode = MeasureSpec.EXACTLY;  
  53.            } else if (childDimension == LayoutParams.MATCH_PARENT) {  
  54.                // Child wants to be our size, but our size is not fixed.  
  55.                // Constrain child to not be bigger than us.  
  56.                resultSize = size;  
  57.                resultMode = MeasureSpec.AT_MOST;  
  58.            } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
  59.                // Child wants to determine its own size. It can't be  
  60.                // bigger than us.  
  61.                resultSize = size;  
  62.                resultMode = MeasureSpec.AT_MOST;  
  63.            }  
  64.            break;  
  65.   
  66.        // Parent asked to see how big we want to be  
  67.        case MeasureSpec.UNSPECIFIED:  
  68.            if (childDimension >= 0) {  
  69.                // Child wants a specific size... let him have it  
  70.                resultSize = childDimension;  
  71.                resultMode = MeasureSpec.EXACTLY;  
  72.            } else if (childDimension == LayoutParams.MATCH_PARENT) {  
  73.                // Child wants to be our size... find out how big it should  
  74.                // be  
  75.                resultSize = 0;  
  76.                resultMode = MeasureSpec.UNSPECIFIED;  
  77.            } else if (childDimension == LayoutParams.WRAP_CONTENT) {  
  78.                // Child wants to determine its own size.... find out how  
  79.                // big it should be  
  80.                resultSize = 0;  
  81.                resultMode = MeasureSpec.UNSPECIFIED;  
  82.            }  
  83.            break;  
  84.        }  
  85.        return MeasureSpec.makeMeasureSpec(resultSize, resultMode);  
  86.    }  

 这个Measure算法主要是算child的测量方式和大小的。主要根据如下四种(下面所指的自身是调用此方法的viewgroup):

1. 自身的测量方式(MeasureSpec EXACTLY,AT_MOST,UNSPECIFIED)【这个是parent传过来的】
2. 自身的大小 【这个是parent传过来的】
3. 自己的padding【计算得到】
4. child的大小【child的LayoutParam得到】

根据以上四个推测出child的MeasureSpec 。也就是大小和测量方式。

child的请求大小来确定到底child应该有多宽以及应该怎么测量。

0 0
原创粉丝点击