绘制流程

ActivityThread

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    ...
    
    //这里是创建Activity,并调用了 Activity 的 onCreate()和onStart()
    Activity a = performLaunchActivity(r, customIntent);

    if (a != null) {
        r.createdConfig = new Configuration(mConfiguration);
        Bundle oldState = r.state;
        //这里调用Activity 的 onResume()
        handleResumeActivity(r.token, false, r.isForward,
                !r.activity.mFinished && !r.startsNotResumed);
    }
    ....
}

performLaunchActivity

private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
    ......
    //这里底层是通过反射来创建的Activity实例
    java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
    activity = mInstrumentation.newActivity(cl, component.getClassName(), r.intent);

    //底层也是通过反射构建Application,如果已经构建则不会重复构建,毕竟一个进程只能有一个Application
    Application app = r.packageInfo.makeApplication(false, mInstrumentation);

    if (activity != null) {
        Context appContext = createBaseContextForActivity(r, activity);
        CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
        Configuration config = new Configuration(mCompatConfiguration);
        if (DEBUG_CONFIGURATION) Slog.v(TAG, "Launching activity "
                + r.activityInfo.name + " with config " + config);
        //分析2 : 在这里实例化了PhoneWindow,并将该Activity设置为PhoneWindow的Callback回调,还初始化了WindowManager
        activity.attach(appContext, this, getInstrumentation(), r.token,
                r.ident, app, r.intent, r.activityInfo, title, r.parent,
                r.embeddedID, r.lastNonConfigurationInstances, config);

        //间接调用了Activity的performCreate方法,间接调用了Activity的onCreate方法.
        mInstrumentation.callActivityOnCreate(activity, r.state);
        
        //这里和上面onCreate过程差不多,调用Activity的onStart方法
        if (!r.activity.mFinished) {
            activity.performStart();
            r.stopped = false;
        }
        ....
    }
}

Instrumentation#newActivity()

Activity#attach()

callOnCreate

perform start

handleResume

调用 ViewRootImpl 的 setView 方法,将 decorView 与ViewRootImpl关联。ViewRootImpl 中有一个 mView字段保存。

ViewRootImpl

由 ViewRootImpl 的 scheduleTraversals 方法发起,方法通过给 Choreographer 发送一个callback,在下一帧时发起绘制流程。

mTraversalRunnable 调用了 doTraversals:

doTraversal 调用了 performTraversals:

真正绘制在 performTraversals 中进行,代码较长,方法源码链接 ,主要流程:

performMeasure,调用顶层view的measure方法,开始测量

performLayout,会调用view的layout方法,开始布局,performDarw 会调用draw方法,然后调用drawSoftWare,继而调用view的draw方法,分发绘制。

View

measure

measure 会调用 onMeasure,对于ViewGroup onMeasure 还对先测量每个子View,即调用它们的measure方法,向下传递measure过程

layout

layout 会调用onLayout,ViewGroup 会通过调用子View的layou来向下传递布局过程。

draw

dispatchDraw 会对每个子View 调用drawChild,继而调用子view的draw 方法,完成绘制分发。

https://blog.csdn.net/xfhy_/article/details/90270630

最后更新于

这有帮助吗?