如何检测双击拖动? 我正在制作类似于宝石迷阵的游戏,并希望发现这一点。 在我目前的实现中,我能够获得滑动效果。我还想检测游戏中不同功能的双击拖动效果。
CODE:
@Override
public boolean onTouch(MotionEvent e, int scaledX, int scaledY) {
if (e.getAction() == MotionEvent.ACTION_DOWN && board.touchBoard(scaledX, scaledY) ) {
recentTouchY = scaledY;
recentTouchX = scaledX;
board.whichCell(scaledX,scaledY);
touchedCell = board.whichCellTouched(scaledX,scaledY);
//board.swapClearShape(touchedCell.getOffX(),touchedCell.getOffY());
} else if (e.getAction() == MotionEvent.ACTION_UP) { //NEED TO ADD IF TOUCH BOARD AREA
//swipe event
if (scaledX - recentTouchX < -25) {
System.out.println("SWAPPED LEFT");
Assets.playSound(Assets.swapSound);
board.setSwapLeft(true);
}
else if (scaledX - recentTouchX > 25) {
System.out.println("SWAPPED RIGHT");
Assets.playSound(Assets.swapSound);
board.setSwapRight(true);
}
//swap down
else if(scaledY- recentTouchY > 25){
System.out.println("SWAPPED DOWN");
Assets.playSound(Assets.swapSound);
board.setSwapDown(true);
}
//swap up
else if(scaledY- recentTouchY < -25){
System.out.println("SWAPPED UP");
Assets.playSound(Assets.swapSound);
board.setSwapUp(true);
}
}
return true;
}
我不是在寻找两个手指拖动,而是一次触摸然后另一次触摸然后拖动
答案 0 :(得分:2)
您可以使用GestureDetector.OnDoubleTapListener检测双击手势并触发标记。然后在onTouch(MotionEvent.ACTION_MOVE)上执行拖动逻辑。
如果您使用onDoubleTapEvent(MotionEvent e)方法,您将能够检测双击手势之间的事件并在第二个ACTION_UP事件之前触发该标志。
以下是双击侦听器实现的示例: http://developer.android.com/training/gestures/detector.html