View onMeasure (int widthMeasureSpec, int heightMeasureSpec)

  1. 点击打开链接:http://blog.csdn.net/lzx_bupt/article/details/5581615
  2. 除非你总是需要一个100×100像素的控件,否则,你必须要重写onMeasure。  
  3.   
  4. onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec。  
  5. 它们指明控件可获得的空间以及关于这个空间描述的元数据。  
  6.   
  7.  比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。  
  8. 接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。  
  9.   
  10.    
  11.   
  12. @Override  
  13.   
  14. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  15.   
  16. int measuredHeight = measureHeight(heightMeasureSpec);  
  17.   
  18. int measuredWidth = measureWidth(widthMeasureSpec);  
  19.   
  20. setMeasuredDimension(measuredHeight, measuredWidth);  
  21.   
  22. }  
  23.   
  24.    
  25.   
  26. private int measureHeight(int measureSpec) {  
  27.   
  28. // Return measured widget height.  
  29.   
  30. }  
  31.   
  32.    
  33.   
  34. private int measureWidth(int measureSpec) {  
  35.   
  36. // Return measured widget width.  
  37.   
  38. }  
  39.   
  40.    
  41.   
  42. 边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:  
  43.   
  44.    
  45.   
  46. int specMode = MeasureSpec.getMode(measureSpec);  
  47.   
  48. int specSize = MeasureSpec.getSize(measureSpec);  
  49.   
  50.    
  51.   
  52. 依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。  
  53.   
  54. 当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。  
  55.   
  56. 在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。  
  57.   
  58.  接下来的框架代码给出了处理View测量的典型实现:  
  59.   
  60.    
  61.   
  62. @Override  
  63.   
  64. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  65.   
  66. int measuredHeight = measureHeight(heightMeasureSpec);  
  67.   
  68. int measuredWidth = measureWidth(widthMeasureSpec);  
  69.   
  70. setMeasuredDimension(measuredHeight, measuredWidth);  
  71.   
  72. }  
  73.   
  74.    
  75.   
  76. private int measureHeight(int measureSpec) {  
  77.   
  78. int specMode = MeasureSpec.getMode(measureSpec);  
  79.   
  80. int specSize = MeasureSpec.getSize(measureSpec);  
  81.   
  82.    
  83.   
  84. // Default size if no limits are specified.  
  85.   
  86. int result = 500;  
  87.   
  88. if (specMode == MeasureSpec.AT_MOST)   
  89.   
  90. {  
  91.   
  92. // Calculate the ideal size of your  
  93.   
  94. // control within this maximum size.  
  95.   
  96. // If your control fills the available  
  97.   
  98. // space return the outer bound.  
  99.   
  100. result = specSize;  
  101.   
  102. }   
  103.   
  104. else if (specMode == MeasureSpec.EXACTLY)   
  105.   
  106. {  
  107.   
  108. // If your control can fit within these bounds return that value.  
  109.   
  110. result = specSize;  
  111.   
  112. }  
  113.   
  114. return result;  
  115.   
  116. }  
  117.   
  118.    
  119.   
  120. private int measureWidth(int measureSpec) {  
  121.   
  122. int specMode = MeasureSpec.getMode(measureSpec);  
  123.   
  124. int specSize = MeasureSpec.getSize(measureSpec);  
  125.   
  126.    
  127.   
  128. // Default size if no limits are specified.  
  129.   
  130. int result = 500;  
  131.   
  132. if (specMode == MeasureSpec.AT_MOST)  
  133.   
  134. {  
  135.   
  136. // Calculate the ideal size of your control  
  137.   
  138. // within this maximum size.  
  139.   
  140. // If your control fills the available space  
  141.   
  142. // return the outer bound.  
  143.   
  144. result = specSize;  
  145.   
  146. }   
  147.   
  148. else if (specMode == MeasureSpec.EXACTLY)   
  149.   
  150. {  
  151.   
  152. // If your control can fit within these bounds return that value.  
  153.   
  154. result = specSize;  
  155.   
  156. }  
  157.   
  158. return result;  
  159.   
  160. }  
  161.   
  162.    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值