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.
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) :
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;
}
}
I have tried to implement the multitouch like that, but it doesn’t works :
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 & 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 & MotionEvent.ACTION_POINTER_ID_MASK) >> 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 & MotionEvent.ACTION_POINTER_ID_MASK) >> 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 & MotionEvent.ACTION_POINTER_ID_MASK) >> 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 & MotionEvent.ACTION_POINTER_ID_MASK) >> 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;
}
What’s wrong ? Can you help me ? Thanks you !
I’ve found the solution and the explanation.
Android doesn’t support multi-touch in multi views, the multi-touch is only for one view. So, to have a multi-touch for multi view, we had to catch touch event in the main activity and generate new event for child views.
Thanks to Pascal Welsch and his blog ( http://www.passsy.de/multitouch-for-all-views/ ), and a modification of a mistake in his code that I’ve found ( http://code.google.com/p/friendbattle/issues/detail?id=19 ), I have solved my problem.
Hope this will help for other people 🙂