2016-11-29 93 views
0

enter code here需要檢測滑動方向。在我的代碼中,我可以檢測方向,但它會出現像是,如果我滑過右上方或左上方, 。同樣的左邊,我的要求是沒有舉手指,如果我向左滑動它應該只剩下,同樣所有的方向。誰能幫我嗎。多謝!所有方向的滑動手勢(左,右,上,下)

@覆蓋 公共布爾的onTouchEvent(MotionEvent的TouchEvent){

switch (touchevent.getAction()) { 
     // when user first touches the screen we get x and y coordinateyo 
     case MotionEvent.ACTION_DOWN: { 
      x1 = touchevent.getX(); 
      y1 = touchevent.getY(); 
      break; 
     } 

     case MotionEvent.ACTION_MOVE: { 


      x2 = touchevent.getX(); 
      y2 = touchevent.getY(); 


      float deltaX = x2 - x1; 


      if (Math.abs(deltaX) > MIN_DISTANCE) { 
       // Left to Right swipe action 
       if (x2 > x1) {; 
        Log.e("RTL", "Right to Left Swap Performed"); 
       } 

       else { 
        Log.e("LTR", "Left to Right Swap Performed"); 
       } 

      } else { 

       if (y2 > y1) { 

        Log.e("UTD", "UP to Down Swap Performed"); 
       } 

       // Right to left swipe action 
       else { 
        Log.e("DTU", "Down to UP Swap Performed"); 
       } 
      } 
     } 
    } 
    return false; 
} 
+0

http://stackoverflow.com/questions/4139288/android-how-to-handle-right-to-left-swipe-gestures https://developer.android.com/training/手勢/ detector.html – cokceken

+0

@cokceken移動工作..但我無法得到確切的左,右,上下 – user2806221

+0

我不認爲有一個函數,返回方向。你應該做數學。計算移動矢量,計算角度。如果你的矢量大於X,並且你的角度在Z-Y之間,那麼用戶滑過方向1。 – cokceken

回答

0

您應該計算角度則與該決定。如果矢量大於最小距離值,則可以檢查角度。

float angle = (float) Math.toDegrees(Math.atan2(y2 - y1, x2 - x1)); 
if(angle > 45 && angle <= 135) return UP; 
else if(angle > 135 && angle <= 225) return LEFT; 
else if(angle > 225 && angle <= 315) return DOWN; 
else return RIGHT; 
0

在活動創建GestureDetectorCompat對象

GestureDetectorCompat gestureDetectorCompat; 

,並覆蓋的onTouchEvent

@Override 
     public boolean onTouchEvent(MotionEvent event) { 
      gestureDetectorCompat.onTouchEvent(event); 
      return true; 
     } 

,或者如果你想在一些視圖檢測,那麼你可以重寫onTouch

someView.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent motionEvent) { 
       gestureDetectorCompat.onTouchEvent(motionEvent); 
       return false; 
      } 
     }); 

和初始化gestureDetectorCompat在的onCreate(最好的地方如下)和你做。

gestureDetectorCompat = new GestureDetectorCompat(this, new GestureDetector.SimpleOnGestureListener() { 
     @Override 
     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
      float angle = (float) Math.toDegrees(Math.atan2(e1.getY() - e2.getY(), e2.getX() - e1.getX())); 

      if (angle > -45 && angle <= 45) { 
       Log.d(DEBUG_TAG, "Right to Left swipe performed"); 
       return true; 
      } 

      if (angle >= 135 && angle < 180 || angle < -135 && angle > -180) { 
       Log.d(DEBUG_TAG, "Left to Right swipe performed"); 
       return true; 
      } 

      if (angle < -45 && angle >= -135) { 
       Log.d(DEBUG_TAG, "Up to Down swipe performed"); 
       return true; 
      } 

      if (angle > 45 && angle <= 135) { 
       Log.d(DEBUG_TAG, "Down to Up swipe performed"); 
       return true; 
      } 

      return false; 
     } 


    }); 
相關問題