B4A Library [Lib] Gesture Detector

This library adds the detection of standard gestures (press, single-tap, double-tap, long tap, drag, scroll, fling, pinch, rotation) to B4A. Instead of using the Touch events to figure out what the user really did, now you just set 15 different listeners with one line of code and you get the gestures as events with all the useful values (scrolling distance, fling velocity, pinch variation, rotation angle...).
With this library, you can also know easily the pressure or the size of a touch event.

It works with any view.

Note: you have to create an instance of GestureDetector (with Dim) for each view you want to bind to the detector with SetOnGestureListener.

v2.2:
- Freeware.

v2.3:
- I fixed an issue with the boolean value returned by the OnTouch event with some views (e.g. ListView or ScrollView);
- I added a new demo (GD_SwipeLV);
- The OnDown event is now raised before OnDoubleTap.

v2.4:
- I fixed an issue when a view is dragged after one of the pointer is up (delta values were computed according to the pointer 0, not to the remaining pointer).

Incompatible with Android versions < 2.
 

Attachments

  • GestureDetector v2.4.zip
    35.3 KB · Views: 3,304
  • Java source - GestureDetector.zip
    5.5 KB · Views: 1,346
Last edited:

susu

Well-Known Member
Licensed User
Longtime User
Thank you so much.

This is my last free contribution. My next classes and libraries will be provided as donationware.

Yes, it's fair. I willing to donate to good libraries :D
 

susu

Well-Known Member
Licensed User
Longtime User
I wish your library can detect "pinch to zoom" action :)
 

Gargoyle

Member
Licensed User
Longtime User
Pretty useful. However... :)
Would it be possible to include a time element for the onScroll so one scroll would get processed rather than lots of scrolls which make up the whole scroll?

If that makes sense?
 

Informatix

Expert
Licensed User
Longtime User
Pretty useful. However... :)
Would it be possible to include a time element for the onScroll so one scroll would get processed rather than lots of scrolls which make up the whole scroll?

If that makes sense?

The scrolling in Android does not work this way. You can see with a ListView or a ScrollView that you can repeat finger moves and all of them are instantly taken into account. If the amount of these moves is greater than a certain level in a certain period of time, it's a fling gesture.
That being said, it's not difficult to wait until a certain time has elapsed before taking into account the scrolling amount:
B4X:
Dim t as Long
t = DateTime.Now
ScrlAmount = 0
B4X:
ScrlAmount = ScrlAmount + distanceX
B4X:
if DateTime.Now - t > Duration then
    t = DateTime.Now
    Use ScrlAmount
    ScrlAmount = 0
end if

A very important thing to know about scrolling: the scrolling event receiver must not be the view scrolled. In a ScrollView for example, the receiver is the ScrollView, the scrolled view is the panel content.
Moving an object with the finger is a drag. That's a different gesture and it needs only the Touch event.
 
Last edited:

Gargoyle

Member
Licensed User
Longtime User
The scrolling in Android does not work this way. You can see with a ListView or a ScrollView that you can repeat finger moves and all of them are instantly taken into account. If the amount of these moves is greater than a certain level in a certain period of time, it's a fling gesture.
That being said, it's not difficult to wait until a certain time has elapsed before taking into account the scrolling amount:
B4X:
Dim t as Long
t = DateTime.Now
ScrlAmount = 0
B4X:
ScrlAmount = ScrlAmount + distanceX
B4X:
if DateTime.Now - t > Duration then
    t = DateTime.Now
    Use ScrlAmount
    ScrlAmount = 0
end if

A very important thing to know about scrolling: the scrolling event receiver must not be the view scrolled. In a ScrollView for example, the receiver is the ScrollView, the scrolled view is the panel content.
Moving an object with the finger is a drag. That's a different gesture and it needs only the Touch event.

I've been looking at this and the Gestures Lib, mainly just for a couple of swipe actions. Haven't decided which lib to use as yet.

Did a similar thing as your code, but used a timer, in onScroll if timer was running - added to scroll amounts. In timer event logged distances.

Thanks.
 

Informatix

Expert
Licensed User
Longtime User
I've been looking at this and the Gestures Lib, mainly just for a couple of swipe actions. Haven't decided which lib to use as yet.

Did a similar thing as your code, but used a timer, in onScroll if timer was running - added to scroll amounts. In timer event logged distances.

Thanks.

For swipe actions, I can't see why you want to use the Gestures lib. It only handles multi-touch events. You won't know the fling velocity of your swipe gestures.
 

Gargoyle

Member
Licensed User
Longtime User
For swipe actions, I can't see why you want to use the Gestures lib. It only handles multi-touch events. You won't know the fling velocity of your swipe gestures.


Just a newbie, still learning lots, that's why I haven't decided yet;).

For this case I don't actually need the velocity, just the swipe gesture.

I've got a transparent button at edge of screen and want to use a swipe up/down to allow quickly changing a fontsize setting used in a webview.
So I only need the Swipe(Up/Down)

Thanks
 

Informatix

Expert
Licensed User
Longtime User
Just a newbie, still learning lots, that's why I haven't decided yet;).

For this case I don't actually need the velocity, just the swipe gesture.

I've got a transparent button at edge of screen and want to use a swipe up/down to allow quickly changing a fontsize setting used in a webview.
So I only need the Swipe(Up/Down)

Thanks

Swipe gesture = scroll + fling events (with LongPress disabled)

Here's the core gesture set supported by Android:
http://developer.android.com/design/patterns/gestures.html
 

Cableguy

Expert
Licensed User
Longtime User
Hi...

From reading the 'gestures' link provided above, it makes logic to me that even being a multi-touch gesture, including it within this lib would be a must have feature, thus concentrating the common gestures all in a single lib.
True that we can code the multi-touch lib to listen to this gesture, but using it along side with this lib is overkill... just my two cents
 

Informatix

Expert
Licensed User
Longtime User
Hi...

From reading the 'gestures' link provided above, it makes logic to me that even being a multi-touch gesture, including it within this lib would be a must have feature, thus concentrating the common gestures all in a single lib.
True that we can code the multi-touch lib to listen to this gesture, but using it along side with this lib is overkill... just my two cents

Yes, you're right. I'll add the multi-touch handling next week.
 

Cableguy

Expert
Licensed User
Longtime User

Informatix

Expert
Licensed User
Longtime User
v2.0

Finally I found some time today to release a version that supports multi-touch. It does not work with Android < 2 and is released as a donationware, as all my new projects. The previous version is still available as freeware.

New events:
onPointerDown, onPointerUp, onPinchOpen, onPinchClose, onRotation

New functions:
getPID, getPointerIndex, getPointerCount, IgnoreWidthForPinch, IgnoreHeightForPinch

All the documentation is in the contextual help of the SetOnGestureListener function.
 

bluedude

Well-Known Member
Licensed User
Longtime User
Great work as always

Hi,

Great stuff! A few requests, could you make it even more easier?

I guess lots of people are also interested in simple events/methods like:

- a left swipe is done;
- a right swipe is done;
- etc.

I know it can be done by monitoring the x1, y1 etc. but these kind of events would it make even more simpler.

In the end a combination with accelerometer would even be more fantastic. In the end a phone can be used/touched in many ways:

- screen swiped;
- phone rotated (upside down, straight up);
- shaken;
etc.

Basically this is all stuff where physical touch is involved and one library covering it all would make sense in my opinion.

I know, all this stuff can already be done by combining pieces of code from all over the forum.
 

Informatix

Expert
Licensed User
Longtime User
I guess lots of people are also interested in simple events/methods like:

- a left swipe is done;
- a right swipe is done;
- etc.

I know it can be done by monitoring the x1, y1 etc. but these kind of events would it make even more simpler.

In the end a combination with accelerometer would even be more fantastic. In the end a phone can be used/touched in many ways:

- screen swiped;
- phone rotated (upside down, straight up);
- shaken;
etc.

Basically this is all stuff where physical touch is involved and one library covering it all would make sense in my opinion.

To detect the swipe gesture, use the onScroll event. Use only the distanceX value if you want to detect a horizontal gesture. I think that users don't need a function to do "if distanceX > 0 then".

The accelerometer and the other sensors should be handled by a specific library. I won't never include them in this library because its only purpose is to handle finger gestures. I don't want it to become a bloatware.

That reminds me I have to include the drag event. I forgot it. After that, I think that the library will be complete.
 
Top