DecorView的测量过程

Posted huyang011

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DecorView的测量过程相关的知识,希望对你有一定的参考价值。

 

DecorView的MeasureSpec的创建过程

在ViewRootImpl中的mearsureHierarchy方法中有一段代码展示了DecorView的MeasureSpec的创建过程:

       childWidthMeasureSpec = getRootMeasureSpec(baseSize, lp.width);
       childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
       performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);

看一下gerRootMeasureSpec方法:

    /**
     * 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的测量过程的主要内容,如果未能解决你的问题,请参考以下文章

如何测量代码片段的调用次数和经过时间

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

View绘制源码浅析(二)布局的测量布局绘制

View的layout机制

Android绘制源码分析(下)

设置自定义dialog的正确宽高