觉得魅族的悬浮球做的很不错,主要是简洁方便。于是就自己实现了一下。项目源代码:FloatingBall源代码
主要是解决三个方面的问题:
1.桌面悬浮窗的实现
2.设置悬浮球触摸事件(拖动,点击,上下左右滑动)
2.模拟虚拟按键(返回,home,任务面板,电源键,菜单键等)
(一)桌面悬浮窗的实现
通过WindowManager将自定义的悬浮球布局ballview添加到手机屏幕
//布局转化成要显示的view
ballView = LayoutInflater.from(this).inflate(R.layout.floatball, null);
wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
ballWmParams = new WindowManager.LayoutParams();
//设置悬浮窗的相关参数
ballWmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
ballWmParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
ballWmParams.gravity = Gravity.LEFT | Gravity.TOP;
ballWmParams.x = MainActivity.sp.getInt("ballWmParamsX",0);
ballWmParams.y = MainActivity.sp.getInt("ballWmParamsY",0);
ballWmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
ballWmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
ballWmParams.format = PixelFormat.RGBA_8888;
//将悬浮球view添加到屏幕