Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing drag and drop with multi-touch in Android
    primarykey
    data
    text
    <p>I'm looking for a way to implement drag and drop with multi touch in my application. I have many views and I want that a first user drag a view in the same time than a second user is dragging an other view.</p> <p>My single touch drag and drop works well, here is a part of my code (my views are implementing this listener. onDragStart records the point touched, onDragContinuing change the position of the view and onDrop check if the position is valid) :</p> <pre><code>public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { return onDragStart(event.getX(), event.getY()); } else if (event.getAction() == MotionEvent.ACTION_MOVE){ return onDragContinuing(event.getRawX(), event.getRawY()); } else if (event.getAction() == MotionEvent.ACTION_UP){ return onDrop(event.getRawX(), event.getRawY()); } else{ return false; } } </code></pre> <p>I have tried to implement the multitouch like that, but it doesn't works :</p> <pre><code>private static final int INVALID_POINTER_ID = -1; // The ‘active pointer’ is the one currently moving our object. private int mActivePointerId = INVALID_POINTER_ID; public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); int actionCode = action &amp; MotionEvent.ACTION_MASK; switch(actionCode){ case MotionEvent.ACTION_DOWN :{ Toast.makeText(getContext(), "down", Toast.LENGTH_SHORT).show(); // Save the ID of this pointer mActivePointerId = event.getPointerId(0); return onDragStart(event.getX(), event.getY()); } case MotionEvent.ACTION_POINTER_DOWN :{ Toast.makeText(getContext(), "pointer down", Toast.LENGTH_SHORT).show(); // Save the ID of this pointer // Extract the index of the pointer that left the touch sensor final int pointerIndex = (action &amp; MotionEvent.ACTION_POINTER_ID_MASK) &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT; mActivePointerId = event.getPointerId(pointerIndex); return onDragStart(event.getX(mActivePointerId), event.getY(mActivePointerId)); } case MotionEvent.ACTION_MOVE :{ Toast.makeText(getContext(), "move", Toast.LENGTH_SHORT).show(); final int pointerIndex = (action &amp; MotionEvent.ACTION_POINTER_ID_MASK) &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT; final int mCurrentPointerId = event.getPointerId(pointerIndex); if (mActivePointerId == mCurrentPointerId){ return onDragContinuing(getRawX(event, mActivePointerId), getRawY(event, mActivePointerId)); } else return false; } case MotionEvent.ACTION_UP :{ Toast.makeText(getContext(), "up", Toast.LENGTH_SHORT).show(); final int pointerIndex = (action &amp; MotionEvent.ACTION_POINTER_ID_MASK) &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT; final int mCurrentPointerId = event.getPointerId(pointerIndex); if (mActivePointerId == mCurrentPointerId){ return onDrop(event.getRawX(), event.getRawY()); } else return false; } case MotionEvent.ACTION_POINTER_UP :{ Toast.makeText(getContext(), "pointer up", Toast.LENGTH_SHORT).show(); final int pointerIndex = (action &amp; MotionEvent.ACTION_POINTER_ID_MASK) &gt;&gt; MotionEvent.ACTION_POINTER_ID_SHIFT; final int mCurrentPointerId = event.getPointerId(pointerIndex); if (mActivePointerId == mCurrentPointerId){ return onDrop(getRawX(event, pointerIndex), getRawY(event, pointerIndex)); } else return false; } default :{ return false; } } } /** * MotionEvent has no getRawX(int) method; simulate it pending future API approval. */ private static float getRawX(MotionEvent event, int pointerIndex) { float offset = event.getX() - event.getRawX(); return event.getX(pointerIndex) + offset; } /** * MotionEvent has no getRawY(int) method; simulate it pending future API approval. */ private static float getRawY(MotionEvent event, int pointerIndex) { float offset = event.getY() - event.getRawY(); return event.getY(pointerIndex) + offset; } </code></pre> <p>What's wrong ? Can you help me ? Thanks you !</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload