`
daojin
  • 浏览: 695745 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

安卓高手之路 图形系统(4 Measure的算法)

 
阅读更多

 	    


 /**
     * 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。从而打破了这种循环依赖。

分享到:
评论

相关推荐

    S-measure(matlab)_S-measure_libraryzsp_

    标题中的"S-measure(matlab)_S-measure_libraryzsp_"提到了S-measure,这是一个在图像分析和图形检测领域中使用的评价指标。S-measure,全称Structural Similarity Measure,是针对图像分割结果的质量评估工具,它在...

    apm_test.rar_android_android 回音_apm_far.pcm_flatness measure_voi

    这个名为"apm_test.rar_android_android 回音_apm_far.pcm_flatness measure_voi"的压缩包文件似乎专注于Android系统中的回音消除算法,这对于实现高质量的VoIP(Voice over IP)服务至关重要。以下是对这个主题的...

    PR-F_Measure.zip_f measure _f-measure_ground truth_ground truth

    F_Measure.zip_f measure _f-measure_ground truth_ground truth”指的是一个压缩文件,其中包含了一个名为“PR-F_Measure”的应用程序,该应用主要用于评估图像处理中的边缘检测方法,如Canny和Sobel算法。...

    使用 GPU 实现的一键快速评估显着性对象检测,包括 MAE、Max F-measure、S-measure、E-measure

    使用 GPU 实现的一键快速评估显着性对象检测,包括MAE、Max F-measure、S-measure、E-measure。 代码从可从http://dpfan.net/ 获得的 matlab 版本重新实现 使用 pytorch 实现 GPU 可以更容易地嵌入到 eval 代码中...

    cesium-measure.rar

    在GIS(地理信息系统)领域,Cesium是一款强大的开源JavaScript库,专用于创建交互式的3D地球浏览器。这个“cesium-measure.rar”压缩包文件很可能包含了一组资源和示例,帮助用户在Cesium环境中实现空间测量功能,...

    Sketch Measure 插件

    Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch Measure 插件Sketch ...

    k_means 算法

    K-means算法是一种广泛使用的聚类算法,在数据挖掘领域中是应用最多的分割算法之一。然而,K-means算法存在一些固有的局限性,例如初始质心选择的随机性可能会影响最终聚类结果的质量,而且算法对异常值敏感。此外,...

    ads.rar_Measure For Measure_OCT

    《OCT测量技术在"Measure for Measure"中的应用》 在现代医学成像技术中,光学相干断层扫描(Optical Coherence Tomography, OCT)以其高分辨率、非侵入性的特性,在眼科、皮肤科、心血管科等多个领域得到了广泛...

    measurepos.rar_measure_pos_measurepos_opencv卡尺_一维测量_卡尺测量

    4. 直线拟合:通过对边缘点进行拟合,找到最佳直线,这条直线可以被视为测量的标准线。 5. 测量计算:根据拟合的直线和已知的像素到真实世界单位的转换比例,计算出目标物体的实际尺寸。 6. 结果显示:将测量结果...

    Imatest_SFR Measure

    通过Slanted-edge算法,SFR可以准确测量成像系统的MTF(Modulation Transfer Function),也就是调制传递函数。MTF值越高,表示系统能够传递更多的细节信息,成像越清晰。MTF值通常以百分比形式表达,100%代表完全...

    HED-BSDS用于边缘检测hed算法的实现

    4. **训练脚本**:用于训练HED模型的脚本,可能包括参数设置、损失函数定义、优化器选择等。 5. **评估脚本**:用于计算模型在BSDS数据集上的性能指标,如精度、F-measure等。 6. **文档**:详细说明如何使用这些...

    android系统调试

    在Android系统调试这一主题中,我们主要探讨的是如何在Android平台上对应用程序或系统服务进行调试,以优化性能、解决bug并提升用户体验。以下是一些关键的知识点,它们涵盖了Android系统框架的理解、系统原理以及...

    此工具箱包含 E- measure、S - measure、加权 F 和 F- measure、MAE 和 PR 曲线或显着对象

    MATLAB 显着性评估工具箱 该工具箱包含几乎所有显着对象检测的评估指标,包括: 电子测量 S-测量 加权 F 度量 F-测量 MAE分数 PR 曲线或条形指标 ...您可以轻松地使用此工具箱来评估 SOD 结果。...更多详情、使用方法,...

    griffin-measure.jar

    griffin-measure.jar

    K-S检验与mRMR相结合的基因选择算法

    以SVM为分类器,以F1_measure、分类准确率和AUC为评价指标对该算法选择的基因子集进行评估,并将本算法与K-S检验、mRMR,以及经典的RELIEF和FAST算法进行比较。五个经典基因数据集上的平均实验结果表明:本算法的...

    用遗传算法实现自动文摘

    标题 "用遗传算法实现自动文摘" 指涉的核心技术是遗传算法(Genetic Algorithm,GA)在自然语言处理中的应用,特别是自动文摘领域。自动文摘是指通过计算机程序从大量文本中提取关键信息,生成简洁、连贯的摘要,而...

    NCCB和WCPO算法.zip

    而"wcpo_smi_lsmi.m"文件可能是WCPO算法的一个变种或应用,"SMI"和"LSMI"可能是"Sorted Measure Index"和"Least Squares Minimization with Index"的缩写,分别指的是排序指标和带指标的最小二乘拟合。这个文件可能...

Global site tag (gtag.js) - Google Analytics