我一直在玩动作事件和拖拽(所以我不会把手指从屏幕上拿下来——这不是一次投掷)。问题是它只检测到第二个、第三个、第四个等等,当我的手指移动过上下拖动开始和结束的点时,拖动向下或向上移动。
见下面的代码。向上拖动时计数等于2,向下拖动时计数等于1。然而,它只在我向上移动手指(计数2)然后向下移动超过开始向上移动的点(计数1)时才起作用,而不是在等于2的点之前。当我向上移动时,它继续起作用。只有当我向下移动超过改变方向的点时,它才起作用。但它为什么不在这些点之前将其识别为阻力,因为在这些方向上的任何移动都应该是阻力。我该如何解决?
下面是我测试它的简单代码:

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        oldX = (int) event.getRawX();
        oldY = (int) event.getRawY();


        break;

    case MotionEvent.ACTION_MOVE:

        posY = (int) event.getRawY();
        posX = (int) event.getRawX();


        diffPosY = posY - oldY;
        diffPosX = posX - oldX;

       if (diffPosY > 0){//down

            count  = 1;

        }
        else//up
        {
            count = 2;

        }

        break;

最佳答案

如果我理解您试图正确执行的操作,我认为您需要在您的oldX中更新oldYcase MotionEvent.ACTION_MOVE:,在您使用它设置diffPosYdiffPosX之后,因为您当前仅在触摸开始时设置oldXoldY。因此,在设置diffPosYdiffPosX之后,添加:

oldX = posX;
oldY = posY;

更新
因为动作事件是经常处理的,所以你可能想引入一些触控斜坡来解释这样一个事实:当你把手指放在屏幕上时,你可能会在你向上移动之前稍微向下移动一点而没有意识到,如果你慢慢刷卡,你可能会不经意地朝着你认为的方向轻轻刷卡。这看起来就像你在下面的评论里看到的一样。下面的代码应该有助于解决这个问题,但会使它对方向的变化反应稍微慢一点:
// Get the distance in pixels that a touch can move before we
// decide it's a drag rather than just a touch. This also prevents
// a slight movement in a different direction to the direction
// the user intended to move being considered a drag in that direction.
// Note that the touchSlop varies on each device because of different
// pixel densities.
ViewConfiguration vc = ViewConfiguration.get(context);
int touchSlop = vc.getScaledTouchSlop();

// So we can detect changes of direction. We need to
// keep moving oldY as we use that as the reference to see if we
// are dragging up or down.
if (posY - oldY > touchSlop) {
    // The drag has moved far enough up the Y axis for us to
    // decide it's a drag, so set oldY to a new position just below
    // the current drag position. By setting oldY just below the
    // current drag position we make sure that if while dragging the
    // user stops their drag and accidentally moves down just by a
    // pixel or two (which is easily done even when the user thinks
    // their finger isn't moving), we don't count it as a change of
    // direction, so we use half the touchSlop figure as a small
    // buffer to allow a small movement down before we consider it
    // a change of direction.
    oldY = posY - (touchSlop / 2);
} else if (posY - oldY < -touchSlop) {
    // The drag has moved far enough down the Y axis for us to
    // decide it's a drag, so set oldY to a new position just above
    // the current drag position. This time, we set oldY just above the
    // current drag position.
    oldY = posY + (touchSlop / 2);
}

08-05 04:16