Question

I'm currently working on my first multi-touch android application and I'm having some difficulty with the onTouchEvent(). I've used some code from an online tutorial, which seems to give me the right pointer ID information for each touch on the screen, but the event coordinates for ACTION_POINTER_DOWN events always seem to be the same as the coordinates for the intial touch. My code is below:

private int getIndex(MotionEvent event) {
          int idx = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
          return idx;
}

@Override

public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        switch(action) {
                case MotionEvent.ACTION_DOWN : {
                        int id = event.getPointerId(0);
                        Log.d("CV", "Point number " +id+ " is down at X value " +event.getX());
                        callbackListener.onTouchDown(event, id);
                        break;
                }
                case MotionEvent.ACTION_MOVE : {
                        int touchCounter = event.getPointerCount();
                        for (int t = 0; t < touchCounter; t++) {
                                int id = event.getPointerId(t);
                                callbackListener.onMove(event, id);
                        }
                        break;
                }
                case MotionEvent.ACTION_POINTER_DOWN : {
                        int id = event.getPointerId(getIndex(event));
                        Log.d("CV", "Point number " +id+ " is down at X value " +event.getX());
                        callbackListener.onTouchDown(event, id);
                        break;
                }
                case MotionEvent.ACTION_POINTER_UP : {
                        int id = event.getPointerId(getIndex(event));
                        //Log.d("CV", "Other point up ["+id+"]");
                        callbackListener.onTouchUp(event, id);
                        break;
                }
                case MotionEvent.ACTION_UP : {
                        int id = event.getPointerId(0);
                        //Log.d("CV", "Pointer up ["+id+"]");
                        callbackListener.onTouchUp(event, id);
                        break;
                }
        }
        return true;
}

In the Log, where my pointer X positions are shown, you can see that when I add an additional touch to the screen (pointer number 1), the X coordinate is the same as the first touch (pointer number 0)...

11-25 12:34:02.911: D/CV(25231): Point number 0 is down at X value 260.60608
11-25 12:34:05.281: D/CV(25231): Point number 0 is down at X value 477.57578
11-25 12:34:06.261: D/CV(25231): Point number 0 is down at X value 581.8182
11-25 12:34:11.891: D/CV(25231): Point number 0 is down at X value 267.87878
11-25 12:34:13.321: D/CV(25231): Point number 1 is down at X value 267.87878

This is frustrating me as I've pretty much copied and pasted the entire thing from online tutorials and I can't see why its not working properly! Also, as you can imagine, the ACTION_MOVE isn't working properly either...

Any help is much appreciated, thanks

Was it helpful?

Solution

This is because event.getX() returns value for the first pointer index.

You should be using, getX(int pointerIndex) as follows:

Log.d("CV", "Point number " +id+ " is down at X value " +event.getX(getIndex(event)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top