WindowManager使用-------实现悬浮框移动效果

一、WindowManager实现悬浮框效果流程
1、新建一个布局文件layout_window.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="200dp"
        android:orientation="vertical"
        android:gravity="center"
        android:background="@color/colorPrimary"
        android:layout_height="200dp">
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:text="移动"
            android:layout_height="wrap_content" />
        <TextView

            android:layout_width="wrap_content"
            android:text="移动"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>

2、在AndroidManifest.xml中添加悬浮窗口权限和添加服务

//添加权限
 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
    //添加一个服务
  <service android:name="wq.com.camerademo.MyService" >
        </service>

3、代码实现(添加一个按钮,点击按钮并且显示悬浮窗口出来)

//按钮点击事件并且启动一个服务
public class TestActivity extends Activity{
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @TargetApi(Build.VERSION_CODES.M)
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                // takePicture();
                if(!Settings.canDrawOverlays(TestActivity.this)) {

                    Toast.makeText( TestActivity.this, "当前无权限,请授权", Toast.LENGTH_SHORT);

                    startActivityForResult( new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse( "package:"+ getPackageName())), 0);

                } else{

                    Intent intentOne = new Intent(TestActivity.this, MyService.class);
                    startService(intentOne);

                }

            }
        });
    }
}

//下面是开启一个服务显示悬浮窗口出来
public class MyService extends Service {
    private static final String TAG = "MyService";
    private WindowManager.LayoutParams wmParams;
    private WindowManager mWindowManager;
    private View mWindowView;
    private LinearLayout mText;
    private Context context;

    private int mStartX;
    private int mStartY;
    private int mEndX;
    private int mEndY;
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "onBind()");
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "onCreate()");
        context = this;

    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand()");
        initWindowParams();
        initView();
        addWindowViewZWindow();
        initClick();
        return super.onStartCommand(intent, flags, startId);
    }

    // 调用startService方法启动Service时调用该方法
    @Override
    public void onStart(Intent intent, int startId) {
        Log.e(TAG, "onStart()");
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mWindowView != null) {
            //移除悬浮窗口
            Log.i(TAG, "removeView");
            mWindowManager.removeView(mWindowView);
        }
        Log.e(TAG, "onDestroy()");
    }
    /**
     * 初始化Window对象的参数
     */
    private void initWindowParams() {
        Log.e(TAG, "initWindowParams()");
        //1,获取系统级别的WindowManager
        mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
        wmParams =new WindowManager.LayoutParams();
        //2,添加系统参数,确保悬浮框能显示到手机上
        //电话窗口。它用于电话交互(特别是呼入)。它置于所有应用程序之上,状态栏之下。
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        // flag 设置 Window 属性
        wmParams.flags
                |= WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

        //期望的位图格式。默认为不透明
        wmParams.format = PixelFormat.TRANSLUCENT;
        //不许获得焦点
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //窗口停靠位置
        wmParams.gravity = Gravity.LEFT | Gravity.TOP;
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

    }
    /**
     * 添加布局view,初始控件
     */
    private void initView() {
        Log.e(TAG, "initView()");
        mWindowView = LayoutInflater.from(getApplication()).inflate(R.layout.layout_window, null);
        mText = (LinearLayout) mWindowView.findViewById(R.id.linear);
    }
    /**
     * 添加View到桌面Window界面上
     */
    private void addWindowViewZWindow() {
        Log.e(TAG, "addWindowViewZWindow()");
        mWindowManager.addView(mWindowView, wmParams);
    }
    /**
     * 点击事件和拖拽事件
     */
    private void initClick() {
        mText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    //按下鼠标的时候记录下屏幕的位置
                    case MotionEvent.ACTION_DOWN:
                        mStartX = (int) event.getRawX();
                        mStartY = (int) event.getRawY();

                        break;
                    case MotionEvent.ACTION_MOVE:
                        mEndX = (int) event.getRawX();
                        mEndY = (int) event.getRawY();
                        if (needIntercept()) {
                            //getRawX是触摸位置相对于整个屏幕的位置,getX是控触摸点相对于控件最左边的位置
                            wmParams.x = (int) event.getRawX() - mWindowView.getMeasuredWidth() / 2;
                            wmParams.y = (int) event.getRawY() - mWindowView.getMeasuredHeight() / 2;
                            mWindowManager.updateViewLayout(mWindowView, wmParams);
                            return true;
                        }
                        break;
                    case MotionEvent.ACTION_UP:
                        if (needIntercept()) {
                            return true;
                        }
                        break;
                }

                return false;
            }
        });

        mText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i(TAG, "点击了");
                if (isAppAtBackground(MyService.this)) {
                    Intent intent = new Intent(MyService.this, TestActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });


    }

    /**
     * 判断当前应用是前台还是后台
     */
    private boolean isAppAtBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否拦截,根据滑动的距离
     *
     * @return
     */
    private boolean needIntercept() {
        if (Math.abs(mStartX - mEndX) > 30 || Math.abs(mStartY - mEndY) > 30) {
            return true;
        }
        return false;
    }
}

4、效果图如下所示
在这里插入图片描述

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值