The android control implements the click and drag effect

  • 2021-12-11 18:49:36
  • OfStack

This article example for everyone to share the android control to achieve click drag effect of the specific code, for your reference, the specific content is as follows

Analysis

setOnClickListener

setOnClickListener can receive the click action of the control with no return value

If you want to drag the control, you have to define it yourself using setOnTouchListener

setOnTouchListener

Calculate the distance to move by the difference between the coordinates when pressed and the coordinates when moved, and then change the position of the control for the purpose of dragging
Because the Touch event will continue to execute the click event if false is returned after the execution is completed, which is not what we want. If you return true directly, it means that the event is processed and the click event is no longer executed, which is not what we want.
At this time, it is necessary to judge when to return true and when to return false, so as to achieve the purpose of clicking + dragging at the same time

Instance code

Binding event


// Bind drag event 
floatView.floatMenuIcon.setOnTouchListener(touchListener);
// Bind single-click event, here is lambda Expression 
floatView.floatMenuIcon.setOnClickListener(view -> {
 EventBus.getDefault().post(AccessibilityService.GLOBAL_ACTION_BACK);
});

Rewrite onTouchListener

Matters needing attention

ACTION_UP is an event executed when the finger is lifted, and the judgment of the click event is placed here. If the coordinates when pressing are not much different from those when lifting, we regard it as a click action. If we need to deal with the long press action or double click action in the later period, the reason is similar.

Complete code


private final View.OnTouchListener touchListener = new View.OnTouchListener() {
 // The coordinates of the record when it is first pressed will change due to dragging 
 private float dX;
 private float dY;
 // Record the coordinates at the time of initial pressing, and do not change due to dragging 
 private float sX;
 private float sY;

 @SuppressLint("ClickableViewAccessibility")
 @Override
 public boolean onTouch(View view, MotionEvent motionEvent) {
  switch (motionEvent.getAction()) {
   case MotionEvent.ACTION_DOWN:
    dX = motionEvent.getRawX();
    dY = motionEvent.getRawY();
    sX = motionEvent.getRawX();
    sY = motionEvent.getRawY();
    break;
   case MotionEvent.ACTION_MOVE:
    // Drag event, in order to be able to drag in real time, instead of lifting and then moving, the processing code is put into ACTION_MOVE Medium 
    float nX = (int) motionEvent.getRawX();
    float nY = (int) motionEvent.getRawY();
    float cW = nX - dX;
    float cH = nY - dY;
    dX = nX;
    dY = nY;
    layoutParams.x = (int) (layoutParams.x + cW);
    layoutParams.y = (int) (layoutParams.y + cH);
    windowManager.updateViewLayout(mView, layoutParams);
    break;
   case MotionEvent.ACTION_UP:
    // If the position when lifting is roughly the same as the position when pressing is regarded as a click event 
    // The interval value may be negative, so you want to compare it in absolute value 
    if (Math.abs(motionEvent.getRawX() - sX) > 6 && Math.abs(motionEvent.getRawY() - sY) > 6) {
     return true;
    }
    break;
  }
  return false;
 }
};

Related articles: