0

因此,我的 onFling() 方法具有以下主体:

    public boolean onFling(MotionEvent e1, MotionEvent e2,
                         float velocityX, float velocityY) {
      try {

        // Left swipe
        if ( velocityX < -SWIPE_THRESHOLD_VELOCITY) {
            if( velocityY < -SWIPE_THRESHOLD_VELOCITY) {
                GameWindow.writeToOutput("northwest");
                Log.d("Console","Wrote NW");
            }
            else
                GameWindow.writeToOutput("west");

            return true;
        // Right swipe
        } else if (velocityX > SWIPE_THRESHOLD_VELOCITY) {

            if( velocityY < -SWIPE_THRESHOLD_VELOCITY) {
                GameWindow.writeToOutput("northeast");
                Log.d("Console","Wrote NE");
            }
            else
                GameWindow.writeToOutput("east");

            return true;

        }

        if ( velocityY > SWIPE_THRESHOLD_VELOCITY) {

            GameWindow.writeToOutput("south");

            return true;
        }


        if ( velocityY < -SWIPE_THRESHOLD_VELOCITY) {

            GameWindow.writeToOutput("north");

            return true;
        }
      } catch (Exception e) {
        Log.e("YourActivity", "Error on gestures");
      }

      return false;
    }

我的问题是,我会做一个左上角的“甩动”,但是速度会突然显示在 logcat 中,我在相反的方向做了一个“甩动”。这可能是模拟器问题,还是我的代码没有准确测量我的手势方向?

4

1 回答 1

2

好的,Dutt 是对的,我想为此表扬他。我修改了我的代码以更准确地测量手势,以防有人想使用此代码,如下所示。我将不得不稍微修改灵敏度,因为左上角的数字不会像直线上的那么高,但这是一个非常可靠的起点。

    public boolean onFling(MotionEvent e1, MotionEvent e2,
                         float velocityX, float velocityY) {
      try {
          double xDir = e1.getX() - e2.getX();
          double yDir = e1.getY() - e2.getY();

          Log.d("Console","xVel = " + xDir);
          Log.d("Console","yVel = " + yDir);

        if ( xDir > SWIPE_THRESHOLD_VELOCITY) {
            if( yDir > SWIPE_THRESHOLD_VELOCITY) {
                //Up-Left swipe
                GameWindow.writeToOutput("northwest");
            }
            else  if ( yDir < -SWIPE_THRESHOLD_VELOCITY){
                //Down-Left swipe
                GameWindow.writeToOutput("southwest");
            }
            else{
                //Left swipe
                GameWindow.writeToOutput("west");
            }

            return true;
        } else if (xDir < -SWIPE_THRESHOLD_VELOCITY) {

            if( yDir > SWIPE_THRESHOLD_VELOCITY) {
                //Up-Right swipe
                GameWindow.writeToOutput("northeast");
            }else  if ( yDir < -SWIPE_THRESHOLD_VELOCITY){
                //Down-Right swipe
                GameWindow.writeToOutput("southeast");
            }
            else {
                //Right swipe
                GameWindow.writeToOutput("east");
            }

            return true;

        }

        if ( yDir < -SWIPE_THRESHOLD_VELOCITY) {
            //Down swipe
            GameWindow.writeToOutput("south");
            return true;
        }


        if ( yDir > SWIPE_THRESHOLD_VELOCITY) {
            //Up swipe
            GameWindow.writeToOutput("north");
            return true;
        }
      } catch (Exception e) {
        Log.e("YourActivity", "Error on gestures");
      }

      return false;
    }
于 2013-05-05T23:06:37.530 回答