实现 Android 可拖动悬浮窗的步骤如下:
- 创建一个布局文件,用于显示悬浮窗的界面。
- 创建一个 Service,用于启动悬浮窗并处理拖动事件。
- 在 Service 中,使用 WindowManager.LayoutParams 类来设置悬浮窗的属性,例如宽高、位置、类型等。
- 在 Service 的 onStartCommand() 方法中,使用 WindowManager.addView() 方法将悬浮窗添加到窗口中。
- 在布局文件中,使用触摸事件监听用户的手势操作,例如 ACTION_DOWN、ACTION_MOVE、ACTION_UP 等。
- 在触摸事件的回调方法中,根据用户手势的变化,更新悬浮窗的位置。可以使用 WindowManager.updateViewLayout() 方法来更新悬浮窗的位置。
- 在 Service 的 onDestroy() 方法中,使用 WindowManager.removeView() 方法将悬浮窗从窗口中移除。
下面是一个简单的示例代码,用于实现可拖动的悬浮窗:
- 创建一个布局文件 float_window.xml,用于显示悬浮窗的界面:
- 创建一个 Service 类 FloatWindowService,用于启动悬浮窗并处理拖动事件:
public class FloatWindowService extends Service implements View.OnTouchListener { private WindowManager windowManager; private WindowManager.LayoutParams layoutParams; private int initialX; private int initialY; private float initialTouchX; private float initialTouchY; @Override public void onCreate() { super.onCreate(); // 创建悬浮窗的布局参数 layoutParams = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); // 创建窗口管理器 windowManager = (WindowManager) getSystemService(WINDOW_SERVICE); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // 创建悬浮窗的视图 LinearLayout floatLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.float_window, null); floatLayout.setOnTouchListener(this); // 将悬浮窗添加到窗口中 windowManager.addView(floatLayout, layoutParams); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); // 将悬浮窗从窗口中移除 if (windowManager != null) { windowManager.removeView(floatLayout); } } @Override public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 记录悬浮窗的初始位置 initialX = layoutParams.x; initialY = layoutParams.y; initialTouchX = event.getRawX(); initialTouchY = event.getRawY(); return true; case MotionEvent.ACTION_MOVE: // 更新悬浮窗的位置 layoutParams.x = initialX + (int) (event.getRawX() - initialTouchX); layoutParams.y = initialY + (int) (event.getRawY() - initialTouchY); windowManager.updateViewLayout(view, layoutParams); return true; case MotionEvent.ACTION_UP: // 手指抬起时不处理事件 return true; } return false; } @Nullable @Override public IBinder onBind(Intent intent) { return null; } }
- 在 AndroidManifest.xml 文件中注册 FloatWindowService:
- 在需要启动悬浮窗的地方,使用 startService() 方法启动 FloatWindowService:
startService(new Intent(MainActivity.this, FloatWindowService.class));
这样就实现了一个简单的