@Override public boolean onTouchEvent(MotionEvent event) { int pointerCount = event.getPointerCount(); if (pointerCount == 1 && event.getAction() == MotionEvent.ACTION_UP) { // Single-tap detected performSingleTapAction(); return true; } return super.onTouchEvent(event); }
private int initialPointerCount = 0; private int deltaY = 0; @Override public boolean onTouchEvent(MotionEvent event) { int pointerCount = event.getPointerCount(); switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: // Save the initial pointer count and y-coordinate initialPointerCount = pointerCount; deltaY = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: if (pointerCount == initialPointerCount) { // Calculate the vertical distance between the first and last pointer int newDeltaY = (int) event.getY(); int distanceY = newDeltaY - deltaY; // Check if it meets the threshold for a swipe if (Math.abs(distanceY) > SWIPE_THRESHOLD) { // Swipe detected if (distanceY > 0) { performSwipeUpAction(); } else { performSwipeDownAction(); } } } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_CANCEL: // Reset the initial pointer count and y-coordinate initialPointerCount = 0; deltaY = 0; break; } return super.onTouchEvent(event); }In this example, we save the initial pointer count and y-coordinate when the first pointer is pressed down. On subsequent MOVE events, we check if the pointer count is the same as the initial count, and calculate the vertical distance between the first and last pointer. If this distance meets our swipe threshold, we determine the direction of the swipe and perform the corresponding action. We reset the initial values when all pointers are released.