android界面编程方面有两大核心,一个是View代表的界面元素,一个是WindowManager代表的界面管理.前者为卒子,后者为将帅.
基础
一切view,在界面最终以Window形式展现,被WindowManager管理.
WindowManager提供有三个方法:添加View、更新View和删除View.
public void addView(View view, ViewGroup.LayoutParams params);
public void updateViewLayout(View view, ViewGroup.LayoutParams params);
public void removeView(View view);
View的展现规则封装在LayoutParams中,其内部属性见:
LayoutParams 主要的几个属性
flag
标识这个window怎么响应事件,怎样的一个透明度,以及一些全屏,锁屏显示等等.
WindowManager.LayoutParams的各种flag含义
type
类似于前端里的z-index.值越大越在上面.
type的真正含义: 类型-->根据值的范围将window分成三大类:
- 系统级窗口(System windows):
ranging from FIRST_SYSTEM_WINDOW to LAST_SYSTEM_WINDOW 2000-2999
可以在任何地方显示 - 应用级窗口(Application windows):
ranging from FIRST_APPLICATION_WINDOW to LAST_APPLICATION_WINDOW 1-99
典型的: activity - 子窗口(Sub-windows):
ranging from FIRST_SUB_WINDOW to LAST_SUB_WINDOW 1000-1999
必须依附于某一个应用级窗口
WindowManager.LayoutParams.type的使用
WindowManager.LayoutParams.type属性
系统内置的窗口实现
- popupwindow
- dialog
- activity
- toast
- notification
系统级窗口,6.0以前的权限
一般来说,弹出系统级窗口需要申请android.permission.SYSTEM_ALERT_WINDOW权限,
但是,TYPE_TOAST虽然是系统级窗口,不用申请
然而,国产rom,比如MIUI,会强制要求申请此权限,更闹心的是,还默认关闭这个权限.
所以,需要兼顾6.0以前的权限申请,以及适配:
https://github.com/hss01248/FloatWindowPermission
奇技淫巧
需求1
在屏幕上显示一个浮动控件。需要能接收点击事件,还要能显示在statusBar(状态栏)之上,不能被状态栏遮住
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
需求2
偷偷地拍照:
- 思路1:弹出一个透明的activity,这个activity还要能够不拦截任何事件,屏幕的所有触摸都要能够传递到下层activity.
拦路虎: 对activity的许多flag设置会无效,主要是activity无法设置不拦截事件. - 思路2: 弹出一个type = TYPE_TOAST的窗口,1平方像素.
代码库见此: https://github.com/hss01248/HiddenCamera
核心代码:
final WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
PhotoCallback callback2 = new PhotoCallback() {
@Override
public void onFail() {
callback.onFail();
windowManager.removeView(page.getRootView());
}
@Override
public void onSuccess(String path) {
callback.onSuccess(path);
windowManager.removeView(page.getRootView());
}
};
page.setCallback(callback2);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();//dialog.getWindow().getAttributes();
params.type = WindowManager.LayoutParams.TYPE_TOAST;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
|WindowManager.LayoutParams.FLAG_DIM_BEHIND//后面变暗
|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.LEFT| Gravity.TOP;
params.dimAmount = 0;//后面变暗区域透明...
windowManager.addView(page.getRootView(),params);