Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Android Rotate Gesture Recognising — Part 2

Writing custom rotate gesture listener for Android views

 

 
Elvina Sh
Marketing associate at Navigine.com — Integrated software platform for precise positioning.
Published: September 15, 2020
Tweet
Share
 

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.

 

Tags: Android,Android App Development,Programming,AndroidDev,Gesture Recognition

 

View original article at: 


 

Originally published: September 11, 2020

Android News
Compose CameraX on Android
Compose CameraX on Android

By Peng Jiang

Android new UI toolkit Jetpack compose is in beta now, which has all the features you need to build production-ready apps. CameraX is another Jetpack support library, which let you control the camera easier. As compose is still under development, lots of the views are still not available the compose way.

By ProAndroidDev -
Android News
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03
Getting… your BottomSheetScaffold working on Jetpack Compose Beta 03

By Carlos Mota

It’s Monday, no releases this week, and… there’s a new version of Jetpack Compose — beta 03—available. What a perfect time to just increment 02 to 03 and see what’s new. The API is (almost) final so after updating from alpha to beta there weren’t any big changes to do. However, and remember that’s still in development, there’s always something that I need to update. 

By ProAndroidDev -
Android News
Noisy Code With Kotlin Scopes
Noisy Code With Kotlin Scopes

By Chetan Gupta

Scopes make your code more readable? think again... You are going to encounter these scope functions namely let, run, apply, also, within every Kotlin codebase, along with all the mischievous ways developers exploit their usage from the way they were intended for. Let see how popular opinion on those ends up just as a code noise.

By ProAndroidDev -
Android News
Improving Android DataBinding with Bindables library
Improving Android DataBinding with Bindables library

By Jaewoong Eum

DataBinding is one of the most important factors for MVVM architecture. The basic concept of DataBinding is to link the view and view model via observer patterns, properties, event callbacks, etc. Linking and automating communication between the view via the bound properties or something in the view model has a lot of benefits in the MVVM architecture concept.

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One