Android中onMeasure方法详解

        在开发中,当Android原生控件不能满足我们的需求的时候,就需要自定义View。View在屏幕上绘制出来先要经过measure(计算)和layout(布局)。

  什么时候调用onMeasure方法?

  当子View的父控件要放置该View的时候,父控件会传递两个参数给View——widthMeasureSpec和heightMeasureSpec。这两个参数是View可以获取的宽高尺寸和模式 混合的int数据。可以通过int mode = MeasureSpec.getMode(widthMeasureSpec)得到模式,用int size = MeasureSpec.getSize(widthMeasureSpec)得到尺寸。

  mode共有三种情况,取 分别为:

  MeasureSpec.UNSPECIFIED;

       MeasureSpec.EXACTLY;

       MeasureSpec.AT_MOST。


  MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数 时如

  andorid:layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。

  MeasureSpec.AT_MOST是最大尺寸,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。

  MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。

  可以调用setMeasuredDimenson方法,将View的高度和宽度传入,设置子View实际的大小,告诉父控件需要多大的空间放置子View。

         经过代码测试就知道,当我们设置width或height为fill_parent时,容器在布局时调用子 view的measure方法传入的模式是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。
而当设置为 wrap_content时,容器传进去的是AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸。当子view的大小设置为精确值时,容器传入的是EXACTLY, 而MeasureSpec的UNSPECIFIED模式目前还没有发现在什么情况下使用。 


 
         View的onMeasure方法默认行为是当模式为UNSPECIFIED时,设置尺寸为mMinWidth(通常为0)或者背景drawable的最小尺寸,当模式为EXACTLY或者AT_MOST时,尺寸设置为传入的MeasureSpec的大小。 
 
          有个观念需要纠正的是,fill_parent应该是子view会占据剩下容器的空间,而不会覆盖前面已布局好的其他view空间,当然后面布局子 view就没有空间给分配了,所以fill_parent属性对布局顺序很重要。以前所想的是把所有容器的空间都占满了,难怪google在2.2版本里 把fill_parent的名字改为match_parent.

  在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
  接下来的框架代码给出了处理View测量的典型实现:


  以下是框架中View的onMeasure的典型实现:

 

<span style="font-size:12px;"> @Override

  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

  int measuredHeight = measureHeight(heightMeasureSpec);

  int measuredWidth = measureWidth(widthMeasureSpec);

  setMeasuredDimension(measuredHeight, measuredWidth);

  private int measureHeight(int measureSpec) {

  int specMode = MeasureSpec.getMode(measureSpec);

  int specSize = MeasureSpec.getSize(measureSpec);

  // Default size if no limits are specified.

  int result = 500;

  if (specMode == MeasureSpec.AT_MOST) {

  // Calculate the ideal size of your

  // control within this maximum size.

  // If your control fills the available

  // space return the outer bound.

  result = specSize;

  } else if (specMode == MeasureSpec.EXACTLY) {

  // If your control can fit within these bounds return that

  // value.

  result = specSize;

  return result;

  private int measureWidth(int measureSpec) {

  int specMode = MeasureSpec.getMode(measureSpec);

  int specSize = MeasureSpec.getSize(measureSpec);

  // Default size if no limits are specified.

  int result = 500;

  if (specMode == MeasureSpec.AT_MOST) {

  // Calculate the ideal size of your control

  // within this maximum size.

  // If your control fills the available space 

  // return the outer bound.

  result = specSize;

  else if (specMode == MeasureSpec.EXACTLY) {

  // If your control can fit within these bounds return that

  // value.

  result = specSize;

  return result;

  }</span>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值