View-MeasuerSepc

View-MeasuerSepc
MeasuerSepc 是View 的一个内部类,在分析View 的测量(measuer)会出现这个类,这里先记录一下对MeasuerSepc的理解.

MeasuerSepc可以理解为 一个32位的int 值,其中前2位表示mode(模式),后30位表示size(具体大小),通过将mode和size体现在一个int值里面来避免过多的对象内存分配. MeasuerSepc同时提供了来makeMeasureSpec(int size,int mode) 来打包出来一个32位的int值.当然也有根据一个MeasuerSepc值来解包获取mode和size的方法.方便是getMode(int measureSpec)和getSize(int measureSpec) .
注意: MeasuerSepc代表的int值并不是MeasuerSepc自己本身.
具体源码看下面,红色部分是注释.(Android L的源码)

   
   
  1. public static class MeasureSpec {
  2. private static final int MODE_SHIFT = 30;
  3. /**
  4. * 11 0000...(30个0)
  5. */
  6. private static final int MODE_MASK = 0x3 << MODE_SHIFT;
  7. /**
  8. * Measure specification mode: The parent has not imposed any constraint
  9. * on the child. It can be whatever size it wants.父容器不对View做限制,想要多大就多大
  10. */
  11. public static final int UNSPECIFIED = 0 << MODE_SHIFT;//00 000...(32个0)
  12. /**
  13. * Measure specification mode: The parent has determined an exact size
  14. * for the child. The child is going to be given those bounds regardless
  15. * of how big it wants to be.父容器已经精确的检测出view的大小,View最终的大小就是由MeasureSpec的size决定.对应LayoutParams中     * 的match_parent和具体数值2种模式
  16. */
  17. public static final int EXACTLY = 1 << MODE_SHIFT;//01 0000...(30个0)
  18. /**
  19. * Measure specification mode: The child can be as large as it wants up
  20. * to the specified size.父容器给了一个最大值,View的大小不能超过这个值.它对应于LayoutParams中的warp_content.
  21. */
  22. public static final int AT_MOST = 2 << MODE_SHIFT;//10 0000...(30个0)
  23. public static int makeMeasureSpec(int size, int mode) {//创建一个MeasureSpec的int值:32位(前2位表示mode,后面30位表示size)
  24. if (sUseBrokenMakeMeasureSpec) {//JB之前的版本
  25. return size + mode;
  26. } else {
  27. return (size & ~MODE_MASK) | (mode & MODE_MASK);
  28. }
  29. }
  30. /**
  31. * Extracts the mode from the supplied measure specification.
  32. *
  33. * @param measureSpec the measure specification to extract the mode from
  34. * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
  35. * {@link android.view.View.MeasureSpec#AT_MOST} or
  36. * {@link android.view.View.MeasureSpec#EXACTLY}
  37. */
  38. public static int getMode(int measureSpec) {
  39. return (measureSpec & MODE_MASK);
  40. }
  41. /**
  42. * Extracts the size from the supplied measure specification.
  43. *
  44. * @param measureSpec the measure specification to extract the size from
  45. * @return the size in pixels defined in the supplied measure specification
  46. */
  47. public static int getSize(int measureSpec) {
  48. return (measureSpec & ~MODE_MASK);
  49. }
  50. static int adjust(int measureSpec, int delta) {
  51. final int mode = getMode(measureSpec);
  52. if (mode == UNSPECIFIED) {
  53. // No need to adjust size for UNSPECIFIED mode.
  54. return makeMeasureSpec(0, UNSPECIFIED);
  55. }
  56. int size = getSize(measureSpec) + delta;
  57. if (size < 0) {
  58. Log.e(VIEW_LOG_TAG, "MeasureSpec.adjust: new size would be negative! (" + size +
  59. ") spec: " + toString(measureSpec) + " delta: " + delta);
  60. size = 0;
  61. }
  62. return makeMeasureSpec(size, mode);
  63. }
  64. //.......
  65. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值