在开发中,当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”,或者为MATCH_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。
MeasureSpec.AT_MOST:是最大尺寸,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。如果自定义View要支持wrap_content必须重写onMeasure,否则大小可能为0
MeasureSpec.UNSPECIFIED: 是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。
可以调用setMeasuredDimenson方法,将View的高度和宽度传入,设置子View实际的大小,告诉父控件需要多大的空间放置子View。
而测量我们需要MeasureSpec来帮助,它字面意思就是测量规则,它包括测量模式以及大小,它是一个32位的int值,它的高2位是测量的模式,低30位是测量的大小.
以下是框架中View的onMeasure的典型实现:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measuredHeight = measureHeight(heightMeasureSpec);
int measuredWidth = measureWidth(widthMeasureSpec);
setMeasuredDimension(measuredHeight, measuredWidth);
}
private int measureWidth(int measureSpec) {
// 默认为0
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
}else {
//设置实际需要的大小
result = 200;
if (specMode == MeasureSpec.AT_MOST){
//最大模式,不要超过父容器
result = Math.min(specSize,result);
}
}
return result;
}
private int measureHeight(int measureSpec) {
// 默认为0
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
}else {
//设置实际需要的大小
result = 200;
if (specMode == MeasureSpec.AT_MOST){
//最大模式,不要超过父容器
result = Math.min(specSize,result);
}
}
return result;
}
以下是框架中ViewGroup的onMeasure的典型实现:
如果是自定义ViewGroup,那就各有不同了,每个ViewGroup都不一样,不过大致流程也差不多,就是测量子View再决定自己的大小.
简单的例子如下,把所有子View的高度之和当做自己的高度:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int childCount = getChildCount();
int height = 0;
for(int i = 0; i < childCount; i++){
View child = getChildAt(i);
child.measure(widthMeasureSpec, heightMeasureSpec);
height += child.getMeasuredHeight();
}
setMeasuredDimension(MeasureSpec.getSize(widthMeasuresSpec), height);
}
以上就是自定义View和自定义ViewGroup的OnMeasure()的模板代码,以后直接用就可以了