How to listen the number of touches?

290 Views Asked by At

I thought it's easy. Like always

//somewhere inside constructor
overlay.addOnTouchListener(this)
//...
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        event?:return false
        if (event.actionMasked == MotionEvent.ACTION_DOWN || event.actionMasked == MotionEvent.ACTION_POINTER_DOWN) {
           //start drag
            moving=true
        } else if (event.actionMasked == MotionEvent.ACTION_MOVE) {
            if (!moving) return true
          //do drag
        } else if (event.actionMasked == MotionEvent.ACTION_UP) {
           //stop drag
            println("touch:up(${event.pointerCount})")
        }
        return true
    }

I was expecting MotionEvent.ACTION_UP will fire up on release of each finger.But looks like it only fires once for the last finger.
Is there way catch moment when user releases one finger but keep's moving another one?

2

There are 2 best solutions below

0
undefined On BEST ANSWER

The initial statement is incorrect. Actualy android notifies all touch ups with events MotionEvent.ACTION_UP/MotionEvent.ACTION_POINTER_UP

1
alfie On

You can use two methods getPointerCount(x) and/or getPointerId(x).

getPointerCount gets the number of pointers of data contained in an event. This is always >= 1.

While getPointerId() returns the pointer identifier associated with a particular pointer data index in an event. The identifier tells you the actual pointer number associated with the data, accounting for individual pointers going up and down since the start of the current gesture.

So in your case, if you want to capture the pointer that was not removed from the screen, you can use getPointerCount to check if there has been a pointer removed and then use getPointerId to assign some sort of function when the last pointer has been released.