Advanced event handling
We've seen how to process onTouchEvent()
, but we can also detect some gestures or more complex interactions. Android provides us with the GestureDetector
to help us detect some gestures. There is even a GestureDetectorCompat
on the support library to provide this support to older versions of Android.
For more information on the GestureDetector
, please check the Android documentation.
Detecting gestures
Let's change the code we've been building to use GestureDetector
. We'll also use a Scroller
implementation to scroll smoothly between values. We can modify the constructor to create the Scroller
object and the GestureDetector
with an implementation of a GestureDetector.OnGestureListener
:
private GestureDetector gestureListener; private Scroller angleScroller; public CircularActivityIndicator(Context context, AttributeSet attributeSet) { super(context, attributeSet); ... selectedAngle = 280; pressed = false; angleScroller = new Scroller...