How to implement a draggable floating window in Android…

The steps to achieve a draggable floating window on Android are as follows:

  1. Create a layout file for displaying the interface of a floating window.
  2. Create a Service that starts a floating window and handles drag events.
  3. In the Service class, use the WindowManager.LayoutParams class to configure properties of the floating window, such as width, height, position, and type.
  4. In the onStartCommand() method of Service, use the WindowManager.addView() method to add the floating window to the screen.
  5. In the layout file, utilize touch events to monitor user gestures, such as ACTION_DOWN, ACTION_MOVE, ACTION_UP, and so on.
  6. In the callback method of touch events, update the position of the floating window based on the changes in user gestures. You can use the WindowManager.updateViewLayout() method to update the position of the floating window.
  7. In the onDestroy() method of Service, use the WindowManager.removeView() method to remove the floating window from the window.

Here is a simple sample code for implementing a draggable floating window.

  1. Create a layout file called float_window.xml to display the interface of the floating window.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Floating Window" />

</LinearLayout>
  1. Create a Service class called FloatWindowService to start a floating window and handle drag events.
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;
    }
}
  1. Register FloatWindowService in the AndroidManifest.xml file.
<service
    android:name=".FloatWindowService"
    android:enabled="true"
    android:exported="false" />
  1. Use the startService() method to start the FloatWindowService where the floating window needs to be activated.
startService(new Intent(MainActivity.this, FloatWindowService.class));

This accomplishes a simple.

bannerAds