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
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

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