Quick note about learning GestureDetector
and ScaleGestureDetector
.
GestureDetector
Android provides GestureDetector
class for detecting common gestures.
Steps
- Create an instance of
GestureDetector
- Override View or Activity’s
onTouchEvent(MotionEvent event)
and pass event toGestureDetector.onTouchEvent(event)
; - Implement
GestureDetector.OnGestureListener
callbacks for gestures. - Implement
GestureDetector.OnDoubleTapListener
callbacks for click gestures. - Or implement
GestureDetector.SimpleOnGestureListener
callbacks if process only a few gestures above.
Suggest to use SimpleOnGestureListener
which only needs to implement the gestures we need.
Gestures
Check SimpleOnGestureListener Api page for the full list of gestures.
The following table shows the gestures of both OnGestureListener
and OnDoubleTapListener
. The first six are from OnGestureListener
and the last three are from OnDoubleTapListener
. But SimpleOnGestureListener
have all of them.
Gestures | Description |
---|---|
onDown() | Notified when a tap occurs with the down MotionEvent that triggered it. |
onFling() | Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. |
onLongPress() | Notified when a long press occurs with the initial on down MotionEvent that trigged it. |
onScroll() | Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent. |
onShowPress() | The user has performed a down MotionEvent and not performed a move or up yet. |
onSingleTapUp() | Notified when a tap occurs with the up MotionEvent that triggered it. |
onDoubleTap() | Notified when a double-tap occurs. |
onDoubleTapEvent() | Notified when an event within a double-tap gesture occurs, including the down, move, and up events. |
onSingleTapConfirmed() | Notified when a single-tap occurs. Unlike onSingleTapUp() , it will only be called after the detector is confident that the user’s first tap is not followed by a second tap leading to a double-tap gesture. |
Example
ScaleGestureDetector
It detects scaling transformation gestures or called pinch gestures for scaling views. (Two fingers)
Steps
- Create an instance of
ScaleGestureDetector
- Override View or Activity’s
onTouchEvent(MotionEvent event)
and pass event toScaleGestureDetector.onTouchEvent(event)
; - Implement
ScaleGestureDetector.OnScaleGestureListener
callbacks for gestures. - Or implement
ScaleGestureDetector.SimpleOnScaleGestureListener()
callbacks if process only a few gestures above.
Gestures
Gestures | Description |
---|---|
onScaleBegin() | Responds to the beginning of a scaling gesture. |
onScale() | Responds to scaling events for a gesture in progress. |
onScaleEnd() | Responds to the end of a scale gesture. |