问题描述
我正在寻找一种方式来实现拖动,并与多点触摸在我的应用程序拖放。
我有很多意见,我想第一用户拖动的同时比第二个用户的视图中拖动其他视图。
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.
我的单点触摸拖拽效果很好,这是我的code(我的看法正在实施这个监听器的一部分。onDragStart记录点感动,onDragContinuing更改视图的位置和onDrop检查位置有效):
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不支持在多次多触摸,多触摸是只为一个图。因此,有一个多点触摸的多视图,我们在主要活动搭上触摸事件和子视图生成新的事件。
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.
由于帕斯卡韦尔施和他的博客(),并在他的code错,我已经发现的修改(的),我解决了我的问题。
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 :-)
这篇关于实现拖放在Android的多点触控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!