首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onFling()手势不准确

onFling()手势不准确
EN

Stack Overflow用户
提问于 2013-05-05 14:22:24
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
    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中,我在相反的方向上进行了“放飞”。这可能是模拟器的问题,还是我的代码没有准确地测量我的手势的方向?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-06 07:06:38

好的,达特是对的,我想给他一点功劳。我修改了我的代码,以便更准确地测量手势,如果有人想使用这段代码,可以参考下面的代码。我将不得不稍微修改一下灵敏度,因为左上角的数字不会像上下角那样高,但这是一个非常可靠的起点。

代码语言:javascript
复制
    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;
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16381973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档