View的工作流程1

Posted 呼啸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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绘制出来。

以上是关于View的工作流程1的主要内容,如果未能解决你的问题,请参考以下文章

View测量流程与困惑

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

反思|Android View机制设计与实现:测量流程

Android View 的测量流程详解

分析Android中View的工作流程

View绘制流程二:测量布局绘制