布局是怎么被添加到Activity窗口中的

前言

之前简单分析了setContentView的一个执行流程,但是我们还是不清楚我们的布局是怎么添加到Activity窗口当中的,要解决这个问题,我们必须从Activity启动的源码入手。

流程分析

  • ActivityThread.java
    public static void main(String[] args) {
     // 代码省略
        Looper.prepareMainLooper();
        ActivityThread thread = new ActivityThread();
        thread.attach(false);
        if (sMainThreadHandler == null) {
            sMainThreadHandler = thread.getHandler();
        }
        Looper.loop();
    }

很多曾经从事Java,现在从事Android的人都好奇为什么Android没有main方法,其实是有的。通过阅读相关文档,我们知道ActivityThread是应用程序真真的入口,每个应用程序有且只有一个ActivityThread,由它来负责进行所在进程的主线程消息循环处理。在ActivityThread的main方法中,我们看到它初始化了主线程handler,开启了消息循环,那么它的handler里面循环处理什么工作呢?

  • ActivityThread.java

public void handleMessage(Message msg) {
            switch (msg.what) {
                case LAUNCH_ACTIVITY: {
                    handleLaunchActivity(r, null);
                } break;
// 代码省略...
}

private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// 代码省略...
    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);
                }
 // 代码省略...
}

handleMessage里面处理的事情基本上是与四大组件相关,比如开启暂停停止一个Activity或Service等,这里我们只用关心LAUNCH_ACTIVITY,看看它里面做了什么。

我们发现在LAUNCH_ACTIVITY里面,它调用了handleLaunchActivity,而handleLaunchActivity里面又调用了handleResumeActivity。

  • ActivityThread.java
    final void handleResumeActivity(IBinder token,
        // 代码省略
        ActivityClientRecord r = performResumeActivity(token, clearHide);
                if (r != null) {
            final Activity a = r.activity;
            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 (a.mVisibleFromClient) {
                    a.mWindowAdded = true;
                    wm.addView(decor, l);
                }
        }
            
            // 代码省略

在handleResumeActivity里面,首先是通过调用performResumeActivity方法获取了当前Activity的标识(ActivityClientRecord持有当前Activity以及相关的各种状态信息),然后再来获取当前Activity关联的Window对象以及WindowManager对象,最后将DecorView添加到WindowManager中。那么WindowManager中是怎么实现的呢?

  • WindowManager.java
public interface WindowManager extends ViewManager {
//代码省略
}


可以发现,WindowManager只是一个接口,那么它的实现类在什么地方呢,从handleResumeActivity方法中我们可以看到,WindowManager是通过Activity获取的,所以我们还是要从Activity的代码中去寻找它的实现类。

  • ActivityThread.java

    private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
   
//代码省略
                activity.attach(appContext, this, getInstrumentation(), r.token,
                        r.ident, app, r.intent, r.activityInfo, title, r.parent,
                        r.embeddedID, r.lastNonConfigurationInstances, config,
                        r.referrer, r.voiceInteractor);

//代码省略
                activity.mCalled = false;
                if (r.isPersistable()) {
                    mInstrumentation.callActivityOnCreate(activity, r.state, r.persistentState);
                } else {
                    mInstrumentation.callActivityOnCreate(activity, r.state);
                }
//代码省略
        return activity;
    }

  • Activity.java

public class Activity extends ContextThemeWrapper
        implements LayoutInflater.Factory2,
        Window.Callback, KeyEvent.Callback,
        OnCreateContextMenuListener, ComponentCallbacks2,
        Window.OnWindowDismissedCallback {
            
public WindowManager getWindowManager() {
    return mWindowManager;
}

final void attach(Context context, ActivityThread aThread..)

  //省略
    mWindow.setWindowManager(
            (WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
            mToken, mComponent.flattenToString(),
            (info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
    if (mParent != null) {
        mWindow.setContainer(mParent.getWindow());
    }
    mWindowManager = mWindow.getWindowManager();
    mCurrentConfig = config;
}
            
}

  • Window.java

    public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
            boolean hardwareAccelerated) {
        mAppToken = appToken;
        mAppName = appName;
        mHardwareAccelerated = hardwareAccelerated
                || SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
        if (wm == null) {
            wm = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
        }
        mWindowManager = ((WindowManagerImpl)wm).createLocalWindowManager(this);
    }
    

我们知道,当一个Activity启动,会调用ActivityThread的performLaunchActivity,然后在performLaunchActivity里面会首先调用Activity的attach方法,再调用onCreate方法。而在Activity的attach方法里面可以看到,Activity的WindowManager对象实际上是来自于Window,而Window的setWindowManager方法里面揭示出WindowManager的实现类是WindowManagerImpl。

  • WindowManagerImpl.java

    private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

    @Override
    public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
        applyDefaultToken(params);
        mGlobal.addView(view, params, mDisplay, mParentWindow);
    }

  • WindowManagerGlobal.java

    public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow) {
//代码省略
        ViewRootImpl root;
        View panelParentView = null;
            }
            if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
                    wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
//代码省略
            root = new ViewRootImpl(view.getContext(), display);
            view.setLayoutParams(wparams);
            mViews.add(view);
            mRoots.add(root);
            mParams.add(wparams);
        }
        root.setView(view, wparams, panelParentView);
//代码省略

*ViewRootImpl.java


    public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
        //代码省略
                requestLayout();  // 调用View的绘制
                if ((mWindowAttributes.inputFeatures
                        & WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
                    mInputChannel = new InputChannel();
                }
                try {
                    mOrigWindowType = mWindowAttributes.type;
                    mAttachInfo.mRecomputeGlobalAttributes = true;
                    collectViewAttributes();
                    // 最终的添加显示
                    res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(),
                            mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                            mAttachInfo.mOutsets, mInputChannel);
                } 
        }

在WindowManagerImpl的addView方法中,我们发现它又是调用的WindowManagerGlobal的addView方法,而在WindowManagerGlobal的addView方法中,通过层层传递,它会把DecorView传递给ViewRootImpl的setView,然后在ViewRootImpl中进行最终的绘制工作。

在ViewRootImpl中,有二个地方需要我们注意,一个是requestLayout()方法,一个是mWindowSession.addToDisplay方法,mWindowSession是系统服务WindowManagerService在应用程序中创建的代理对象,它最终会通过Binder跨进程调用WindowManagerService的addWindow()方法,然后增加窗口界面。

而requestLayout()方法里面则是进行具体界面绘制的关键方法。

*ViewRootImpl.java


    @Override
    public void requestLayout() {
        if (!mHandlingLayoutInLayoutRequest) {
            checkThread();
            mLayoutRequested = true;
            scheduleTraversals();
        }
    }
    
    
void scheduleTraversals() {
    if (!mTraversalScheduled) {
        mTraversalScheduled = true;
        mTraversalBarrier = mHandler.getLooper().getQueue().postSyncBarrier();
        mChoreographer.postCallback(
                Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
        if (!mUnbufferedInputDispatch) {
            scheduleConsumeBatchedInput();
        }
        notifyRendererOfFramePending();
        pokeDrawLockIfNeeded();
    }
}

final TraversalRunnable mTraversalRunnable = new TraversalRunnable();

final class TraversalRunnable implements Runnable {
    @Override
    public void run() {
        doTraversal();
    }
}

void doTraversal() {
     if (mTraversalScheduled) {
         mTraversalScheduled = false;
         mHandler.getLooper().getQueue().removeSyncBarrier(mTraversalBarrier);

         if (mProfile) {
             Debug.startMethodTracing("ViewAncestor");
         }

         performTraversals();

         if (mProfile) {
             Debug.stopMethodTracing();
             mProfile = false;
         }
     }
 }
 
  //真正执行UI绘制的遍历过程

private void performTraversals() {
// 代码省略
    performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
    performLayout(lp, desiredWindowWidth, desiredWindowHeight);
    performDraw();
  }

在requestLayout里面调用了scheduleTraversals()方法,在scheduleTraversals()方法里面开启了一个叫做TraversalRunnable的线程进行执行遍历,而在执行遍历的doTraversal()方法里面,它最终调用了performTraversals()方法,这个方法是最终的界面绘制的方法,这个方法实际上是调用了DecorView的measure、layout、draw方法进行View树绘制遍历。

总结

最后我们来总结下布局添加到Activity窗口的具体流程。

布局是怎么被添加到Activity窗口中的_第1张图片
0BF668A9-A6EB-4B65-BC6B-2FFB2C6AF3A0.png

你可能感兴趣的:(布局是怎么被添加到Activity窗口中的)