当前位置: 移动技术网 > IT编程>移动开发>Android > Android View.onMeasure方法详解及实例

Android View.onMeasure方法详解及实例

2019年07月24日  | 移动技术网IT编程  | 我要评论

日本旅游价格,孢子物语txt下载,1920x1080

android view.onmeasure方法详解及实例

view在屏幕上显示出来要先经过measure(计算)和layout(布局).

1、什么时候调用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的值,(measurespec有3种模式分别是unspecified, exactly和at_most)

  • 如果是at_most,specsize 代表的是最大可获得的空间;
  • 如果是exactly,specsize 代表的是精确的尺寸;
  • 如果是unspecified,对于控件尺寸来说,没有任何参考意义。

2、那么这些模式和我们平时设置的layout参数fill_parent, wrap_content有什么关系呢?

经过代码测试就知道,当我们设置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测量的典型实现:

java代码:

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

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网