Window, PhoneWindow, DecorView, WindowManager, WindowManagerService的基本概念

Window, PhoneWindow之间的关系

Window是抽象基类, 作为top-level view添加到WindowManager中.
定义了标准 UI policies such as a background, title area, default key processing, etc.

public abstract class Window {
    public abstract int getStatusBarColor();
}

典型应用, 例如设置状态栏的颜色:
CustomTabActivity中设置状态栏的颜色.

Window window = activity.getWindow();
statusBarColor == Color.GREEN;  //设置自己需要的颜色.
window.setStatusBarColor(statusBarColor);

Window唯一的实现类是PhoneWindow

public class PhoneWindow extends Window {
    // This is the top-level view of the window
    private DecorView mDecor;

    @Override
    public void setStatusBarColor(int color) {
        mStatusBarColor = color;
        mForcedStatusBarColor = true;
        if (mDecor != null) {
            mDecor.updateColorViews(null, false /* animate */);
        }
    }
}
WindowManager, WindowManagerService之间的关系

WindowManager 是一个接口, 给app使用, 用来和window manager service进行交互

The interface that apps use to talk to the window manager service.
public interface WindowManager

典型的使用, app获知当前是否为竖屏显示:

    private boolean isOrientationPortrait() {
        Context context = getContext();
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Point outputSize = new Point(0, 0);
        display.getSize(outputSize);
        return outputSize.x <= outputSize.y;
    }

WindowManagerService的作用是: 负责管理各app窗口的创建,更新,删除, 显示顺序.
运行在system_server进程

PhoneWidow, DecorView之间的关系

DecorView是PhoneWindow中的一个成员变量

Activity调用的setContentView(view_id)中到底干了什么.

public class Activity {
    public void setContentView(View view) {
        getWindow().setContentView(view);
        initWindowDecorActionBar();
    }
}
public class PhoneWindow extends Window {
    // This is the top-level view of the window
    private DecorView mDecor;

    @Override
    public void setContentView(View view) {
        mContentParent.addView(view, params);
    }
}

简单理解吧, App代码中调用setContentView(View view), 最终结果就是把自己的view设置给了PhoneWindow中的DecorView对象.

------DONE.-------------------

你可能感兴趣的:(Window, PhoneWindow, DecorView, WindowManager, WindowManagerService的基本概念)