废话不多说直接上代码:首先判断是不是显示在桌面上,是显示在桌面上,浮动窗没有移除,则更新他在内存使用百分比,
// 创建一个浮动窗体
WindowManager windowManager = getWindowManager(context);
if (smallWindow == null) {
smallWindow = new FloatWindowSmallView(context);
if (smallWindowParams == null) {
smallWindowParams = new LayoutParams();
smallWindowParams.type = LayoutParams.TYPE_PHONE;
smallWindowParams.format = PixelFormat.RGBA_8888;
smallWindowParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL
| LayoutParams.FLAG_NOT_FOCUSABLE;
smallWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
smallWindowParams.width = FloatWindowSmallView.viewWidth;
smallWindowParams.height = FloatWindowSmallView.viewHeight;
smallWindowParams.x = ScreenUtils.getScreenW();
smallWindowParams.y = ScreenUtils.getScreenH() / 2;
}
smallWindow.setParams(smallWindowParams);
windowManager.addView(smallWindow, smallWindowParams);
}
// 创建好了一个浮动窗体,那个浮动窗体总要移动的,所以要重写onTouchEvent方法。
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// 手指按下时记录必要数据,纵坐标的值都需要减去状态栏高度
xInView = event.getX();
yInView = event.getY();
xDownInScreen = event.getRawX();
yDownInScreen = event.getRawY() - ScreenUtils.getStatusBarHeight();
xInScreen = event.getRawX();
yInScreen = event.getRawY() - ScreenUtils.getStatusBarHeight();
break;
case MotionEvent.ACTION_MOVE:
xInScreen = event.getRawX();
yInScreen = event.getRawY() - ScreenUtils.getStatusBarHeight();
// 手指移动的时候更新小悬浮窗的位置
updateViewPosition();
//移动小悬浮框时,显示删除框
if(!MyWindowManager.isDeleteWindowShowing()){
MyWindowManager.createDeleteWindow(getContext());
}
if(isInDeleteArea(xInScreen, yInScreen)){
MyWindowManager.setDeleteTextColor(Color.BLACK);
}else{
MyWindowManager.setDeleteTextColor(Color.GRAY);
}
break;
case MotionEvent.ACTION_UP:
// 如果手指离开屏幕时,xDownInScreen和xInScreen相等,且yDownInScreen和yInScreen相等,则视为触发了单击事件。
if (xDownInScreen == xInScreen && yDownInScreen == yInScreen) {
openBigWindow();
}
//移除删除框
MyWindowManager.removeDeleteWindow(getContext());
//用户将小图标拖动到了删除框,则移除所有悬浮窗,并停止Service
if(isInDeleteArea(xInScreen, yInScreen)){
MyWindowManager.removeBigWindow(getContext());
MyWindowManager.removeSmallWindow(getContext());
Intent intent = new Intent(getContext(), FloatWindowService.class);
getContext().stopService(intent);
}
break;
default:
break;
}
return true;