在上篇文章(http://www.cnblogs.com/jerehedu/p/4607599.html#gui)中,根据源码探索了View的绘制过程,过程有三个主要步骤,分别为测量、布局、绘制。系统对绘制已经做了很好的封装,我们主要对测量和布局过程进行分析,看一看android是如何对view进行测量和布局的。

  根据上篇文章的分析,我们知道在ViewRootImpl的performMeasure方法中,实际上调用了mView.measure(childWidthMeasureSpec, childHeightMeasureSpec);方法。根据源码我们找到了该方法的原型,此方法在View类中,并且是final方法,不可被子类重写,方法的具体源码如下:

public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
boolean optical = isLayoutModeOptical(this);
if (optical != isLayoutModeOptical(mParent)) {
……
}
// Suppress sign extension for the low bytes
long key = (long) widthMeasureSpec << 32 | (long) heightMeasureSpec & 0xffffffffL;
if (mMeasureCache == null) mMeasureCache = new LongSparseLongArray(2); if ((mPrivateFlags & PFLAG_FORCE_LAYOUT) == PFLAG_FORCE_LAYOUT ||
widthMeasureSpec != mOldWidthMeasureSpec ||
heightMeasureSpec != mOldHeightMeasureSpec) { ……
if (cacheIndex < 0 || sIgnoreMeasureCache) {
// measure ourselves, this should set the measured dimension flag back
onMeasure(widthMeasureSpec, heightMeasureSpec);
mPrivateFlags3 &= ~PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
} else {
long value = mMeasureCache.valueAt(cacheIndex);
// Casting a long to int drops the high 32 bits, no mask needed
setMeasuredDimensionRaw((int) (value >> 32), (int) value);
mPrivateFlags3 |= PFLAG3_MEASURE_NEEDED_BEFORE_LAYOUT;
}
…… mPrivateFlags |= PFLAG_LAYOUT_REQUIRED;
} mOldWidthMeasureSpec = widthMeasureSpec;
mOldHeightMeasureSpec = heightMeasureSpec; mMeasureCache.put(key, ((long) mMeasuredWidth) << 32 |
(long) mMeasuredHeight & 0xffffffffL); // suppress sign extension
}

  根据方法内容和说明,可以知道本方法就是用来测量View的大小的,而需要的两个参数是由父View构建的,用于说明父View对子View的测量的规格要求,实际上在这个方法中真正完成测量大小的是方法onMeasure,此方法我们稍后分析。在此之前我们先要明白measure方法中的两个参数的含义,刚才有提到参数是父View对子View的测量规格要求,那么Android是如何描述的呢,这里用到了一个类MeasureSpec,此类为View中的一个内部类,关键源码如下:

public static class MeasureSpec {
private static final int MODE_SHIFT = 30;
private static final int MODE_MASK = 0x3 << MODE_SHIFT;
public static final int UNSPECIFIED = 0 << MODE_SHIFT;
public static final int EXACTLY = 1 << MODE_SHIFT;
public static final int AT_MOST = 2 << MODE_SHIFT;
public static int makeMeasureSpec(int size, int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
}
public static int getMode(int measureSpec) {
return (measureSpec & MODE_MASK);
}
public static int getSize(int measureSpec) {
return (measureSpec & ~MODE_MASK);
} ……
}

  根据SDK,此类封装了父View对子View的布局要求,每个实例都代表了对子View的高度或者宽度的要求,测量要求包含两个部分,分别为尺寸和模式。模式主要由三种,具体如下:

1、  UNSPECIFIED:代表父View对子View没有约束,子View可以为任意大小。

2、  EXACTLY:父View确定子View的大小,子View被限定在给定的边界中,忽咯本身的大小。

3、  AT_MOST:子View最大可以达到指定大小的值。

  该类中提供了用来计算和生成测量要求的方法,具体如下:

1、  public static int makeMeasureSpec(int size, int mode),此方法最终生成一个32位二进制数用来表明测量规格要求,其中32和31位用来表明模式,后30位代表了大小。

2、  public static int getMode(int measureSpec),此方法可以根据测量说明,计算模式。

3、  public static int getSize(int measureSpec),此方法根据测量说明,计算大小。

  明白了MeasureSpec,我们在回过头来,看一看onMeasure方法,该方法的源码如下:

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}

  此方法的默认实现非常简单,调用了setMeasuredDimersion方法将测量好的尺寸保存到mMeasuredWidth和mMeasuredHeight。而在setMeasuredDimersion方法中调用了getDefaultSize用来计算,该方法具体如下:

public static int getDefaultSize(int size, int measureSpec) {
int result = size;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec); switch (specMode) {
case MeasureSpec.UNSPECIFIED:
result = size;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.EXACTLY:
result = specSize;
break;
}
return result;
}

  很明显,此方法根据提供的默认大小和测量要求计算View的实际大小。到此为止,View完了测量过程。不过大多数情况下,当我们自定义ViewGroup的时候,我们需要重写onMeasure方法,在此方法中,可以遍历所有的子View并要求他们对自己的大小进行测量,同时不要忘记调用setMeasuredDimension进行保存测量结果,在ViewGroup是通过如下三个方法实现的,关键代码如下:

  方法mesureChildren,遍历所有的非隐藏的子View,并调用measureChild方法设置子View的测量要求。

protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
final int size = mChildrenCount;
final View[] children = mChildren;
for (int i = 0; i < size; ++i) {
final View child = children[i];
if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
}
}
}

  方法measureChild,获取子View的测量规格,并调用measure进行测量实际大小。

protected void measureChild(View child, int parentWidthMeasureSpec,
int parentHeightMeasureSpec) {
final LayoutParams lp = child.getLayoutParams(); final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom, lp.height); child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}

  方法getChildMeasureSpec用于获取View的测量规格要求。

    public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
int specMode = MeasureSpec.getMode(spec);
int specSize = MeasureSpec.getSize(spec); int size = Math.max(0, specSize - padding); int resultSize = 0;
int resultMode = 0; switch (specMode) {
// Parent has imposed an exact size on us
case MeasureSpec.EXACTLY:
if (childDimension >= 0) {
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size. So be it.
resultSize = size;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break; // Parent has imposed a maximum size on us
case MeasureSpec.AT_MOST:
if (childDimension >= 0) {
// Child wants a specific size... so be it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size, but our size is not fixed.
// Constrain child to not be bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size. It can't be
// bigger than us.
resultSize = size;
resultMode = MeasureSpec.AT_MOST;
}
break; // Parent asked to see how big we want to be
case MeasureSpec.UNSPECIFIED:
if (childDimension >= 0) {
// Child wants a specific size... let him have it
resultSize = childDimension;
resultMode = MeasureSpec.EXACTLY;
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should
// be
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how
// big it should be
resultSize = 0;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
}
return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
}

  疑问咨询或技术交流,请加入官方QQ群: (452379712)

作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

Android GUI之View测量的更多相关文章

  1. Android GUI之View绘制流程

    在上篇文章中,我们通过跟踪源码,我们了解了Activity.Window.DecorView以及View之间的关系(查看文章:http://www.cnblogs.com/jerehedu/p/460 ...

  2. Android GUI之View事件处理

     Android中的事件分为按键事件和触屏事件,本篇文章将分析View是如何处理Touch事件的.在View中定义了许多触屏事件,比如OnClick.OnLongClick等等,这些事件都是由一次To ...

  3. Android GUI之View布局

    在清楚了View绘制机制中的第一步测量之后,我们继续来了解分析View绘制的第二个过程,那就是布局定位.继续跟踪分析源码,根据之前的流程分析我们知道View的绘制是从RootViewImpl的perf ...

  4. Android GUI之View事件处理(二)

    在上篇文章中,我们分析了View的事件处理过程,当然这里的View是指基本的View.当View接收到Touch事件时,首先会调用dispacheTouchEvent方法,在这个方法中会调用OnTou ...

  5. 图解Android - Android GUI 系统 (2) - 窗口管理 (View, Canvas, Window Manager)

    Android 的窗口管理系统 (View, Canvas, WindowManager) 在图解Android - Zygote 和 System Server 启动分析一 文里,我们已经知道And ...

  6. Android View 测量流程(Measure)完全解析

    前言 上一篇文章,笔者主要讲述了DecorView以及ViewRootImpl相关的作用,这里回顾一下上一章所说的内容:DecorView是视图的顶级View,我们添加的布局文件是它的一个子布局,而V ...

  7. android Gui系统之SurfaceFlinger(1)---SurfaceFlinger概论

    GUI 是任何系统都很重要的一块. android GUI大体分为4大块. 1)SurfaceFlinger 2)WMS 3)View机制 4)InputMethod 这块内容非常之多,但是理解后,可 ...

  8. Android GUI系统

    图解Android - Android GUI 系统 (1) - 概论 图解Android - Android GUI 系统 (2) - 窗口管理系统 图解Android - Android GUI ...

  9. 图解Android - Android GUI 系统 (1) - 概论

    Android的GUI系统是Android最重要也最复杂的系统之一.它包括以下部分: 窗口和图形系统 - Window and View Manager System. 显示合成系统 - Surfac ...

随机推荐

  1. python3 + selenium 之窗口切换

    窗口切换 此代码来源学习后对淘宝操作实践记录: 以下代码在Chrome61和IE11上正常运行,Firefox5.7上运行存在一些问题须改进,应该是火狐不兼容差link_text部分和循环经常报错,在 ...

  2. 更好用的cmd窗口

    cmder是windows下的命令行工具,用来替代windows自带的cmd. 下载地址 下载后自建文件夹并解压,将Cmder.exe所在文件夹路径加入path, windows + r 键入cmde ...

  3. Spring的核心之IoC容器创建对象

    Spring的Ioc容器,是Spring的核心内容: 作用:对象的创建和处理对象的依赖关系. Spring容器创建对象有以下几种方式: 1:调用无参数的构造器 <!-- 默认无参的构造器 --& ...

  4. [转] HTML5 FormData 方法介绍以及实现文件上传

    XMLHttpRequest 是一个浏览器接口,通过它,我们可以使得 Javascript 进行 HTTP (S) 通信.XMLHttpRequest 在现在浏览器中是一种常用的前后台交互数据的方式. ...

  5. 【BZOJ3626】[LNOI2014]LCA

    题解: 数据结构套路真多.. 思考了一下线段树分治和启发式合并发现复杂度并不怎么对.. 看了题解发现都是套路啊 考虑一下lca(x,y)的dep有什么性质 我们把1-x的路径+1,查询1-y的路径和 ...

  6. BZOJ3224 洛谷3369 Tyvj 1728 普通平衡树 splay

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3224 题意概括 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. ...

  7. Maven启动代理访问

    1.说明 如果你的公司正在建立一个防火墙,并使用HTTP代理服务器来阻止用户直接连接到互联网.如果您使用代理,Maven将无法下载任何依赖. 为了使它工作,你必须声明在 Maven 的配置文件中设置代 ...

  8. Hdu-1358Period(KMP算法之next数组的应用)

    题解:对于串pattern来说,如果0~i-1这个位置中循环,那么i%(i-next[i])==0 ,循环次数为 i/(i-next[i]),循环长度为 i-next[i] 例如对于串ababab来说 ...

  9. macos下mongoDB 3.4.5 添加用户、设置权限

    macos下mongoDB 3.4.5 添加用户.设置权限   在项目中需要根据项目运行环境访问,以不同的身份访问各自的db,所以研究了一下MongoDB的 需求: 给MongoDB添加两个用户分别用 ...

  10. 011.Docker仓库管理

    一 Docker仓库介绍 docker 仓库,即 registry,实现了镜像的管理.分发,同时还包括用户的认证.docker registry 仓库是一个无状态的.高可靠的服务器应用程序,用来存储d ...