• [转载]Android View.onMeasure方法的理解


    2013-12-18 10:56:28

    转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html

    View在屏幕上显示出来要先经过measure(计算)和layout(布局).
    1. 什么时候调用onMeasure()方法? 
    当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”,然后传入两个参数--widthMeasureSpec和heightMeasureSpec.
    这两个参数指明控件可获得的空间以及关于这个空间描述的元数据.
    更好的方法是你传递View的高度和宽度到setMeasuredDimension方法里,这样可以直接告诉父控件,需要多大地方放置子控件.


    2. 接下来的代码片段给出了如何重写onMeasure.注意,调用的本地空方法是来计算高度和宽度的.它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值.

     1 @Override
     2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     3 
     4     int measuredHeight = measureHeight(heightMeasureSpec);
     5     int measuredWidth = measureWidth(widthMeasureSpec);
     6     setMeasuredDimension(measuredHeight, measuredWidth);
     7 }
     8 
     9 private int measureHeight(int measureSpec) {
    10     // Return measured widget height.
    11 }
    12 
    13 private int measureWidth(int measureSpec) {
    14     // Return measured widget width.
    15 }

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

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

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

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

    3. 那么这些模式和我们平时设置的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,使用裁剪还是滚动等技术。

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

     1 @Override
     2 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
     3 
     4     int measuredHeight = measureHeight(heightMeasureSpec);
     5     int measuredWidth = measureWidth(widthMeasureSpec);
     6     setMeasuredDimension(measuredHeight, measuredWidth);
     7 }
     8 
     9 private int measureHeight(int measureSpec) {
    10 
    11     int specMode = MeasureSpec.getMode(measureSpec);
    12     int specSize = MeasureSpec.getSize(measureSpec);
    13 
    14     // Default size if no limits are specified.
    15 
    16     int result = 500;
    17     if (specMode == MeasureSpec.AT_MOST){
    18         // Calculate the ideal size of your
    19         // control within this maximum size.
    20         // If your control fills the available
    21         // space return the outer bound.
    22 
    23         result = specSize;
    24     } else if (specMode == MeasureSpec.EXACTLY){
    25         // If your control can fit within these bounds return that value.
    26         result = specSize;
    27     }
    28     return result;
    29 }
    30 
    31 private int measureWidth(int measureSpec) {
    32     int specMode = MeasureSpec.getMode(measureSpec);
    33     int specSize = MeasureSpec.getSize(measureSpec);
    34 
    35     // Default size if no limits are specified.
    36     int result = 500;
    37     if (specMode == MeasureSpec.AT_MOST){
    38         // Calculate the ideal size of your control
    39         // within this maximum size.
    40         // If your control fills the available space
    41         // return the outer bound.
    42         result = specSize;
    43     } else if (specMode == MeasureSpec.EXACTLY){
    44         // If your control can fit within these bounds return that value.
    45         result = specSize;
    46     }
    47     return result;
    48 }
  • 相关阅读:
    matlab中绘制折线图、绘制条形图(柱形图)的简单方法
    2.序列类型方法
    年度KPI绩效面谈记录
    软件开发工作杂谈——更杂版
    软件研发工作杂谈
    FFI Navigtor在Windows环境下的"No Such File or Directory"Bug解决过程
    Linux环境部署帆软报表(FineReport)说明
    IIS负载均衡Application Request Route 安装配置说明(ARR)
    python使用了resetKeyboard输入法打不开怎么办
    python 执行时报错AttributeError: 'dict' object has no attribute 'has_key'
  • 原文地址:https://www.cnblogs.com/wlrhnh/p/3479928.html
Copyright © 2020-2023  润新知