Android View显示过程

本文深入探讨了Android中View从加载layout文件到显示在屏幕上的完整过程,涉及Activity的生命周期、WindowManager、Surface与SurfaceFlinger的关联。通过分析源码,详细阐述了测量、布局、绘制以及Surface与图形显示的关联机制。
摘要由CSDN通过智能技术生成

研究在布局文件加载之后,View是如何被显示到屏幕上来的,也就是研究View的显示过程。下面将从以下两个方面做介绍:1、从加载layout文件到view的测量、布局和绘制的全过程。2、surface、surfaceFlinger与view是如何关联起来的。 
      在Activity的onCreate方法中,我们通过setContentView(view)来初始化定义好的一个view,而此时view还没有显示出来,在Activity执行了onResume方法后,界面才显示完成,这说明了view的显示经历了Activity的这两个过程。下面是启动Activity与加载view的一个简单过程图。 
这里写图片描述

一、过程分析 
      下面将会以ActivityThread类为起点,一步步跟踪view的显示过程。 
在ActivityThread类中有一个Handler对象,它负责处理服务端传过来的消息。在接收到LAUNCH_ACTIVITY消息后,执行handleLaunchActivity方法,也就是启动一个Activity。

case LAUNCH_ACTIVITY: {
    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "activityStart");
    ActivityClientRecord r = (ActivityClientRecord)msg.obj;
    r.packageInfo = getPackageInfoNoCheck(r.activityInfo.applicationInfo, r.compatInfo);
    handleLaunchActivity(r, null);
    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
} break;
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在此方法中,首先解析package信息,然后调用handleLaunchActivity方法来执行启动Activity。 
我们继续跟踪handleLaunchActivity方法。

// 上面代码省略
// Make sure we are running with the most recent config.
handleConfigurationChanged(null, null);

if (localLOGV) Slog.v(TAG, "Handling launch of " + r);
Activity a = performLaunchActivity(r, customIntent);

if (a != null) {
    r.createdConfig = new Configuration(mConfiguration);
    Bundle oldState = r.state;
    handleResumeActivity(r.token, false, r.isForward,
            !r.activity.mFinished && !r.startsNotResumed);

    if (!r.activity.mFinished && r.startsNotResumed) {
        // The activity manager actually wants this one to start out
        // paused, because it needs to be visible but isn't in the
        // foreground.  We accomplish this by going through the
        // normal startup (because activities expect to go through
        // onResume() the first time they run, before their window
        // is displayed), and then pausing it.  However, in this case
        // we do -not- need to do the full pause cycle (of freezing
        // and such) because the activity manager assumes it can just
        // retain the current state it has.
        try {
            r.activity.mCalled = false;
            mInstrumentation.callActivityOnPause(r.activity);
            // We need to keep around the original state, in case
            // we need to be created again.  But we only do this
            // for pre-Honeycomb apps, which always save their state
            // when pausing, so we can not have them save their state
            // when restarting from a paused state.  For HC and later,
            // we want to (and can) let the state be saved as the normal
            // part of stopping the activity.
            if (r.isPreHoneycomb()) {
                r.state = oldState;
            }
            if (!r.activity.mCalled) {
                throw new SuperNotCalledException(
                    "Activity " + r.intent.getComponent().toShortString() +
                    " did not call through to super.onPause()");
            }
        } catch (SuperNotCalledException e) {
            throw e;
        } catch (Exception e) {
            if (!mInstrumentation.onException(r.activity, e)) {
                throw new RuntimeException(
                        "Unable to pause activity "
                        + r.intent.getComponent().toShortString()
                        + ": " + e.toString(), e);
            }
        }
        r.paused = true;
    }
} else {
    // If there was an error, for any reason, tell the activity
    // manager to stop us.
    try {
        ActivityManagerNative.getDefault()
            .finishActivity(r.token, Activity.RESULT_CANCELED, null);
    } catch (RemoteException ex) {
        // Ignore
    }
}
 
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

此方法注释比较多,过程非常简单,教容易理解,主要是执行performLaunchActivity和handleResumeActivity方法,他们分别执行Activity的onCreate,onStart和onResume生命周期。 
我们继续跟踪performLaunchActivity方法

Activity activity = null;
try {
    java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
    activity = mInstrumentation.newActivity(
            cl, component.getClassName(), r.intent);
    StrictMode.incrementExpectedActivityCount(activity.getClass());
    r.intent.setExtrasClassLoader(cl);
    if (r.state != null) {
        r.state.setClassLoader(cl);
    }
} catch (Exception e) {
    if (!mInstrumentation.onException(activity, e)) {
        throw new RuntimeException(
            "Unable to instantiate activity " + component
            + ": " + e.toString(), e);
    }
}

try {
    Application app = r.packageInfo.makeApplication(false, mInstrumentation);

    if (localLOGV) Slog.v(TAG, "Performing launch of " + r);
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值