MeasureSpec 解析

MeasureSpec

1、A MeasureSpec is comprised of a size and a mode.

打印成二进制:

MODE_MASK=11000000000000000000000000000000, //0011左移动30位得到 

~MODE_MASK =  00111111111111111111111111111111; 

size & ~MODE_MASK  :  MeasureSpec 是个32位的int型,后三十位是是分配给size的。


 

2、mode有三种:

//0

// 01000000000000000000000000000000

 // 10000000000000000000000000000000

 

mode & MODE_MASK  :  & 相同取1,相异取0;所以  01&11 =01 ;10 &11=10,后三十位全部为0

                                  MeasureSpec 是个32位的int型,前2位是是分配给mode 的。

 

MeasureSpec objects are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED:    The parent has not imposed any constraint on the child. It can be whatever size it wants. 无限制;,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。
  • EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size. 限制尺寸;当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。
 
  • AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size. 限制最大尺寸;,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。

3、取或:要任一表达式的一位为 1,则结果中的该位为 1。否则,结果中的该位为 0。 如图

(size & ~MODE_MASK) | (mode & MODE_MASK) 

MeasureSpec 是个32位的int型,前2位是是分配给mode 的;后三十位是是分配给size的。

 

 

4 getMode()

 

如果现在一个 MeasureSpec  =01000000000000000000111100000000;

01000000000000000000111100000000 & MODE_MASK (11000000000000000000000000000000)=01000000000000000000000000000000  

//结果值是EXACTLY mode

5 、getSize()

如果现在一个 MeasureSpec  =01000000000000000000111100000000

01000000000000000000111100000000 & ~MODE_MASK(00111111111111111111111111111111)=000000000000000000111100000000  //得出size值111100000000=3840

 

出处:http://www.cnblogs.com/sueZheng/p/4046869.html

posted @ 2014-10-23 21:20  sue_zheng  Views(1338)  Comments(0Edit  收藏  举报