View的工作流程1

View的工作流程,指的就是measure,layout和draw。其中,measure用来测量View的宽和高。layout用来确定View的位置,draw用来绘制View。

View的工作流程入口

前面我们讲到Activity的构成。然后说到了DecorView的创建以及它加载的资源。这个时候呢,DecorView的内容还无法显示,因为它还没有被加载到Window中。接下来,我们重点学习DecorView如何被加载到Window中。

1DecorView被加载到Window中

当DrcorView创建完毕,要加载到Window中时,我们需要先了解一下,Activity的创建过程。

当我们调用Activity的startActivity的时候,最终是调用ActivityThread的handleLaunchActivity的,代码里有这么一句:

Activity a = performLaunchActivity(r,customIntent)

这里调用performLaunchActivity方法来创建Activity,在这个方法里会调用Activity的onCreate方法,从而完成DecorView的创建。

在接下来的代码里,还有handleResumeActivity方法调用,

@Override
    public void handleResumeActivity(ActivityClientRecord r, boolean finalStateRequest,
            boolean isForward, String reason) {
        // If we are getting ready to gc after going to the background, well
        // we are back active so skip it.
        unscheduleGcIdler();
        mSomeActivitiesChanged = true;

        // TODO Push resumeArgs into the activity for consideration
        // skip below steps for double-resume and r.mFinish = true case.
        if (!performResumeActivity(r, finalStateRequest, reason)) {
            return;
        }
        if (mActivitiesToBeDestroyed.containsKey(r.token)) {
            // Although the activity is resumed, it is going to be destroyed. So the following
            // UI operations are unnecessary and also prevents exception because its token may
            // be gone that window manager cannot recognize it. All necessary cleanup actions
            // performed below will be done while handling destruction.
            return;
        }

        final Activity a = r.activity;

        if (localLOGV) {
            Slog.v(TAG, "Resume " + r + " started activity: " + a.mStartedActivity
                    + ", hideForNow: " + r.hideForNow + ", finished: " + a.mFinished);
        }

        final int forwardBit = isForward
                ? WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION : 0;

        // If the window hasn't yet been added to the window manager,
        // and this guy didn't finish itself or start another activity,
        // then go ahead and add the window.
        boolean willBeVisible = !a.mStartedActivity;
        if (!willBeVisible) {
            willBeVisible = ActivityClient.getInstance().willActivityBeVisible(
                    a.getActivityToken());
        }
        if (r.window == null && !a.mFinished && willBeVisible) {
            r.window = r.activity.getWindow();
            View decor = r.window.getDecorView();
            decor.setVisibility(View.INVISIBLE);
            ViewManager wm = a.getWindowManager();
            WindowManager.LayoutParams l = r.window.getAttributes();
            a.mDecor = decor;
            l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
            l.softInputMode |= forwardBit;
            if (r.mPreserveWindow) {
                a.mWindowAdded = true;
                r.mPreserveWindow = false;
                // Normally the ViewRoot sets up callbacks with the Activity
                // in addView->ViewRootImpl#setView. If we are instead reusing
                // the decor view we have to notify the view root that the
                // callbacks may have changed.
                ViewRootImpl impl = decor.getViewRootImpl();
                if (impl != null) {
                    impl.notifyChildRebuilt();
                }
            }
            if (a.mVisibleFromClient) {
                if (!a.mWindowAdded) {
                    a.mWindowAdded = true;
                    wm.addView(decor, l);
                } else {

在这个方法里,performeResumeActivity会有调用,而在performResumeActivity里,会调用Activity的onResume方法。

看下面这句代码:

View decor = r.window.getDecorView();

这里获得了DecorView,接着获取到了WindowManager,WindowManager是一个接口并且继承了接口ViewManager。在最后调用wm.addView方法。WindowManager的实现类是WindowManagerImpl,所以实际调用的是WindowManagerImpl的addView方法。里面有这样一句代码:

mGloal.addView(view,params,mDisplay,mParentWindow);

可以看到,在WindowManagerImpl的addView方法中,又调用了WindowManagerGlobal的addView方法,在这个方法中,有这样一句:

root = new ViewRootImpl(view.getContext(),display);

这句代码创建了ViewRootImpl的实例,接着下面还有这样一句代码:

root.setView(view,wparams,panelParentView);

这里调用ViewRootImpl的setView方法将DeccorView作为参数传递进去。这样就把DecorView加载到了Window中。当然,这个时候界面仍然不会显示出什么,因为View的工作流程还没有执行完,还需要经过measure,layout以及draw才会把view绘制出来。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值