Android利用WindowManager实现悬浮窗

不废话,四步走:

1、首先要声明悬浮窗口权限:


2、引导用户打开悬浮窗权限:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (!Settings.canDrawOverlays(this)) {
        showTip("请先打开悬浮窗权限");
        startActivityForResult(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())), 0);
    } else {
        //有权限了,可以用service或者直接用第三步开启悬浮窗
    }
}

3、初始化悬浮窗:

private void initFloatingWindow() {
    if (Settings.canDrawOverlays(this)) {
        // 获取WindowManager服务
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        //FloatView为自定义的悬浮窗控件,亦可使用Button、ImageView之类的控件
        floatView = new FloatView(getApplicationContext());
        // 设置LayoutParam
        layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        // 实现悬浮窗可以移动的属性(把这个值改成其他值可以操作悬浮窗底下的内容)
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.format = PixelFormat.TRANSPARENT;
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.x = 0;
        layoutParams.y = 0;

        //todo
        //对你的悬浮窗做一些点击或移动事件的处理
        
    }
}

layoutParams.flags的值选择参考:https://www.jianshu.com/p/c91448e1c7d1

 

4、显示和隐藏悬浮窗:

//显示悬浮窗(添加悬浮窗控件)
windowManager.addView(floatView, layoutParams);


//隐藏悬浮窗(移除悬浮窗控件)
windowManager.removeView(floatView);


//更新悬浮窗控件布局(对悬浮窗做修改之后调用)
windowManager.updateViewLayout(floatView, layoutParams);

 

至于悬浮控件的移动,悬浮窗控件设置一个OnTouchListener事件:

floatView.setOnTouchListener(new View.OnTouchListener() {
            private int x;
            private int y;

            @Override
            public boolean onTouch(View view, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        x = (int) event.getRawX();
                        y = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int nowX = (int) event.getRawX();
                        int nowY = (int) event.getRawY();
                        int movedX = nowX - x;
                        int movedY = nowY - y;
                        x = nowX;
                        y = nowY;
                        layoutParams.x = layoutParams.x + movedX;
                        layoutParams.y = layoutParams.y + movedY;

                        // 更新悬浮窗控件布局
                        windowManager.updateViewLayout(floatView, layoutParams);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });

但是这样实现的却是整屏控制悬浮窗的移动,并不能很好的实现在悬浮窗上面拖动的效果,这个我还得再研究研究,也望各位大佬指点指点。

 

完。

 

你可能感兴趣的:(Android)