Chinaunix首页 | 论坛 | 博客
  • 博客访问: 388801
  • 博文数量: 120
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 741
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-27 18:15
文章分类

全部博文(120)

文章存档

2016年(13)

2015年(41)

2014年(66)

我的朋友

分类: Android平台

2014-10-24 17:36:08

转自:http://www.cnblogs.com/slider/archive/2011/11/28/2266538.html
    一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组"宽度和高度"的要求。一个MeasureSpec由大小和模式组成。它有三种模式:
    UNSPECIFIED(未指定):父元素不对子元素施加任何束缚,子元素可以得到任意想要的大小;
    EXACTLY(完全):父元素决定子元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;
    AT_MOST(至多):子元素至多达到指定大小的值。
它常用的三个函数:
    1.static int getMode(int measureSpec):根据提供的测量值(格式)提取模式(上述三个模式之一)
    2.static int getSize(int measureSpec):根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小)
    3.static int makeMeasureSpec(int size,int mode):根据提供的大小值和模式创建一个测量值(格式)
    这个类的使用呢,通常在view组件的onMeasure方法里面调用但也有少数例外,看看几个例子:

a.首先一个我们常用到的一个有用的函数,View.resolveSize(int size,int measureSpec)

点击(此处)折叠或打开

  1. public static int resolveSize(int size, int measureSpec) {
  2.         int result = size;
  3.         int specMode = MeasureSpec.getMode(measureSpec);
  4.         int specSize = MeasureSpec.getSize(measureSpec);
  5.         switch (specMode) {
  6.         case MeasureSpec.UNSPECIFIED:
  7.             result = size;
  8.             break;
  9.         case MeasureSpec.AT_MOST:
  10.             result = Math.min(size, specSize);
  11.             break;
  12.         case MeasureSpec.EXACTLY:
  13.             result = specSize;
  14.             break;
  15.         }
  16.         return result;
  17.     }
    上面既然要用到measureSpec值,那自然表示这个函数通常是在onMeasure方法里面调用的。简单说一下,这个方法的主要作用就是根据你提供的大小和模式,返回你想要的大小值,这个里面根据传入模式的不同来做相应的处理。
    再看看MeasureSpec.makeMeasureSpec方法,实际上这个方法很简单:

点击(此处)折叠或打开

  1. public static int makeMeasureSpec(int size, int mode) {
  2.     return size + mode;
  3. }

    这样大家不难理解size跟measureSpec区别了。看看它的使用吧,ListView.measureItem(View child)

点击(此处)折叠或打开

  1. private void measureItem(View child) {
  2.         ViewGroup.LayoutParams p = child.getLayoutParams();
  3.         if (p == null) {
  4.             p = new ViewGroup.LayoutParams(
  5.                     ViewGroup.LayoutParams.MATCH_PARENT,
  6.                     ViewGroup.LayoutParams.WRAP_CONTENT);
  7.         }

  8.         int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
  9.                 mListPadding.left + mListPadding.right, p.width);
  10.         int lpHeight = p.height;
  11.         int childHeightSpec;
  12.         if (lpHeight > 0) {
  13.             childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
  14.         } else {
  15.             childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
  16.         }
  17.         child.measure(childWidthSpec, childHeightSpec);
  18.     }

    measureSpec方法通常在ViewGroup中用到,它可以根据模式(MeasureSpec里面的三个)可以调节子元素的大小。
    注意,使用EXACTLY和AT_MOST通常是一样的效果,如果你要区别他们,那么你就要使用上面的函数View.resolveSize(int size,int measureSpec)返回一个size值,然后使用你的view调用setMeasuredDimension(int,int)函数。

点击(此处)折叠或打开

  1. protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
  2.     mMeasuredWidth = measuredWidth;
  3.     mMeasuredHeight = measuredHeight;

  4.     mPrivateFlags |= MEASURED_DIMENSION_SET;
  5. }
  然后你调用view.getMeasuredWidth,view.getMeasuredHeigth 返回的就是上面函数里的mMeasuredWidth,mMeasuredHeight的值。
getChildMeasureSpec
    计算MeasureSpec然后传递到特定的子视图,此方法用来计算一个合适子视图的尺寸大小(宽度或者高度),目的在于结合我们从子视图的LayoutParams所给出的MeasureSpec信息来获取最合适的结果。
    比如,如果这个View知道自己的大小尺寸(因为它本身的MeasureSpec的model为Exactly,)并且子视图的大小恰好跟父窗口一样大,父窗口必须用给定的大小去layout子视图
参数:spec 父窗口传递给子视图的大小和模式
         padding 父窗口的边距,也就是xml中的android:padding
        childDimension 子视图想要绘制的准确大小,但最终不一定绘制此值
    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
     * a MeasureSpec integer for the child
阅读(837) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~