【问题标题】:How do you detect a double tap drag?如何检测双击拖动?
【发布时间】:2015-06-23 20:36:02
【问题描述】:

如何检测双击拖动? 我正在制作一个类似于宝石迷阵的游戏,并想检测到这一点。 在我当前的实现中,我能够获得滑动效果。我还想检测游戏中不同功能的双击拖动效果。

代码:

@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;
}

我不是在寻找两个手指拖动,而是一个触摸,然后另一个触摸,然后拖动

【问题讨论】:

标签: java android touch logic drag


【解决方案1】:

您可以使用GestureDetector.OnDoubleTapListener 来检测双击手势并触发标志。然后在您的 onTouch (MotionEvent.ACTION_MOVE) 上执行拖动逻辑。

如果使用 onDoubleTapEvent(MotionEvent e) 方法,则可以检测到双击手势之间的事件,并在第二个 ACTION_UP 事件之前触发标志。

以下是双击监听器实现的示例: http://developer.android.com/training/gestures/detector.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-14
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多