Android WindowManager机制的学习

1.引言

学习View这章节,涉及到FramWork的WindowManagerService和ActivityManagerService。其实之前都学习过,不过在此写一遍,增加印象。

2.正题

Window 有三种类型,分别是应用 Window、子 Window 和系统 Window。应用类 Window 对应一个 Acitivity,子 Window 不能单独存在,需要依附在特定的父 Window 中,比如常见的一些 Dialog 就是一个子 Window。系统 Window是需要声明权限才能创建的 Window,比如 Toast 和系统状态栏都是系统 Window。

Window 是分层的,每个 Window 都有对应的 z-ordered,层级大的会覆盖在层级小的 Window 上面,这和 HTML 中的 z-index 概念是完全一致的。在三种 Window 中,应用 Window 层级范围是 1~99,子 Window 层级范围是 1000~1999,系统 Window 层级范围是 2000~2999,我们可以用一个表格来直观的表示:

Window 层级
应用层级 1~99
子Window层级 1000~1999
系统 Window 2000~2999

WindowManagerService的作用:
对Window进行管理,例如Window上的view的增删改查全都都需要通过WindowManagerService来做处理。

2.1 WindowManager

WindowManager是对Window操作的接口,对Window一系列的操作都是通过WindowManager操作的。下面是Window机制的UML图解:


image.png
  • ViewRootImpl中的requestLayout() 方法中调用了performMeasure方法对View进行测量,performLayout进行布局,performDraw进行绘制。

  • WindowManagerGlobal通过getWindowSession() 获取WindowManagerService的IBinder接口。也就是返回的IWindowSession。通过调用addToDisplay 方法将mView添加到mWindow当中。

 try {
                    mOrigWindowType = mWindowAttributes.type;
                    mAttachInfo.mRecomputeGlobalAttributes = true;
                    collectViewAttributes();
                    res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                            getHostVisibility(), mDisplay.getDisplayId(),
                            mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                            mAttachInfo.mOutsets, mInputChannel);
                } catch (RemoteException e) {
                    mAdded = false;
                    mView = null;
                    mAttachInfo.mRootView = null;
                    mInputChannel = null;
                    mFallbackEventHandler.setView(null);
                    unscheduleTraversals();
                    setAccessibilityFocus(null, null);
                    throw new RuntimeException("Adding window failed", e);
                } finally {
                    if (restore) {
                        attrs.restore();
                    }
                }

把UML图搞懂了。那么整个Window流程基本上就差不多了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容