Android window type图层分析

目录

  • 1,Window 的分类
  • 2, 自定义window图层

1,Window 的分类

按照使用场景的划分,window有三种类型:
1.1,Application Window:
应用程序串口,activity界面就属于这一级;
1.2,Sub Window:
子窗口,顾名思义,对应有主窗口。子窗口需要附在主窗口上,如 PopWindow;
1.3,System Window:
系统级的窗口,层级一般较高,如Toast弹框,输入法弹框等;

1.1 Application Window

用于应用级窗口,其type 取值范围 [1,99],实际只定义使用了4个值:
TYPE_BASE_APPLICATION:
=1,base窗口,应用的最基本窗口
TYPE_APPLICATION:
=2,正常的稳定态应用窗口
TYPE_APPLICATION_STARTING:
=3,应用启动过程中的一个过程态窗口
TYPE_DRAWN_APPLICATION:
=4,确保窗口管理器在显示应用程序之前等待绘制该窗口

/**
         * Window type: an application window that serves as the "base" window
         * of the overall application; all other application windows will
         * appear on top of it.
         * In multiuser systems shows only on the owning user's window.
         */
        public static final int TYPE_BASE_APPLICATION   = 1;

        /**
         * Window type: a normal application window.  The {@link #token} must be
         * an Activity token identifying who the window belongs to.
         * In multiuser systems shows only on the owning user's window.
         */
        public static final int TYPE_APPLICATION        = 2;

        /**
         * Window type: special application window that is displayed while the
         * application is starting.  Not for use by applications themselves;
         * this is used by the system to display something until the
         * application can show its own windows.
         * In multiuser systems shows on all users' windows.
         */
        public static final int TYPE_APPLICATION_STARTING = 3;

        /**
         * Window type: a variation on TYPE_APPLICATION that ensures the window
         * manager will wait for this window to be drawn before the app is shown.
         * In multiuser systems shows only on the owning user's window.
         */
        public static final int TYPE_DRAWN_APPLICATION = 4;

1.2,Sub Window

顾名思义,对应有主窗口。子窗口需要依附在主窗口上,如果主窗口消失,子窗口也会消失,如 PopWindow;
type 取值范围 [1000,1999],实际只使用了5个值:

/**
         * 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;

        /**
         * Window type: a panel on top of an application window.  These windows
         * appear on top of their attached window.
         */
        public static final int TYPE_APPLICATION_PANEL = FIRST_SUB_WINDOW;

        /**
         * Window type: window for showing media (such as video).  These windows
         * are displayed behind their attached window.
         */
        public static final int TYPE_APPLICATION_MEDIA = FIRST_SUB_WINDOW + 1;

        /**
         * Window type: a sub-panel on top of an application window.  These
         * windows are displayed on top their attached window and any
         * {@link #TYPE_APPLICATION_PANEL} panels.
         */
        public static final int TYPE_APPLICATION_SUB_PANEL = FIRST_SUB_WINDOW + 2;

        /** Window type: like {@link #TYPE_APPLICATION_PANEL}, but layout
         * of the window happens as that of a top-level window, not
         * as a child of its container.
         */
        public static final int TYPE_APPLICATION_ATTACHED_DIALOG = FIRST_SUB_WINDOW + 3;

        /**
         * Window type: window for showing overlays on top of media windows.
         * These windows are displayed between TYPE_APPLICATION_MEDIA and the
         * application window.  They should be translucent to be useful.  This
         * is a big ugly hack so:
         * @hide
         */
        @UnsupportedAppUsage
        public static final int TYPE_APPLICATION_MEDIA_OVERLAY  = FIRST_SUB_WINDOW + 4;

        /**
         * Window type: a above sub-panel on top of an application window and it's
         * sub-panel windows. These windows are displayed on top of their attached window
         * and any {@link #TYPE_APPLICATION_SUB_PANEL} panels.
         * @hide
         */
        public static final int TYPE_APPLICATION_ABOVE_SUB_PANEL = FIRST_SUB_WINDOW + 5;

        /**
         * End of types of sub-windows.
         */
        public static final int LAST_SUB_WINDOW = 1999;

1.3,System Window

系统级的弹框,不依赖于某个界面,但很多图层需要系统权限;
type 取值范围 [2000,2999],现在原生图层已定义50个左右,包括导航栏、状态栏、键盘、系统报警等图层。

/**
         * Start of system-specific window types.  These are not normally
         * created by applications.
         */
        public static final int FIRST_SYSTEM_WINDOW     = 2000;
        /**
         * Window type: the status bar.  There can be only one status bar
         * window; it is placed at the top of the screen, and all other
         * windows are shifted down so they are below it.
         * In multiuser systems shows on all users' windows.
         */
        public static final int TYPE_STATUS_BAR         = FIRST_SYSTEM_WINDOW;
        /**
         * Window type: the search bar.  There can be only one search bar
         * window; it is placed at the top of the screen.
         * In multiuser systems shows on all users' windows.
         */
        public static final int TYPE_SEARCH_BAR         = FIRST_SYSTEM_WINDOW+1;
        /**
         * Window type: phone.  These are non-application windows providing
         * user interaction with the phone (in particular incoming calls).
         * These windows are normally placed above all applications, but behind
         * the status bar.
         * In multiuser systems shows on all users' windows.
         * @deprecated for non-system apps. Use {@link #TYPE_APPLICATION_OVERLAY} instead.
         */
        @Deprecated
        public static final int TYPE_PHONE              = FIRST_SYSTEM_WINDOW+2;
        /**
         * Window type: system window, such as low power alert. These windows
         * are always on top of application windows.
         * In multiuser systems shows only on the owning user's window.
         * @deprecated for non-system apps. Use {@link #TYPE_APPLICATION_OVERLAY} instead.
         */
        @Deprecated
        public static final int TYPE_SYSTEM_ALERT       = FIRST_SYSTEM_WINDOW+3;
... ...
        /**
         * Window type: wallpaper window, placed behind any window that wants
         */
        public static final int TYPE_VENDOR_WALLPAPER   = FIRST_SYSTEM_WINDOW+54;
        /**
         * Window type: status bar for U6X VirtualDisplay .same as TYPE_STATUS_BAR
         */
        public static final int TYPE_VENDOR_STATUS_BAR  = FIRST_SYSTEM_WINDOW + 55;
        /**
         * End of types of system windows.
         */
        public static final int LAST_SYSTEM_WINDOW      = 2999;

2 自定义window图层

Google提供的原生图层有限,无法满足所有的使用场景。特别是车载项目上,窗口的使用场景甚至比手机上还要多很多。所以需要根据项目需要,定制新图层,或者修改图层之间的关系;过程分两步:

2.1 声明新图层

WindowManager.java中定义,可以直接被app层使用,注意新定义的值不重复即可,一般自定义的图层需要TYPE_VENDOR_开头;不是定义的值越大,图层层级越高!

        /**
         * Window type: Banner info app
         */
        public static final int TYPE_VENDOR_BANNER_CRITICAL = FIRST_SYSTEM_WINDOW+44;

        /**
         * Window type: VPA
         */
        public static final int TYPE_VENDOR_VPA_OVERLAY = FIRST_SYSTEM_WINDOW+45;
        /**
         * Window type: Toast+
         */
        public static final int TYPE_VENDOR_TOAST_PLUS = FIRST_SYSTEM_WINDOW+46;

        /**
         * Window type:Pano Card used for CDX707
         */
        public static final int TYPE_VENDOR_CARD = FIRST_SYSTEM_WINDOW+47;
        /**
         * Window type:seconed navigationbar window type
         */
        public static final int TYPE_VENDOR_NAVIGATION_BAR = FIRST_SYSTEM_WINDOW+48;

2.2 调整图层的层级关系

windowManagerPolicy中的getWindowLayerFromTypeLw()方法中声明了各层级的关系,返回的值越大,层级越高;开发者可以根据项目的需求,在这个方法里自行调整window的层级;

default int getWindowLayerFromTypeLw(int type, boolean canAddInternalSystemWindow) {
        if (type >= FIRST_APPLICATION_WINDOW && type <= LAST_APPLICATION_WINDOW) {
            return APPLICATION_LAYER;
        }
        switch (type) {
            case TYPE_WALLPAPER:
            case TYPE_VENDOR_WALLPAPER:
                // wallpaper is at the bottom, though the window manager may move it.
                return  1;
            case TYPE_PRESENTATION:
            case TYPE_PRIVATE_PRESENTATION:
                return  APPLICATION_LAYER;
            case TYPE_DOCK_DIVIDER:
                return  APPLICATION_LAYER;
            case TYPE_QS_DIALOG:
                return  APPLICATION_LAYER;
            case TYPE_VENDOR_SURPRISE_MESSAGE:
                return  3;
            case TYPE_VENDOR_CARD:
                return  4;
            case TYPE_STATUS_BAR:
            case TYPE_VENDOR_STATUS_BAR:
                return  5;
            case TYPE_STATUS_BAR_ADDITIONAL:
                return  6;
            ... ...

你可能感兴趣的:(Android图形显示子系统,android,adb)