欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android高级UI之绘制流程分析

程序员文章站 2024-03-24 09:59:04
...


要了解绘制流程,首先我们要了解View的加载过程,我们从setContentView开始

MainActivity->Activity

 /**
     * Set the activity content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the activity.
     *
     * @param layoutResID Resource ID to be inflated.
     *
     * @see #setContentView(android.view.View)
     * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentView(@LayoutRes int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

我们可以看到是通过getWindow()来进行setContentView,getWindow()到底是什么呢,我们进行源码跟踪,定位Window

Android高级UI之绘制流程分析

Android高级UI之绘制流程分析

我们可以发现Window是一个抽象类,它有唯一一个子类就是PhoneWindow,因此我们setContentView是在PhoneWindow里面实现的

Android高级UI之绘制流程分析

   我们来看一下PhoneWindow中setContentView中到底做了些什么

  @Override
    public void setContentView(int layoutResID) {
        // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
        // decor, when theme attributes and the like are crystalized. Do not check the feature
        // before this happens.
        if (mContentParent == null) {
            installDecor();//初始化DecorView
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            mContentParent.removeAllViews();
        }

        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                    getContext());
            transitionTo(newScene);
        } else {
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        mContentParent.requestApplyInsets();
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
        mContentParentExplicitlySet = true;
    }

我们发现如果mContentParetn==null的话,我们需要调用installDecor()方法,这个方法主要是用于初始化DecorView

转自http://www.iblogstreet.com/2017/06/27/android_advance_ui_draw_flow_analysis.html

相关标签: Android 绘制