前言:有时候会好奇,打开一个应用你所看到的屏幕上每一个界面时怎么显现出来的。
下面就会在这个问题进行剖析之前,做一些知识储备。
1.在完成这个问题之前需要熟悉一下几个知识点。
-
Window
-
WindowManager
-
WindowManagerService
-
DecorView
-
PhoneWindow
-
RootViewimpl
-
ViewManager
当看到这些的时候,会想这些是什么玩意全是什么什么window,下面就会对这些进行阐释。对这些知识点熟悉后,就能很容易的理解android的Activity是怎么显示和加载的了。
2.所分析的源码是基于Android6.0
Window:就如英文所表示的意思,它在Android中表示一个窗口并且它是一个用来装View的抽象概念。它可以提供一些背景,标题等功能。几乎所有的View都是附属在Window上面的(PopWindow就没有所依附的Window,它是直接浮在Window上面的一个容器)。
WindowManager:在Window中有个setWindowManager方法,该方法是为window添加一个window管理器,一个Window对应于一个WindowManager,它继承与ViewManager接口,接口中定义了三个方法add, updateViewLayout, removeView.所以WindowManger想对Window中的View进行操作的话,是必须利用WindowManager的
WindowManagerService:它是由SystemServer孵化出来的一个Service.它继承于IWindowManager.Stub,马上反应出就知道它需要进程间通信的。它通信的主要目标就还是WindowManager。所以它的作用就是对屏幕里面的窗口进行一一管理。
PhoneWindow:它是Window的唯一实现类。它里面包含了一个最顶层的DecorView。大家使用的View都是存在于DecorView下面的。PhoneWindow里面继承了一个Callback接口,该接口里面包含了大量事件处理方法。分析的点击事件,就是对这些方法进行分析的。
RootViewImpl:View的测量,布局,绘制就是在它的performTraversals方法里开始的。
以上只是对一些重要的知识点进行一个简单阐释,下面将会从代码来阅读每个关键点,最后再给出Android是怎么加载并显示一个Activity的。
3.源码阅读
Window:就如英文所表示的意思,它在Android中表示一个窗口并且它是一个用来装View的抽象概念。它可以提供一些背景,标题等功能。几乎所有的View都是附属在Window上面的(PopWindow就没有所依附的Window,它是直接浮在Window上面的一个容器)。
public void setWindowManager(WindowManager wm, IBinder appToken, String appName) {
setWindowManager(wm, appToken, appName, false);
}
/**
* Set the window manager for use by this Window to, for example,
* display panels. This is not used for displaying the
* Window itself -- that must be done by the client.
*
* @param wm The window manager for adding new windows.
*/
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);
}
虽然有两个setWindowManager 方法来为Window添加WindowManager,但可以直接看后者。这里可以看到WindowManager的是一个实现类是WindowManagerImpl.从这里就可以进入到WindowManager去看看它的代码了。
WindowManager是一个接口继承于ViewManager接口。在ViewManager中有三个方法分别是
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
这三个方法就说明了前面为什么说WindowManager是外界对Window访问的接口了,你想操作Window中View就需要通过WindowManager来进行添加,更新好布局参数,移除Window中的View。
WindowManager中LayoutParams对ViewGroup的LayoutParams进行了一个补充给对应的Window添加Type参数表示Window的类型,有三种类型,分别是应用Window,子Window和系统Window,应用类Window对应一个Activity,子Window不能单独存在,它需要附属在特定的父Window之中,比如常见的Dialog就是一个子Window,系统Window是需要声明权限在能创建的Window,比如Toast和系统状态栏这些都是系统Window。
对Type类型的定义截取一段:
/**
* Start of window types that represent normal application windows.
*/
public static final int FIRST_APPLICATION_WINDOW = 1;
/**
* End of types of application windows.
*/
public static final int LAST_APPLICATION_WINDOW = 99;
这两个说明了应用层Window数值大小是1至99
/**
* Start of types of sub-windows. The {@link #token} of these windows
* must be set to the window they are attached to. These types of
* windows are kept next to their attached window in Z-order, and their
* coordinate space is relative to their attached window.
*/
public static final int FIRST_SUB_WINDOW = 1000;
/**
* End of types of sub-windows.
*/
public static final int LAST_SUB_WINDOW = 1999;
这两个说明了子Window数值大小是1000至1999
/**
* Start of system-specific window types. These are not normally
* created by applications.
*/
public static final int FIRST_SYSTEM_WINDOW = 2000;
/**
* End of types of system windows.
*/
public static final int LAST_SYSTEM_WINDOW = 2999;
这两个说明了系统Window数值大小是 2000至2999
通过Type的设置可以改变Window所处层级,也就指定了View显示的优先级。
WindowManager的实现类是WindowManagerImpl,在它里面主要是去看对View操作的三个方法:add update remove
@Override'
public void addView(@NonNull View view, @NonNull ViewGroup.LayoutParams params) {
android.util.SeempLog.record_vg_layout(383,params);
applyDefaultToken(params);
mGlobal.addView(view, params, mDisplay, mParentWindow);
}
WindowManagerimpl的add方法实际上是调用了WindowManagerGlobal的add方法,方法如下:
public void addView(View view, ViewGroup.LayoutParams params,
Display display, Window parentWindow) {
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");
}
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
if (parentWindow != null) {
parentWindow.adjustLayoutParamsForSubWindow(wparams);
} else {
// If there's no parent, then hardware acceleration for this view is
// set from the application's hardware acceleration setting.
final Context context = view.getContext();
if (context != null
&& (context.getApplicationInfo().flags
& ApplicationInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
wparams.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
}
}
ViewRootImpl root;
View panelParentView = null;
synchronized (mLock) {
// Start watching for system property changes.
if (mSystemPropertyUpdater == null) {
mSystemPropertyUpdater = new Runnable() {
@Override public void run() {
synchronized (mLock) {
for (int i = mRoots.size() - 1; i >= 0; --i) {
mRoots.get(i).loadSystemProperties();
}
}
}
};
SystemProperties.addChangeCallback(mSystemPropertyUpdater);
}
int index = findViewLocked(view, false);
if (index >= 0) {
if (mDyingViews.contains(view)) {
// Don't wait for MSG_DIE to make it's way through root's queue.
mRoots.get(index).doDie();
} else {
throw new IllegalStateException("View " + view
+ " has already been added to the window manager.");
}
// The previous removeView() had not completed executing. Now it has.
}
// If this is a panel window, then find the window it is being
// attached to for future reference.
if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW &&
wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
final int count = mViews.size();
for (int i = 0; i < count; i++) {
if (mRoots.get(i).mWindow.asBinder() == wparams.token) {
panelParentView = mViews.get(i);
}
}
}
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);
} catch (RuntimeException e) {
// BadTokenException or InvalidDisplayException, clean up.
synchronized (mLock) {
final int index = findViewLocked(view, false);
if (index >= 0) {
removeViewLocked(index, true);
}
}
throw e;
}
}
从这个方法中可以提炼出几个关键点,
1.ViewRootImpl 在它里面开始对Window所加载的View进行绘制
2.mView mRoots mParms为数组,它们之间形成一一对应的关系
3.display: 提供有关逻辑显示器大小和密度的信息
对ViewRootImpl放到以后的文章中进行详解,它对于理解自定义View有很大的帮助。
PhoneWindow:大部分View都有它本身所依附的Window,而PhoneWindow是Window的唯一实现类,所以可以直接说大部分View都是依附在PhoneWindow上。
PhoneWinodw中有个关键的DecotView变量。
// This is the top-level view of the window, containing the window decor.
private DecorView mDecor;
从它的注释就可以知道,它是界面显示View中的顶层View,而我们所操作看到和操作的View都是它的子View。DecorView本身是继承于FrameLayout。它内部有两个变量
// View added at runtime to draw under the status bar area
private View mStatusGuard;
// View added at runtime to draw under the navigation bar area
private View mNavigationGuard;
注释说明它们两个在DecorView的顶部和底部的指示栏,中间就由ViewGroup呈现的Content区域。
所以Activit,PhoneWindow,DecorView,View的关系可以用如下图来说明:
2017021702426993.jpg
网友评论