window的工作流程

435 阅读2分钟

「这是我参与2022首次更文挑战的第21天,活动详情查看:2022首次更文挑战」。

window的工作流程

之前在事件分发中说过activity会将事件传递给window,window对view进行管理,但window并不是一个在屏幕上显示的实体,window是视图的管理者,对于每一个视图都有一个对应的window

WindowManager

/**
 * The interface that apps use to talk to the window manager.
 * </p><p>
...
 * </p>
 */

在源代码关于WindowManager上提到,WindowManager是一个提供window管理的一个接口,如果我们想要得到一个WindowManager的实例可以通过一些带视图的东西,比如activity,Toast等。 由此可以得知WindowManager管理window,window又管理view.WindowManager的具体实现是WindowManagerImpl,在WindowManagerImpl中会实现Window对view的以下几个操作

  • addview
  • removeView
  • updateViewLayout

addview

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

这里发现WindowManagerImpl实际上是mGlobal(WindowManager类型)的代理,mGlobal的addView方法如下

public void addView(View view, ViewGroup.LayoutParams params,
        Display display, Window parentWindow, int userId) {
    if (view == null) {
        throw new IllegalArgumentException("view must not be null");
    }
    if (display == null) {
        throw new IllegalArgumentException("display must not be null");
    }
    if (!(params instanceof WindowManager.LayoutParams)) {
        throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
    }

...

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

        view.setLayoutParams(wparams);

        mViews.add(view);
        mRoots.add(root);
        mParams.add(wparams);

        // do this last because it fires off messages to start doing things
        try {
            root.setView(view, wparams, panelParentView, userId);
        } catch (RuntimeException e) {
            // BadTokenException or InvalidDisplayException, clean up.
            if (index >= 0) {
                removeViewLocked(index, true);
            }
            throw e;
        }
    }
}

这个过程比较长,这里省略了一些东西,总的来说作用一是检查参数,二是往mGloal这个单例中添加元素,这些元素主要有 view,root,params,分别代表window的对应的view和ViewRootImpl以及布局参数params,接着调用了root的setView方法,在这个方法中会实现window的添加

res = mWindowSession.addToDisplayAsUser(mWindow, mSeq, mWindowAttributes,
        getHostVisibility(), mDisplay.getDisplayId(), userId, mTmpFrame,
        mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
        mAttachInfo.mDisplayCutout, inputChannel,
        mTempInsets, mTempControls);

在setView中有这么一段,我们观察mWindowSession的类型

final IWindowSession mWindowSession;

发现这是一个IBinder,这个window的添加过程最终会让WMS去进行添加

removeView

public void removeView(View view, boolean immediate) {
    if (view == null) {
        throw new IllegalArgumentException("view must not be null");
    }

    synchronized (mLock) {
        int index = findViewLocked(view, true);
        View curView = mRoots.get(index).getView();
        removeViewLocked(index, immediate);
        if (curView == view) {
            return;
        }

        throw new IllegalStateException("Calling with view " + view
                + " but the ViewAncestor is attached to " + curView);
    }
}

在这个过程中先找到view的位置,然后调用removeViewLocked进行删除

updateViewLayout

public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
    if (view == null) {
        throw new IllegalArgumentException("view must not be null");
    }
    if (!(params instanceof WindowManager.LayoutParams)) {
        throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
    }

    final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams)params;

    view.setLayoutParams(wparams);

    synchronized (mLock) {
        int index = findViewLocked(view, true);
        ViewRootImpl root = mRoots.get(index);
        mParams.remove(index);
        mParams.add(index, wparams);
        root.setLayoutParams(wparams, false);
    }
}

这个过程的关键是最后的root.setLayoutParams,在这个过程中会调用scheduleTraversal方法对view重建(也就是测量,布局,绘制三个工作过程)

void setLayoutParams(WindowManager.LayoutParams attrs, boolean newView) {
...
        scheduleTraversals();
   
}