2013-05-05 2 views
0

그래서, 난 내 onFling에 대한() 메소드를 다음과 같은 몸을 가지고 내 문제가 그렇게하고있다 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에 표시되어 반대 방향으로 "뛰어 넘기"가됩니다. 이것이 에뮬레이터 문제 일 수 있습니까, 아니면 제 제스처의 방향을 정확하게 측정하지 못하는 코드입니까?

+0

에뮬레이터가 아니라 코드에 오류가 있다는 것은 좋은 기본 가정입니다. 코드를 확인하고 작동하는지 확인한 후에 에뮬레이터를 의심 할 수 있습니다. – dutt

+0

그래서 X와 Y의 속도를 출력하는 logcat 라인을 삽입했습니다. 때로는 다소 엉뚱한 것으로 보입니다. 상향 제스처는 -velocityY를 생성해야하지만 때로는 올바르게 동작하지만 다른 값은 갑자기 긍정적인데, 이는 내가 원하는 것이 아닙니다. 이 문제를 에뮬레이터로 해결할 수있는 방법이 있습니까? 저는 .apk를 친구에게 보냈습니다. 그는 실제 장치에서 기대했던대로 행동한다고합니다. – Jon

답변

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; 
    } 
+0

이러한 상수에 적절한 값은 무엇입니까? –

관련 문제