地址
http://blog.youkuaiyun.com/xiangyong_1521/article/details/78401755
目录
-
关于Window/WindowManagerService的简介
-
ViewManager和WindowManager的实现
-
举例实现
-
附
关于Window/WindowManagerService的简介
-
WindowManagerService
先来看WindowManagerService,从此图可以得知,它是位于Framework层的窗口管理服务,职责就是管理系统中的所有窗口,窗口的本质是什么呢?其实就是一块显示区域,在 Android 中就是绘制的画布:Surface,当一块 Surface 显示在屏幕上时,就是用户所看到的窗口了。WindowManagerService 添加一个窗口的过程,其实就是 WindowManagerService 为其分配一块 Surface 的过程,一块块的 Surface 在 WindowManagerService 的管理下有序的排列在屏幕上
-
Window
Window 是一个抽象类,表示一个窗口,它的具体实现类是 PhoneWindow,实现位于 WindowManagerService 中;Window 有三种类型
应用 Window:应用类 Window 对应一个 Acitivity;
子 Window :子 Window 不能单独存在,需要依附在特定的父 Window 中,比如常见的一些 Dialog 就是一个子 Window;
系统 Window:系统 Window是需要声明权限才能创建的 Window,比如 Toast 和系统状态栏都是系统 Window。
Window 是分层的,每个 Window 都有对应的 z-ordered(处在这些叠加窗口中的位置),层级大的会覆盖在层级小的 Window 上面,这和 HTML 中的 z-index 概念是完全一致的。在三种 Window 中,应用 Window 层级范围是 1~99,子 Window 层级范围是 1000~1999,系统 Window 层级范围是 2000~2999,我们可以用一个表格来直观的表示
Window 层级 应用 1-99 子 1000-1999 系统 2000-299 这些层级是对应的WindowManager.LayoutParams 的 type 参数,如下图:
ViewManager和WindowManager的实现
-
ViewManager
我们对 Window 的操作是通过 WindowManager 来完成的,WindowManager继承于 ViewManagerWindowManager 是一个接口,它继承自只有三个方法的 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);
}
三个方法其实就是 WindowManager 对外提供的主要功能,即添加 View、更新 View 和删除 View
-
WindowManager实现
public void show(){
if (mContentView != null) {
wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
wmParams.format = PixelFormat.RGBA_8888;
wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
wmParams.alpha = 1.0F;
wmParams.gravity = Gravity.TOP | Gravity.LEFT;
wmParams.x = 0;
wmParams.y = 0;
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
wm.addView(mContentView, wmParams);
bShow=true;
}
}
权限:<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
举例实现
-
service
/*
* ervice中随时监听限制条件的变化以调动悬浮开关
*/
private Timer mTimer = null;
private TimerTask mTimerTask = null;
private void upTime() {//开启一个线程,在service中随时监听限制条件的变化
this.mTimerTask=new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
if () {//限制条件
MyWindowManager.createwinm(getApplicationContext());//开启窗口
}
}
});
}else
if () { //限制条件
MyWindowManager.removewinm(getApplicationContext());//关闭窗口
}
};
this.mTimer.schedule(mTimerTask, 300, 300);
}
-
WindowManager
/*
* 定义一个实现WindowManager的类,所有需要悬浮的界面可以在此类中定义windowManager
*/
public class MyWindowManager {
public static CarDoor carDoor;
public static LayoutParams doorParams;
public static void createwinm(Context context) {
WindowManager windowManager = getWindowManager(context);
if (carDoor == null) {
carDoor = new CarDoor(context);
if (doorParams == null) {
doorParams = new LayoutParams();
doorParams.type = LayoutParams.TYPE_PHONE;
doorParams.format = PixelFormat.RGBA_8888;
doorParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
doorParams.gravity = Gravity.CENTER;
doorParams.gravity = Gravity.LEFT | Gravity.BOTTOM;
doorParams.width = carDoor.viewWidth;
doorParams.height = carDoor.viewHight;
doorParams.x = 0;
doorParams.y = 0;
}
carDoor.setParams(doorParams);
windowManager.addView(carDoor, doorParams);
carDoor.updateViewPosition();
}
}
public static void removewinm(Context context) {
if (carDoor != null) {
carDoor.mTimerTask.cancel();
WindowManager windowManager = getWindowManager(context);
windowManager.removeView(carDoor);
carDoor = null;
}
}
}
-
CarDoor
/*
* 悬浮界面要要实现什么功能在此类定义就好了;
*/
public class CarDoor extends LinearLayout {
public CarDoor(Context context) {
super(context);
windowManager=(WindowManager) context.getSystemService(context.WINDOW_SERVICE);
LayoutInflater.from(context).inflate(R.layout.layout_cardoor,this);
View view=findViewById(R.id.layout_cardoor1);
viewWidth=view.getLayoutParams().width;
viewHight=view.getLayoutParams().height;
initView(view);
initData();
test1();
}
}
附
笔记22 | 学习整理开源APP(BaseAnimation)程序源码“中的通讯录效果(三)
结束