1.添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
录像要用到的权限,动态申请一下
2.WindowManager添加一个窗口
private WindowManager mWindowManager;
private WindowManager.LayoutParams mLayoutParams;
private View mView;
private void initFloatWindow() {
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mLayoutParams = new WindowManager.LayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
mLayoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
mLayoutParams.format = PixelFormat.RGBA_8888;
mLayoutParams.gravity = Gravity.LEFT | Gravity.TOP;
mLayoutParams.flags =
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//窗口大小
mLayoutParams.width = 400;
mLayoutParams.height = 500;
//窗口出现位置
mLayoutParams.x = 0;
mLayoutParams.y = 0;
LayoutInflater layoutInflater = LayoutInflater.from(this);
//布局文件中需要一个SurfaceView
mView = layoutInflater.inflate(R.layout.float_recorder_window, null);
mView.setOnTouchListener(new FloatingOnTouchListener());//触摸移动窗口
mWindowManager.addView(mView, mLayoutParams);//添加悬浮窗口
prepareCamera2();
}
private class FloatingOnTouchListener implements 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) even