/** * 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 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); }
这个Measure算法主要是算child的测量方式和大小的。主要根据两种根据:
1. 自身的测量方式(MeasureSpec EXACTLY,AT_MOST,UNSPECIFIED)【这个是parent传过来的】
2. 自身的大小 【这个是parent传过来的】
3. 自己的padding【计算得到】
4. child的大小【child的LayoutParam得到】
根据以上四个推测出child的MeasureSpec 。也就是大小和测量方式。
child的请求大小来确定到底child应该有多宽以及应该怎么测量。
下面的逻辑比较多。
1.如果当前View是Exactly。那么也就是说有了固定的大小。
那么Child如果说是一个固定的LayoutParam.width 或者 height,那么返回结果resultSize 就设置为这个固定值。
如果child是wrapContent,那么resultSize 就设置为当前这个View的工作区大小。也就是去除pad和margin
的大小。同时,测量方式设置为AT_MOST
如果child是match_parent,那么由于当前View是固定大小的。那么resultSize 就设置为当前这个View的工作区大小。也就是去除pad和margin
的大小。同时,测量方式设置为EXACTLY
2.如果当前View是MeasureSpec.AT_MOST。也就是说有个最大值。也是这三种情况
a.fixed
这个时候,当前View可以无限大,并且child有个固定值。那么就设置这个值吧。 这个地方
,没有做越界判断,因此有时候会出现一个子View非常大,撑破了父View的情况。google在这里
算不算是一个bug呢?也就是当前的currrentView为wrap_content.但是parentView却有一个fixed value。 同时,childView也有一个fixed Value。这个时候,如果childView的大小超过了parentView。 则会出现childView显示不全的现象。
b.wrap_content
同理,传入AT_MOST.但是不超过当前View的大小。【这个是与当前View为EXACTLY的情况一样】
c.fill_parent
这种情况也非常奇怪。发生在
currentView是wrap_content.而childView是fill_parent的情况。这个时候就没办法搞了。
这个时候,就告诉子View你也没有固定的大小。也就是说循环依赖产生了。唯一的办法是让childView 继续wrap_content.也就是废掉这个fill_parent。从而打破了这种循环依赖。
相关推荐
用于图形检测领域的评价,具有更好的稳定性和准确性
使用 GPU 实现的一键快速评估显着性对象检测,包括MAE、Max F-measure、S-measure、E-measure。 代码从可从http://dpfan.net/ 获得的 matlab 版本重新实现 使用 pytorch 实现 GPU 可以更容易地嵌入到 eval 代码中...
Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch ...
The Measure of Success
This is a graduate level textbook on measure theory and probability theory. The book can be used as a text for a two semester sequence of courses in measure theory and probability theory, with an ...
MATLAB 显着性评估工具箱 该工具箱包含几乎所有显着对象检测的评估指标,包括: 电子测量 S-测量 加权 F 度量 F-测量 MAE分数 PR 曲线或条形指标 ...您可以轻松地使用此工具箱来评估 SOD 结果。...更多详情、使用方法,...
griffin-measure.jar
以SVM为分类器,以F1_measure、分类准确率和AUC为评价指标对该算法选择的基因子集进行评估,并将本算法与K-S检验、mRMR,以及经典的RELIEF和FAST算法进行比较。五个经典基因数据集上的平均实验结果表明:本算法的...
Measure-Theory:测度论讲义1.
Measure Line 1.3VR UnityVR测量工具 Measure Line 1.3VR UnityVR测量工具 Measure Line 1.3VR UnityVR测量工具Measure Line 1.3VR UnityVR测量工具Measure Line 1.3VR UnityVR测量工具
LOC-Measure03-04(代码step数计算工具)
android系统的消回音算法,voip必备
Measure Simple Ruler Application with an emphasis on Material design. Can display metric or imperial units. Built around custom RulerView class. Created as part of Job Interview process at Tinder. ...
CAM2 Measure v10 - FaroArm Training .pdf
unity测量线Measure+Line+1.3c unity 画线插件,知道两点的坐标可以在两点间画线
Privacy Friendly Tape Measure Privacy Friendly Tape Measure turns the phone screen into a ruler for quick measurements on the go. It also allows to measure size of things in a 2D scene provided a ...
《本源量子计算》课程最后一讲的代码实现,课程网址:https://ke.qq.com/course/413376?from=2&taid=3517226028519104
Measure & Sketch,一个在图片上标注距离的APP。
How Will You Measure Your Life
社会化推荐在一定程度上缓解了推荐中的数据稀疏性问题,但是通常仅考虑了社交网络中用户间的局部...通过与以往的算法在均方根误差、覆盖率和F-Measure等指标的实验结果表明,提出的算法在一定程度上提高了推荐的性能。