理解 MeasureSpec
在开始本篇文章之前,我们先看一段代码:
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expendSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expendSpec);}
是不是很熟悉,没错,它就是我们在 ScrollView 嵌套 ListView 的时候,重写 ListView 来处理 ListView 数据显示不全的问题,那么为什么要这么写呢,而 MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST) 又是什么意思呢,别着急,重头戏马上到来。
1. MeasureSpec 是干什么的
2. MeasureSpec 的原理
MeasureSpec 代表一个32为的 int 值,高两位是 SpecMode,低30位是 SpecSize,SpecMode 是指测量模式,而 SpecMode 是指在某种测量模式下的规格大小,下面先看下 MeasureSpec 内部的一些常量的定义,通过下面的代码,应该不难理解 MeasureSpec 的工作原理:
private static final int MODE_SHIFT = 30;private static final int MODE_MASK = 0x3 << MODE_SHIFT;/** @hide */@IntDef({UNSPECIFIED, EXACTLY, AT_MOST})@Retention(RetentionPolicy.SOURCE)public @interface MeasureSpecMode {}/*** Measure specification mode: The parent has not imposed any constraint* on the child. It can be whatever size it wants.*/public static final int UNSPECIFIED = 0 << MODE_SHIFT;/*** Measure specification mode: The parent has determined an exact size* for the child. The child is going to be given those bounds regardless* of how big it wants to be.*/public static final int EXACTLY = 1 << MODE_SHIFT;/*** Measure specification mode: The child can be as large as it wants up* to the specified size.*/public static final int AT_MOST = 2 << MODE_SHIFT;/*** Creates a measure specification based on the supplied size and mode.** The mode must always be one of the following:* <ul>* <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>* <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>* <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>* </ul>** <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's* implementation was such that the order of arguments did not matter* and overflow in either value could impact the resulting MeasureSpec.* {@link android.widget.RelativeLayout} was affected by this bug.* Apps targeting API levels greater than 17 will get the fixed, more strict* behavior.</p>** @param size the size of the measure specification* @param mode the mode of the measure specification* @return the measure specification based on size and mode*/public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,@MeasureSpecMode int mode) {if (sUseBrokenMakeMeasureSpec) {return size + mode;} else {return (size & ~MODE_MASK) | (mode & MODE_MASK);}}/*** Like {@link #makeMeasureSpec(int, int)}, but any spec with a mode of UNSPECIFIED* will automatically get a size of 0. Older apps expect this.** @hide internal use only for compatibility with system widgets and older apps*/public static int makeSafeMeasureSpec(int size, int mode) {if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {return 0;}return makeMeasureSpec(size, mode);}/*** Extracts the mode from the supplied measure specification.** @param measureSpec the measure specification to extract the mode from* @return {@link android.view.View.MeasureSpec#UNSPECIFIED},* {@link android.view.View.MeasureSpec#AT_MOST} or* {@link android.view.View.MeasureSpec#EXACTLY}*/@MeasureSpecModepublic static int getMode(int measureSpec) {//noinspection ResourceTypereturn (measureSpec & MODE_MASK);}/*** Extracts the size from the supplied measure specification.** @param measureSpec the measure specification to extract the size from* @return the size in pixels defined in the supplied measure specification*/public static int getSize(int measureSpec) {return (measureSpec & ~MODE_MASK);}
MeasureSpec 通过将 SpecMode 和 SpecSize 打包成一个 int 值来避免过多的对象内存分配,为了方便操作,其提供了打包和解包的方法。SpecMode 和 SpecSize 也是一个 int 值,一组 SpecMode 和 SpecSize 可以打包成一个 MeasureSpec,而一个 MeasureSpec 可以通过解包的方法来得出原始的 SpecMode 和 SpecSize,需要注意的是这里提到的 MeasureSpec 是值 MeasureSpec 所代表的 int 值,而并非 MeasureSpec 本身。
3. MeasureSpec 的三种模式
(1)UNSPECIFIEND
父容易不对 View 有任何影响,要多大给多大,这种情况一般用于系统内部,表示一种测量模式(我也不是太懂,有懂的大佬指点下)
(2)EXACTLY
父容器已经检测出 View 所需要的精确大小,这个时候 View 的最终大小就是 MeasureSpec 所指定的值,它对应于 LayoutParams 的 match_parent 和具体的数值两种模式。(如果是重写控件,慎用该模式,不然你会发现一件很神奇的事情,想知道的可以自己动手试试)
(3)AT_MOST
父容器指定一个大小即 SpecSize,View 的大小不能大于这个值,具体是什么值要看不同 View 的具体实现,它对应于 LayoutParams 的warp_content。
4.MeasureSpec 和 LayoutParams 对应关系
childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
接着再看一下 getRootMeasureSpec 方法实现:
/*** Figures out the measure spec for the root view in a window based on it's* layout params.** @param windowSize* The available width or height of the window** @param rootDimension* The layout params for one dimension (width or height) of the* window.** @return The measure spec to use to measure the root view.*/private static int getRootMeasureSpec(int windowSize, int rootDimension) {int measureSpec;switch (rootDimension) {case ViewGroup.LayoutParams.MATCH_PARENT:// Window can't resize. Force root view to be windowSize.measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);break;case ViewGroup.LayoutParams.WRAP_CONTENT:// Window can resize. Set max size for root view.measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);break;default:// Window wants to be an exact size. Force root view to be that size.measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);break;}return measureSpec;}
根据上述代码, DecorView 的 MeasureSpec 的产生过程就很明了了,具体来说遵守如下规则,根据它的 LayoutParams 中的宽高的参数来划分:
// Note: padding has already been removed from the supplied specsprivate void measureChildWithMargins2(View child, int parentWidthSpec, int parentHeightSpec,int childWidth, int childHeight) {int childWidthSpec = getChildMeasureSpec(parentWidthSpec,getTotalMargin(child, true), childWidth);int childHeightSpec = getChildMeasureSpec(parentHeightSpec,getTotalMargin(child, false), childHeight);child.measure(childWidthSpec, childHeightSpec);}
上述方法会对子元素进行 measure,在调用子元素的 measure 方法之前会先调通过 getChildMeasureSpec 的创建与父容器的 MeasureSpec 和子元素本身的 LayoutParams 有关,此外还和 View 的 margins 及 padding 有关,具体情况可以看一下 ViewGroup 的 getChildMeasureSpec 方法,如下所示:
/*** Does the hard part of measureChildren: figuring out the MeasureSpec to* pass to a particular child. This method figures out the right MeasureSpec* for one dimension (height or width) of one child view.** The goal is to combine information from our MeasureSpec with the* LayoutParams of the child to get the best possible results. For example,* if the this view knows its size (because its MeasureSpec has a mode of* EXACTLY), and the child has indicated in its LayoutParams that it wants* to be the same size as the parent, the parent should ask the child to* layout given an exact size.** @param spec The requirements for this view* @param padding The padding of this view for the current dimension and* margins, if applicable* @param childDimension How big the child wants to be in the current* dimension* @return a MeasureSpec integer for the child*/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 uscase 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 uscase MeasureSpec.AT_MOST:if (childDimension >= 0) {// Child wants a specific size... so be itresultSize = 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 becase MeasureSpec.UNSPECIFIED:if (childDimension >= 0) {// Child wants a specific size... let him have itresultSize = childDimension;resultMode = MeasureSpec.EXACTLY;} else if (childDimension == LayoutParams.MATCH_PARENT) {// Child wants to be our size... find out how big it should// beresultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;resultMode = MeasureSpec.UNSPECIFIED;} else if (childDimension == LayoutParams.WRAP_CONTENT) {// Child wants to determine its own size.... find out how// big it should beresultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;resultMode = MeasureSpec.UNSPECIFIED;}break;}//noinspection ResourceTypereturn MeasureSpec.makeMeasureSpec(resultSize, resultMode);}
上述方法不难理解,它的主要作用是根据父容器的 MeasureSpec 同时结合 View 本身的 LayoutParams 来确定子元素的 MeasureSpec ,参数中的 padding 是指父容器中已经占用的空间大小,因此子元素可用的大小为父容器的尺寸减去 padding ,具体代码如下:
int specSize = MeasureSpec.getSize(spec);int size = Math.max(0, specSize - padding);
getChildMeasureSpec 清楚展示了普通 View 的 MeasureSpec 的创建规则。这个整个过程正好对应了文章开头那个表格,然后咱们再回来看下这个表格:
理解 MeasureSpec相关推荐
- 十八 、 View 的工作原理(2)---理解 MeasureSpec
MeasureSpec 是什么: 它是 Android 源码中 View.java 中的一个静态内部类: public static class MeasureSpec {private static ...
- MeasureSpec源码解读
文章目录 MeasureSpec的源码 MeasureSpec与LayoutParams 今天来讲讲MeasureSpec吧.因为他与View的测量流程相关性很大,只有正确的理解了MeasureSpe ...
- android view 绘制过程,深入理解Android中View绘制的三大流程
前言 最近对Android中View的绘制机制有了一些新的认识,所以想记录下来并分享给大家.View的工作流程主要是指measure.layout.draw这三大流程,即测量.布局和绘制,其中meas ...
- Android自定义视图四:定制onMeasure强制显示为方形
这个系列是老外写的,干货!翻译出来一起学习.如有不妥,不吝赐教! Android自定义视图一:扩展现有的视图,添加新的XML属性 Android自定义视图二:如何绘制内容 Android自定义视图三: ...
- Android View 测量流程(Measure)完全解析
前言 上一篇文章,笔者主要讲述了DecorView以及ViewRootImpl相关的作用,这里回顾一下上一章所说的内容:DecorView是视图的顶级View,我们添加的布局文件是它的一个子布局,而V ...
- 《Android开发艺术探索》读书笔记 (4) 第4章 View的工作原理
本节和<Android群英传>中的第3章Android控件架构与自定义控件详解有关系,建议先阅读该章的总结 第4章 View的工作原理 4.1 初始ViewRoot和DecorView ( ...
- Android自定义view之measure、layout、draw三大流程
自定义view之measure.layout.draw三大流程 一个view要显示出来,需要经过测量.布局和绘制这三个过程,本章就这三个流程详细探讨一下.View的三大流程具体分析起来比较复杂,本文不 ...
- View的测量规则以及三大方法流程
通过前面几篇的深入分析,相信大家对View的理解已经很深了,我们说了setContentView背后做了什么,说了View从xml加载到通过WindowManager添加View后的一系列操作,说了A ...
- Android高级特性笔记
##### 谈谈你对安卓签名的理解. ### 2.为什么WebView加载会慢呢? 这是因为在客户端中,加载H5页面之前,需要先初始化WebView,在WebView完全初始化完成之前,后续的界面加载 ...
- 【转载】Android 面试总结
Android面试整理 本文转载自 xiao_nian 的Android面试整理 本文转载自 xiao_nian 的Android面试整理 本文转载自 xiao_nian 的Android面试整理 一 ...
最新文章
- VS2010 发布web项目 问题
- Jvm(4),保持线程可见性的几种方法
- vue代理配置(vue+django前后端分离项目)
- BC30138: 无法在路径“C:\WINDOWS\TEMP\”中创建临时文件: 拒绝访问。
- iOS 字典与字符串之间的互转
- Filezilla 服务器发回了不可路由的地址。使用服务器地址代替
- Tensorflow并行计算:多核(multicore),多线程(multi-thread),计算图分割(Graph Partition)
- Oracle 使用GSON库解析复杂json串
- 分子重构技术_分子影像重构精准未来:百名专家云端共筑 One MI 生态圈
- java技术栈有哪些_2020 年 Java 程序员应该学习掌握哪些技术?
- 虚幻四中怎么保持导入模型坐标_[CG分享]|虚幻引擎5 技术解析
- FLTK学习笔记3-随机点名器
- 企业级AD域管理部署实战 微软升级版MCSE MCSA必修课程 Windows Server 2016AD管理实战
- 泛微协同办公系统移动服务器,泛微协同办公平台Ecology系统重装迁移指导手册.pdf...
- bp神经网络的主要功能,BP神经网络的实现包括
- 深度解析volatile关键字,就是这么简单
- ODT,ZQ校准,OCT,TDQS
- 从零双排java之Map
- 众里寻她千百度,他眼仅观她脸处--无处不在的注意力机制(self-attention)
- 功能最全面 体验更极致 小兴看看mini升级版9月19日首发