Android Rotate Gesture Recognising — Part 2

Writing custom rotate gesture listener for Android views

Elvina Sh
ProAndroidDev

--

Photo by Gaspart from Dribbble

Hi there! In the Navigine team, we’ve been providing indoor and outdoor positioning mobile technologies that enable advanced indoor navigation and proximity solutions for eight years.

In our previous article, we wrote about handling tap, pan, and pinch gestures in Android views using gesture detectors. It was quite easy to implement because there we used the already written by Google gesture detectors. But now we will need to write our own gesture detectors because there are no ready ones. Of course, you can find some libraries and use them in your project, but if you can write yourself, then why not. Well, let’s start.

Base gesture detector

Our plan is to write our own rotation gesture detector, but this is our final goal. However, you must understand that before reaching the final goal, you need to do some sub-goals. In our case, we must be able to detect two-finger gestures before detecting rotation. But before writing a two-finger gesture detector we should write a base gesture detector with base methods that will be overridden in extended classes. So this class will have the abstract methods for detecting gesture start and progress which will be overridden in the next realized classes.

As you can see this is a pretty simple class which has only a few functions. Touch event will notify extended classes about touch events using abstract methods, reset state will set the progress as false, and update state will update the current event state and time delta.

Two-finger gesture detector

The next interesting part is writing a custom two-finger gesture detector. This class would be helpful in writing other gesture detectors too, where we will need to detect two-finger events, like rotate in this case, shove, etc.

So this class will extend our base gesture detector and also will not override our abstract methods for gesture started and progress. It will also contain the method for updating state by event and will call the superclass method, in this case, the base gesture detectors’ method. Also, it will contain the few pretty simple methods for getting the previous and current span and also two more methods for getting the raw coordinates for the x and y-axis.

And the last function is for checking if the gesture is sloppy or not. This function will be helpful for us in the next gesture detector classes. It also looks simple and could be easily understood. So let’s start the most interesting part of our article — realization of the rotate gesture detecting class.

Rotate gesture detector

At first, our class should extend the two-finger gesture detector class. Next, we will need to create some interface for the listener, so the users will get the events there and override the behavior of their view as they want. In our case, this listener will have the methods for the start, rotation, and the end of the rotation. We will pass our gesture detector class there, so the user will be able to call any public method he wants.

The moment has finally come for the realization of the gesture started and progress methods. They will pretty easy, we will check for the type of motion event and update our state. Also, our class will have the public getters so the user will be able to get this data.

Conclusion

As you can see, the rotation gesture detector is not as difficult as it might seem before the start. This usually always happens, something seems much more difficult for us, because it may still be a little scary for us to get out of our comfort zone and start doing it. I hope the article will be useful to you and you can use it in your projects, but even if not, bring out something useful for yourself.

--

--

Marketing associate at Navigine.com — Integrated software platform for precise indoor and outdoor positioning.