Android对Window对象的管理机制分析


转载请注明出处:http://blog.csdn.net/crazy1235/article/details/72625679


关于Window对象管理的相关类:


  • ViewManager

  • WindowManager

  • WindowManagerImpl

  • WindowManagerGlobal

  • ViewParent

  • ViewRootImpl

  • ActvityThread

  • WindowManagerService


Window是一个抽象类,具体实现类就是 PhoneWindow

一个Window是通过一个View来承载的,还对应一个ViewRootImpl!

访问Window必须通过WindowManager来实现!

Window具体操作方法实现实在WindowManagerImpl类中,方法定义在ViewManager接口中!

public interface ViewManager
{    
    public void addView(View view, ViewGroup.LayoutParams params);
    public void updateViewLayout(View view, ViewGroup.LayoutParams params);
    public void removeView(View view);
}

ViewManager接口定义了三个函数,分别对应window的添加,更新和删除!

这里写图片描述


addView

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

其实WindowManagerImpl也没有做具体的操作,而是交给了 WindowManagerGlobal

在WindowManagerImpl类中进行了初始化

private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();

WindowManagerGlobal.java

private WindowManagerGlobal() {
    }

    public static WindowManagerGlobal getInstance() {
        synchronized (WindowManagerGlobal.class) {
            if (sDefaultWindowManager == null) {
                sDefaultWindowManager = new WindowManagerGlobal();
            }
            return sDefaultWindowManager;
        }
    }

很明显看出这是个单例模式


private final ArrayList<View> mViews = new ArrayList<View>();
private final ArrayList<ViewRootImpl> mRoots = new ArrayList<ViewRootImpl>();
private final ArrayList<WindowManager.LayoutParams> mParams =
            new ArrayList<WindowManager.LayoutParams>();
private final ArraySet<View> mDyingViews = new ArraySet<View>();

WindowManagerGlobal中有四个重要的集合!

  • mViews集合存储了所有window对应的View对象!

  • mRoots集合存储了所有window对应的ViewRootImpl对象

  • mParams集合存储了所有window对应的布局参数

  • mDyingViews集合存储了所有正在被删除的window对象


public void addView(View view, ViewGroup.LayoutParams params,
            Display display, Window parentWindow) {
        // 1. 验证参数!
        if (view == null) {
            throw new IllegalArgumentException("
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值