2014-10-08 78 views
0

我創建了一個實現GestureDetector.OnGestureListener的類。但是,我不知道如何調用這些方法。是否有類似view.setOnTouchListener()的GestureListener允許您接收事件?先進的謝謝你。這裏是我的類如何接收來自OnGestureListener的事件

公共類GestureHandler實現GestureDetector.OnGestureListener {

private static final int SWIPE_MIN_DISTANCE = 120; 
private static final int SWIPE_THRESHOLD_VELOCITY = 200; 
private int direction = -1; 

@Override 
public boolean onDown(MotionEvent e) { 
    return true; 
} 

@Override 
public void onShowPress(MotionEvent e) { 

} 

@Override 
public boolean onSingleTapUp(MotionEvent e) { 
    return false; 
} 

@Override 
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
    return false; 
} 

@Override 
public void onLongPress(MotionEvent e) { 

} 

@Override 
public boolean onFling(MotionEvent event1, MotionEvent event2, 
         float velocityX, float velocityY) { 
    //Left Swipe 
    if(event1.getX() - event2.getX() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 270; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
     //Right Swipe 
    } else if(event2.getX() - event1.getX() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 90; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
    } 
    //Up Swipe 
    if(event1.getY() - event2.getY() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 0; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
     //Down Swipe 
    } else if(event2.getY() - event1.getY() > SWIPE_MIN_DISTANCE && 
      Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) { 
     direction = 180; 
     Log.d("Swiped: ", direction + ""); 
     return true; 
    } 
    direction = -1; 
    Log.d("Swiped: ", direction + ""); 
    return false; 
} 

}這裏

回答

0

很簡單的解決方案。 :)

下面是我這究竟是怎麼在我的項目,與谷歌玻璃加工 -

我實際上並沒有延伸GestureDetector。我有這樣的事情:

public class EXAMPLE { 
    private GestureDetector gestureDetector; 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     gestureDetector = createGestureDetector(this); 

    } 
    private GestureDetector createGestureDetector(Context context) { 
     GestureDetector gestureDetectorTemp = new GestureDetector(context, new GestureDetector.OnGestureListener() { 
      @Override 
      public boolean onDown(MotionEvent motionEvent) { 
       return false; 
      } 

      @Override 
      public void onShowPress(MotionEvent motionEvent) { 
       return false; 
      } 

      @Override 
      public boolean onSingleTapUp(MotionEvent motionEvent) { 
       return false; 
      } 
      @Override 
      public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent2, float distanceX, float distanceY) { 
       return false; 
      } 
      @Override 
      public void onLongPress(MotionEvent motionEvent) { 
      } 
      @Override 
      public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) { 
      return false; 
      } 
     }); 
     return gestureDetectorTemp; 
    } 

    @Override 
    public boolean onGenericMotionEvent(MotionEvent event) { 
     if (gestureDetector != null) { 
      return gestureDetector.onTouchEvent(event); 
     } 
     return false; 
    } 
} 

最後一部分是非常重要的。在任何通用運動事件中,如果gestureDetector不爲null,則將通過gestureDetector發送事件進行處理。

還想你還需要了解什麼是返回false;並返回true;事情的意思。如果你返回false,那就意味着這個事件沒有被消耗掉。如果您返回true,則該事件將被消耗。換句話說,如果你返回true,那麼沒有別的東西會被激活,因爲事件會被'吃掉',但是如果你返回false,這會把事件發送到其他功能上,這些功能可能會在採取行動時做些事情。

我希望這會有所幫助。祝你好運:)