View的工作原理(一)——从ViewRoot和DecorView说起

简介: 版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u013132758。 https://blog.csdn.net/u013132758/article/details/52150950 前言本文参考《Android开发艺术与探索》第四章内容及网上几篇博客,里面融入笔者的个人理解。
版权声明:本文为博主原创文章,转载请注明出处http://blog.csdn.net/u013132758。 https://blog.csdn.net/u013132758/article/details/52150950

前言

本文参考《Android开发艺术与探索》第四章内容及网上几篇博客,里面融入笔者的个人理解。希望能对大家理解View有所帮助。

基本概念介绍

介绍View的工作原理之前我们首先要理解DecorView和ViewRoot两个概念:

1、DecorView

DecorView是Windows中的View的最顶层View。我们可以根据下面一副图来认识它:


由这幅图我们可以看到 ,其实DecorView是一个FrameLayout,里面是一个垂直的线性布局,在线性布局中分上下两部分FrameLayout,上面一部分是TitleBar,下面是android.R.id.content,我们平常的setContentView就是将布局加载在android.R.id.content中。

2、ViewRoot

ViewRoot是连接WindowsManager和DecorView的桥梁对应于ViewRootImpl。

View的绘制流程就是从ViewRootImpl的performTraversala()方法开始的,包含三大流程:

1、Measure():[ 测量流程]

2、Layout():[布局流程]

3、Draw():[绘制流程]

这三大流程也就是View绘制的三大流程。我们可以通过下面两幅图来理解performTrarsala()方法;


                                                                                                                                                              

什么是MeasureSpec

MeasureSpec  从名字上来看看起来是“测量规格“或是”测量说明书“。大致意思就是决定View的Measure过程。我们可以这样来理解:”MeasureSpec在很大程度上决定了一个View的尺寸规格,之所以这样说是很大程度上是因为这个过程还受父容器的影响,因为父容器影响View的MeasureSpec的创建过程。“

1、MeasureSpec

MeasureSpec代表一个32位int值,高2位代表SPecMode(测量模式),低30位代表SpecSize(在某种测量模式下的规格大小)。


SpecMode有三类如下所示:
  • UNSPECIFIED(未指定模式)
        父容器不对View有任何限制,要多大给多大。
  • EXACTLY(确定模式)
        父容器已检测出View所需要的精确大小,这时候View的最终大小就是SpecSize指定的值,它对应于LayoutParams中的match_parent和具体指定的数值这两种模式。
  • AT_MOST(最多模式)
          父容器指定一个可用大小即SpecSize,View的代销不能大于这个值。对应于wrap_content。

2、MeasureSPec 和LayoutParams的对应关系

为什么要说MeasureSpec和LayoutParams对应昵?那是因为View在测量的时候会将我们设置的LayoutParams 在父容器的约束条件下 转换成MeasureSpec,然后再根据MeasureSpec来确定View测量后的宽和高。
有些读者会问:那么顶级View(DecorView)怎么转换昵?
对于顶级View:MeasureSpec有窗口尺寸和自身的LayoutParams来共同决定。
对于普通View:由父容器的MeasureSpec和自身的LayoutParams共同来决定。
通过下面一段代码我们来理解DecorView的创建MeasureSpec过程,desiredWindowWidth和desiredWindowHeight是屏幕尺寸:

childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
host.measure(childWidthMeasureSpec, childHeightMeasureSpec);


我们来再看看getRootMeasureSpec方法:


private 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;
    }

在android.view.ViewRootImpl  中可以看到其对应关系LayoutParams 中这三个值在内部有个对应关系,那就是

LayoutParams.MATCH_PARENT  对应 MeasureSpec.EXACTLY

.LayoutParams.WRAP_CONTENT对应  MeasureSpec.AT_MOST

默认值(也就是具体值) 对应 MeasureSpec.EXACTLY

也就是内部只有两种模式 EXACTLY 精确模式 和 AT_MOST 最大模式!


对于普通View它的measure是由ViewGroup传递而来,我们看一下ViewGroup的measureChildWidthMargins方法:

/**
     * Ask one of the children of this view to measure itself, taking into
     * account both the MeasureSpec requirements for this view and its padding
     * and margins. The child must have MarginLayoutParams The heavy lifting is
     * done in getChildMeasureSpec.
     *
     * @param child The child to measure
     * @param parentWidthMeasureSpec The width requirements for this view
     * @param widthUsed Extra space that has been used up by the parent
     *        horizontally (possibly by other children of the parent)
     * @param parentHeightMeasureSpec The height requirements for this view
     * @param heightUsed Extra space that has been used up by the parent
     *        vertically (possibly by other children of the parent)
     */
    protected void measureChildWithMargins(View child,
            int parentWidthMeasureSpec, int widthUsed,
            int parentHeightMeasureSpec, int heightUsed) {
        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin // 考虑上parent的padding和child的margin
                        + widthUsed, lp.width);
        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
                        + heightUsed, lp.height);
        // 这里如果child是个ViewGroup类型,则实际会递归下去。。。
        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

 
  我们可以清楚的看到子View的MeasureSpec创建与父容器的MeasureSpec和子元素本身的LayoutParams有关,此外还和VIew的margin 和 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) {
        // 这个方法是协商型的,最终结果既可能直接由spec(parent提供的),也可能由childDimension决定
        // 所以我们知道了,一个View的大小不是简单的单方面决定的,而是通过一系列条件协商的结果,
        // 有时会尊重parent的spec,有时会坚持自己的dimension要求
        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: // parent说child你应该是个确定的大小
            if (childDimension >= 0) { // child正好设置了确定的大小
                resultSize = childDimension; // 让child是那个确定的大小
                resultMode = MeasureSpec.EXACTLY; // 设置mode为EXACTLY
            } else if (childDimension == LayoutParams.MATCH_PARENT) {
                // Child wants to be our size. So be it.
                resultSize = size; // 其他情况下都是parent spec中的大小,只是mode不同
                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; // 不能超过size
                resultMode = MeasureSpec.AT_MOST;
            }
            break;

        // Parent has imposed a maximum size on us
        case MeasureSpec.AT_MOST: // parent说child你应该最大是某个值。。。
            if (childDimension >= 0) { // child指定确定值了,则听child的
                // 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: // parent没对child的大小有啥要求
            if (childDimension >= 0) { // child指定了确定的值,听child的
                // 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);
    }

从上述方法中我们不难理解, 子View的MeasureSpec创建与父容器的MeasureSpec和子元素本身的LayoutParams有关。

下面一张表格对MeasureSpec和LayoutParams对应做个总结:
                                                              
                                                                  普通View的MeasureSpec创建规则


                              parentSpecMode

childLayoutParams

       EXACTLY

          AT_MOST

         UNSPECIFIED

               dp/px

        EXACTLY

       chiladSize

         EXACTLY

         chiladSize

        EXACTLY

        childSize

 

          match_parent

        EXACTLY

        parentSize

        AT_MOST

        parentSize

     UNSPECIFIED

                0

          wrap_content

        EXACTLY

        parentSize

        AT_MOST

        parentSize

     UNSPECIFIED

                0




相关文章
|
传感器 监控 安全
闭环反馈系统原理概述
有时,为了获得系统的一致性和稳定性并产生控制系统的期望输出,我们使用反馈回路。反馈只不过是输出信号的一部分。这个概念在控制系统中最常见和最重要,以实现输出的稳定性。根据反馈连接,控制系统分为两种类型。它们是开环控制系统和闭环控制系统。下面简单介绍下闭环反馈系统。
5712 0
闭环反馈系统原理概述
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
深入解析:AI如何生成文章的奥秘与技术实现
AI生成文章技术历经规则模板、统计模型到深度学习的演进,尤其Transformer架构推动其飞跃发展。如今,AI已广泛应用于新闻、营销、创作等领域,提升效率的同时,也面临事实准确性、伦理等挑战。未来将趋向人机协同、多模态融合与负责任发展,重塑内容生态。
1436 0
|
12月前
|
数据安全/隐私保护
阿里云号码百科支持哪些功能?号码百科不同功能收费价格表整理
阿里云号码百科提供号码归属、状态查询、二次号检测、在网时长及运营商/企业/身份证要素核验等服务,支持加密通信与风控分析,按套餐包计费,广泛应用于注册验证、广告归因与安全防控场景。
1435 0
|
12月前
|
数据可视化 Java 关系型数据库
01-认识Activiti
工作流指具有审批流程的业务(如请假、报销等),通过流程引擎实现可视化管理。它支持单/多节点审批,广泛应用于CRM、TMS、WMS等系统。主流技术包括BPMN、Activiti、Flowable等,推动了泛微、金蝶、用友等企业的发展。
|
机器学习/深度学习 算法
Proximal SFT:用PPO强化学习机制优化SFT,让大模型训练更稳定
本文介绍了一种改进的监督微调方法——Proximal Supervised Fine-Tuning (PSFT),旨在解决传统SFT易过拟合、泛化能力差及导致“熵坍塌”的问题。受PPO强化学习算法启发,PSFT通过引入参数更新的稳定性机制,防止模型在训练中变得过于确定,从而提升探索能力与后续强化学习阶段的表现。实验表明,PSFT在数学推理、模型对齐及泛化能力方面均优于传统SFT。
1174 3
Proximal SFT:用PPO强化学习机制优化SFT,让大模型训练更稳定
|
人工智能 物联网
ART:匿名区域布局+多层透明图像生成技术,生成速度比全注意力方法快12倍以上
ART 是一种新型的多层透明图像生成技术,支持根据全局文本提示和匿名区域布局生成多个独立的透明图层,具有高效的生成机制和强大的透明度处理能力。
727 8
ART:匿名区域布局+多层透明图像生成技术,生成速度比全注意力方法快12倍以上
|
JSON API 数据安全/隐私保护
车辆五项信息查询 API 的实践指南:通过Python调用赋能车辆信息标准化
本API通过车牌号快速获取车辆五项核心信息,包括品牌、登记日期、车架号等,助力二手车评估、维修、保险等场景实现数字化转型。数据源自权威公安交管库,日更同步,毫秒级响应,满足高并发需求,符合隐私保护规范,是推动汽车后市场智能化的重要工具。
1081 0
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
743 16
|
人工智能 IDE 开发工具
《C++人工智能开发 IDE 全解析:助力智能创新之路》
本文深入探讨了几款适合 C++ 人工智能开发的 IDE,包括 Visual Studio、CLion、Eclipse CDT 和 Qt Creator。每款 IDE 都有其独特的优势,如 Visual Studio 的强大调试工具、CLion 的代码导航和 CMake 支持、Eclipse CDT 的跨平台能力和丰富的插件生态系统,以及 Qt Creator 在界面开发方面的卓越表现。开发者应根据项目需求、团队协作和个人习惯选择最合适的 IDE,以提升开发效率和体验。
752 16
|
数据可视化 IDE 数据挖掘
Python助您洞察先机:2024年A股市场数据抓取与分析实战
【10月更文挑战第1天】随着2024年中国股市的强劲表现,投资者们对于如何高效获取并分析相关金融数据的需求日益增长。本文旨在介绍如何利用Python这一强大的编程语言来抓取最新的A股交易数据,并通过数据分析技术为个人投资决策提供支持。
2464 2