您的位置:首页 > 移动开发 > Android开发

android笔记之onMeasure方法

2015-06-26 22:21 453 查看
在自定义控件时,我们都常常重写onMeasure,onLayout,onDraw等这些方法,而onMeasure又是重中之重,那么它是干什么的以及什么时候被调用呢?下面我们就一起来分析分析:

1. 调用顺序:

onMeasure,onLayout,onDraw的执行顺序:onMeasure—->onLayout—->onDraw

onMeasure是负责测量控件的大小,说白了就是,告诉父元素,我想要多大。

2. 什么时候调用onMeasure方法?

当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec.

这两个参数指明控件可获得的空间以及关于这个空间描述的元数据.

但是更好的方法是你传递View的高度和宽度到setMeasuredDimension方法里,这样可以直接告诉父控件,需要多大地方放置子控件.

注意,下面我们调用的本地空方法是来计算高度和宽度的.它们会译解widthHeightSpec和heightMeasureSpec值,在里面计算出合适的高度和宽度值。

一般写法如下:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//      Log.i(TAG, "onMeasure");
Log.i(TAG, "widthMeasureSpec:" + widthMeasureSpec+ ",heightMeasureSpec:" + heightMeasureSpec);
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredWidth, measuredHeight);
}
private int measureWidth(int widthMeasureSpec) {
// Return measured widget width.
}
private int measureHeight(int heightMeasureSpec) {
// Return measured widget heigth.
}


测量大小

3.1 边界参数——widthMeasureSpec和heightMeasureSpec:

在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:

int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);


依据specMode的值,(MeasureSpec有3种模式分别是UNSPECIFIED, EXACTLY和AT_MOST)

① 如果是AT_MOST,specSize 代表的是最大可获得的空间;

② 如果是EXACTLY,specSize 代表的是精确的尺寸;

③ 如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。

3.2 那么这些模式和我们平时设置的layout参数fill_parent, wrap_content有什么关系呢?经过代码测试就知道:

3.2.1 当我们设置width或height为fill_parent时,容器在布局时调用子 view的measure方法传入的模式是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的;

3.2.2 而当设置为 wrap_content时,容器传进去的是AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸。当子view的大小设置为精确值时,容器传入的是EXACTLY;

3.2.3 而MeasureSpec的UNSPECIFIED模式目前还没有发现在什么情况下使用。

View的onMeasure方法默认行为是当模式为UNSPECIFIED时,设置尺寸为mMinWidth(通常为0)或者背景drawable的最小尺寸,当模式为EXACTLY或者AT_MOST时,尺寸设置为传入的MeasureSpec的大小。

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

private int measureWidth(int widthMeasureSpec) {
int specMode = MeasureSpec.getMode(widthMeasureSpec);
int specSize = MeasureSpec.getSize(widthMeasureSpec);

int result = 500;
if (specMode == MeasureSpec.AT_MOST){   //wrap_content
// 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){  //fill_parent
// If your control can fit within these bounds return that value.
result = specSize;
}
Log.i(TAG, "measureWidth ----result:" + result);
// Return measured widget width.
return result;
}
/**
* 计算控件适合的高度
* @param heightMeasureSpec
* @return
*/
private int measureHeight(int heightMeasureSpec) {
int specMode = MeasureSpec.getMode(heightMeasureSpec);
int specSize = MeasureSpec.getSize(heightMeasureSpec);

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;
}
Log.i(TAG, "measureHeight --result:" + result);
// Return measured widget heigth.
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 测量 控件