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

Android中预估view的大小(宽高)

2015-09-28 14:55 459 查看
很多时候我们是需要预估View的宽高的。因为View的getWidht和getHeight,getMeasuredWidth和getMeasuredHeight是在view放到layout中显示的时候才能获得正确的值的。
但是往往我们需要在它显示之前就知道它的大小是多少,宽高是多少。
这时候,我们就需要预估View的宽高。

其实之所以在View显示出来之后再获取它的宽高时能正确的得到它的大小,是因为在它显示之前经过了两个步骤:
一个是measure()一个是requestLayout()。
我们要预估View的大小,也要借助这个View.measure()方法。

下面是从网上搜集来的一些信息,懒得进一步整理了,现贴在这里做个记录。

View.measure()方法的原型如下:
View.measure(widthMeasureSpec,heightMeasureSpec)
它的两个参数是两个可以经过计算获得的MeasureSpec值,不需要时,可以传两个0。

通过下面的方法可以计算得到MeasureSpec值。
View.MeasureSpec.makeMeasureSpec(maxW,View.MeasureSpec.AT_MOST)
这个方法的两个参数:
第一个参数表示父View中允许这个子View占用的宽/高
第二个参数是模式,它有三种选择
UNSPECIFIED: 不限定
EXACTLY: 固定
AT_MOST:最多

这是从网上摘来的一段通过measure方法来计算ListView高度的代码

public class Utility {

        public static void setListViewHeightBasedOnChildren(ListView listView) {

                ListAdapter listAdapter = listView.getAdapter();

                if (listAdapter == null) {

                        // pre-condition

                        return;

                }

                int totalHeight = 0;

                for (int i = 0; i < listAdapter.getCount(); i++) {

                        View listItem = listAdapter.getView(i, null, listView);

                        listItem.measure(0, 0);

                        totalHeight += listItem.getMeasuredHeight();

                }

                ViewGroup.LayoutParams params = listView.getLayoutParams();

                params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

                listView.setLayoutParams(params);

        }

}

复制代码

下面是从网上摘来的一段关于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;   
  
}

这是从网上摘来的另外一段关于
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。

   

    //----------------------------------------------------------------

    另一篇文章中有这样的描述

http://blog.csdn.net/lzx_bupt/archive/2010/05/12/5581615.aspx)

   
    onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec。它们指明控件可获得的空间以及关于这个空间描述的元数据。比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。

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

    //----------------------------------------------------------------

    我理解,此函数依然用于自定义控件中,就象前一篇文章"转帖并消化:Android中一种使用AttributeSet自定义控件的方法"(简称前一篇)中描述的用于MyView测量自身的大小的.还以前一篇中的MyView类为例,可以在里面再加上一个这样的函数:

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 

    {
   
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        
        if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY)

        {
            throw new IllegalStateException("ApplicationsStackLayout can only be used with "
                    + "measure spec mode=EXACTLY");
        }

        setMeasuredDimension(widthSize, heightSize);
    }

    最终onMeasure通过这样的方法把自己的大小通过setMeasureDimension存储到框架中留调用MyView的布局类用于,至于它怎样用,暂时还没去研究,大概在某些控件在使用的使用需要知道它的精确的大小吧.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: