您的位置:首页 > 其它

Anroid基础建设之View,Window,Activity

2017-08-07 10:24 211 查看
 

1.PhoneWindow

DecorView存在于PhoneWindow类中, 这两个成员变量是属于包含关系,mDecor包含mContentParent

     private DecorView mDecor;
     private ViewGroup mContentParent;
setContentView 调用 installDecor ,installDecor调用generateDecor 后调用 generateLayout,来产生上面两个变量

   mDecor和Layout,Layout里面包含了mContentParent

  下面的代码证明了系统的默认window的资源文件的名称和位置,可以去framework找到。
// Inflate the window decor.

int layoutResource;
int features = getLocalFeatures();
// System.out.println("Features: 0x" + Integer.toHexString(features));
if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
if (mIsFloating) {
layoutResource = com.android.internal.R.layout.dialog_title_icons;
} else {
layoutResource = com.android.internal.R.layout.screen_title_icons;
}
// System.out.println("Title Icons!");
} else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0) {
// Special case for a window with only a progress bar (and title).
// XXX Need to have a no-title version of embedded windows.
layoutResource = com.android.internal.R.layout.screen_progress;
// System.out.println("Progress!");
} else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) {
// Special case for a window with a custom title.
// If the window is floating, we need a dialog layout
if (mIsFloating) {
layoutResource = com.android.internal.R.layout.dialog_custom_title;
} else {
layoutResource = com.android.internal.R.layout.screen_custom_title;
}
} else if ((features & (1 << FEATURE_NO_TITLE)) == 0) {
// If no other features and not embedded, only need a title.
// If the window is floating, we need a dialog layout
if (mIsFloating) {
layoutResource = com.android.internal.R.layout.dialog_title;
} else {
layoutResource = com.android.internal.R.layout.screen_title;
}
// System.out.println("Title!");
} else {
// Embedded, so no decoration is needed.
layoutResource = com.android.internal.R.layout.screen_simple;
// System.out.println("Simple!");
}

 

 

 

 mDecor除了mContentParent之外,还有以下的View

 

private TextView mTitleView;
private ImageView mLeftIconView;
private ImageView mRightIconView;
private ProgressBar mCircularProgressBar;
private ProgressBar mHorizontalProgressBar;

除了VIew之外,还有一个背景

    private Drawable mBackgroundDrawable;

另外还有这几个特殊的东西

  private DrawableFeatureState[] mDrawables;
    private PanelFeatureState[] mPanels;
    private PanelFeatureState mPreparedPanel;

这几个对应的是各种对话框

    public static final int FEATURE_OPTIONS_PANEL = 0;

    public static final int FEATURE_CONTEXT_MENU = 6;

 

PhoneWindow用自己的话说,到底是什么?

这些所有的东西构成了一个Window。

    现在可以定义一个Window了,Window就是一些View的集合。有tile,有Dialog有contentview,还有两个icon,还有背景,还有两种ProgressBar

 

 

下面的代码在ActivityThread的handleResumeActivity方法中

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);
}

 PhoneWindow的decorView被安装到了WindowManager中。

 

  下面的代码说明了另外一个问题,就是PhoneWindow如何创建的问题Activity的attach方法中

  很搞笑的是Window并不是由WindowManager创建的,而是由PolicyManager创建的。

   

mWindow = PolicyManager.makeNewWindow(this);

 

 下面的代码说明了另外一个,DecorView的创建问题:就是Activity的setContentView方法 ,  

public void setContentView(int layoutResID) {
getWindow().setContentView(layoutResID);
}

 

@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
mContentParent.addView(view, params);
final Callback cb = getCallback();
if (cb != null) {
cb.onContentChanged();
}
}

 

2.WindowManager:

     继承于ViewManager,里面有三个重要的数组:

private View[] mViews;
private ViewRoot[] mRoots;
private WindowManager.LayoutParams[] mParams;

  根据数组下标进行映射,构成了一个 三项MAP。

   mRoots实际上是一个handler,这个handler通过callback给Activity发送键盘和鼠标事件。

 

WindowManagerImpl用自己的话说,到底是什么:

  WindowManagerImpl实际上并没有管理Window,管理的是Window与WindowManagerService的对话,mRoots正是这些对话的一个缩影。从继承关系来看,ViewRoot不是一个View,而是实现了ViewParent的Handler,包含一个Surface来绘图。

 

 

ViewRoot是一个真正的根。

 

1.创建一个Surface

2.提供两个对话IWindow和IWindowSession

3.提供Handler对事件进行处理。

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: