view onMeasure方法说明

 在一段无法下载的中文API中对此函数有这样的描述:
    //----------------------------------------------------------------
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec);
    View调用此方法来确定本身和所包含内容的大小。此方法被measure(int,int)唤起,而且必须被子类重写以得到所包含内容的确切大小。
    注意:当重写此方法时,必须调用setMeasureDimension(int,int)来保存View的大小。如果没有做到,将会引发一个measure(int,int)抛出的IllegalStateException(非法状态错误)。超类onMeasure(int,int)可以被调用。
    编写基类的确认大小的方法,缺省情况下是根据其背景大小来确认,除非MeasureSepc允许有更大的高度或宽度。子类必须重写onMeasure(int,int)以得到对其内容大小的更准确的测量。
    若此方法被重写,它的子类需要确保其高度和宽度至少达到View所规定的最小值
   (可通过getSuggestedMinimumHeight()和getSuggestedMinimumWidth()得到)。
参数
        widthMeaureSpec           受上一层大小影响下的对水平空间的要求。可参看View.MeasureSpec。
        heightMeasureSpec         受上一层大小影响下的对垂直空间的要求。可参看View.MeasureSpec。
   




除非你总是需要一个100×100像素的控件,否则,你必须要重写onMeasure。



onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec。
它们指明控件可获得的空间以及关于这个空间描述的元数据。


 比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。
接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。


 


@Override


protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


int measuredHeight = measureHeight(heightMeasureSpec);


int measuredWidth = measureWidth(widthMeasureSpec);


setMeasuredDimension(measuredHeight, measuredWidth);


}


 


private int measureHeight(int measureSpec) {


// Return measured widget height.


}


 


private int measureWidth(int measureSpec) {


// Return measured widget width.


}


 


边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:


 


int specMode = MeasureSpec.getMode(measureSpec);


int specSize = MeasureSpec.getSize(measureSpec);


 


依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。


当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。


在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。


 接下来的框架代码给出了处理View测量的典型实现:


 


@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;


}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值