Android custom draggable floating button

personal center

DownLoad

The implementation steps of Android custom draggable floating button are as follows:

1. Create a custom View that inherits from View or ImageButton.

2. Implement the onTouchEvent method in View to handle touch events and implement dragging.

3. Implement the onDraw method in View to draw the style of the floating button.

4. Add the View to the Activity, and set its LayoutParams so that it can be suspended on the screen.

5. Add a click event in View to realize the operation after clicking the floating button.

6. Animation effects can be added to make the floating button more vivid.

Here is a simple implementation example:

```
public class FloatButton extends ImageButton {

    private int lastX;
    private int lastY;

    public FloatButton(Context context) {
        super(context);
    }

    public FloatButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FloatButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = x - lastX;
                int dy = y - lastY;
                int left = getLeft() + dx;
                int top = getTop() + dy;
                int right = getRight() + dx;
                int bottom = getBottom() + dy;
                layout(left, top, right, bottom);
                lastX = x;
                lastY = y;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {         super.onDraw(canvas);         // Draw the style of the floating button     }


    public void setFloatButtonClickListener(OnClickListener listener) {
        setOnClickListener(listener);
    }
}
```

Add the View to the Activity:

```
FloatButton floatButton = new FloatButton(this);
floatButton.setImageResource(R.drawable.float_button);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
layoutParams.gravity = Gravity.TOP | Gravity.START;
layoutParams.x = 0;
layoutParams.y = 0;
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(floatButton, layoutParams);
```

Add click event:

```
floatButton.setFloatButtonClickListener(new View.OnClickListener() {     @Override     public void onClick(View v) {         // Operation after clicking the floating button     } }); ```





Add animation effects:

```
ObjectAnimator animatorX = ObjectAnimator.ofFloat(floatButton, "translationX", 0, 100, -100, 0);
animatorX.setDuration(2000);
animatorX.setRepeatCount(ValueAnimator.INFINITE);
animatorX.start();
```

Guess you like

Origin blog.csdn.net/YDHIT/article/details/130842770