1.Window与WindowManager基本概念
- Window、PhoneWindow
顶层窗口样式和行为的抽象类,概括了Android窗口的基本属性和功能。该类实例的getDecorView()方法返回的DecorView被用于作为顶层视图添加到WM中;
创建时机:ActivityThread.handleLaunchActivity--->ActivityThread.performLaunchActivity--->Activity.attach
- WindowManager,WindowMangerImpl,WindowMangerGlobal
WindowManger与一个特定的Display相关联,Display类提供关于屏幕尺寸和分辨率的信息;
- WindowManagerService
对系统中的所有窗口进行管理,WindowManger是运行在Application process中的,WindowMangerService是在system_server进行中运行,两者的通信是通过中间的会话层IWindowSession来进行的;
2.添加View到WindowManager过程
final WindowManager wM = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
wM.getCurrentWindowMetrics().getBounds().width(), wM.getCurrentWindowMetrics().getBounds().height()
);
layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.format = PixelFormat.TRANSLUCENT;
wM.addView(maskView, lp);
相关类的简化结构
public class PhoneWindow extends Window implements MenuBuilder.Callback { //窗口顶层的视图 private DecorView mDecor; } //添加和移除到Activity上视图的接口,通过Context.getSystemService()获取实例 public interface ViewManager { /** * @param view 被添加到Window的视图 * @param params 添加View的布局参数 */ public void addView(View view, ViewGroup.LayoutParams params); public void updateViewLayout(View view, ViewGroup.LayoutParams params); public void removeView(View view); } @SystemService(Context.WINDOW_SERVICE) public interface WindowManager extends ViewManager{ public static class LayoutParams extends ViewGroup.LayoutParams implements Parcelable { //添加的View在window上x坐标 public int x; //添加的View在window上y坐标 public int y; //添加视图所属窗口类型,应用窗口(1-99),子窗