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

Last edited:
How can I detect draging up,down,left,right ?

What i need is that, in onDrag example I need to drag the ball up,down,right,left only (like a joystick that goes like a + symbol only) .

How can i achieve it?
 
By checking the direction of the move (the sign).
How can i get direction?

Code:
Sub Gesture_onDrag(deltaX As Float, deltaY As Float, MotionEvent As Object)
    likeBt.Top = Max(25%y+(actionBar.Height/2), Min(likeBt.Top + deltaY, 75%y+(actionBar.Height/2) - likeBt.Height))
End Sub

Sub Gesture_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Select Action
        Case Activity.ACTION_DOWN
            'Log("Action Down")
          
        Case Activity.ACTION_UP
            'Log("Action Up")
            If likeBt.Top <> 25%y+(actionBar.Height/2) Or likeBt.Top <> 75%y+(actionBar.Height/2)-likeBt.Height Then
                likeBt.Top = 50%y+(actionBar.Height/2)- 25dip
            End If
          
    End Select
  
    Return True
End Sub

this is my current snippet

this works for vertical direction only
 
How can i get direction?

Code:
Sub Gesture_onDrag(deltaX As Float, deltaY As Float, MotionEvent As Object)
    likeBt.Top = Max(25%y+(actionBar.Height/2), Min(likeBt.Top + deltaY, 75%y+(actionBar.Height/2) - likeBt.Height))
End Sub

Sub Gesture_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Select Action
        Case Activity.ACTION_DOWN
            'Log("Action Down")
         
        Case Activity.ACTION_UP
            'Log("Action Up")
            If likeBt.Top <> 25%y+(actionBar.Height/2) Or likeBt.Top <> 75%y+(actionBar.Height/2)-likeBt.Height Then
                likeBt.Top = 50%y+(actionBar.Height/2)- 25dip
            End If
         
    End Select
 
    Return True
End Sub

this is my current snippet

this works for vertical direction only
Check the sign of DeltaX and DeltaY and the relative value of DeltaX (horizontal) to the value of DeltaY (vertical).
 
I am testing OnDrag event and it works great. But at the end of the drag, how can I detect when the user lifts his finger off the screen so that I can "drop" the view back to its initial position? With OnTouch?
 
Just to report that today I noticed a new behaviour (in an Android 5.1 smartwatch type S83) whereby an "Action=3" event occurs with the Gesture Detector. Normally Action=1 should occur (=Action_Up).
It only happens when swiping left to right. In all other directions the normal Action_Up event occurs. Is this an electronic (firmware) fault in this smart watch?
3 = Cancel. The touch event is cancelled by one of the activity views that catches this event.
 
Hello.
The lib have a function to pass touch events like
Code:
GD_Green.PassTouchEventTo(MotionEvent, pnlRed)

are a way to pass event like

Code:
Gesture_onScroll(distanceX As Float, distanceY As Float, MotionEvent1 As Object, MotionEvent2 As Object)

The idea is intercept vertical swipes and pass de horizontal events.
A problem than i don't know how to resolve.

Regards.
 
Hello,

I added imageview into panel and trying resize and rotate image with pinch on runtime.

I have 2 problems

1- Too Slowly. Users wait 1 second after each pinch action.
2-Panel can rotate but Imageview in panel doesnt rotate with panel. How to rotate imageview as panel

Need help.

My code : www.amerikaileticaretyapin.com/pinch.zip
 
Last edited:
1- Too Slowly. Users wait 1 second after each pinch action.

Test it on Release. On my Samsung S7 Edge i have no delay from 1 second.

2-Panel can rotate but Imageview in panel doesnt rotate with panel. How to rotate imageview as panel

set:
Code:
pnlTwoFingers.AddView(img, 0, 0, 100%x, 100%x)
 
are a way to pass event like
Do you mean something like that?

Code:
Sub Globals
      Private pnl_back As Panel
    Dim GD As GestureDetector    
End Sub

Sub Gesture_onDrag(deltaX As Float, deltaY As Float, MotionEvent As Object)   
    pnl_back.Top = Max(0, Min(pnl_back.Top + deltaY, 100%y - pnl_back.Height))
End Sub

if you press on your view and wish up or down, your view follow your finger.
 
2-Panel can rotate but Imageview in panel doesnt rotate with panel. How to rotate imageview as panel
Your code does not rotate anything except a text drawn on a canvas, so the result is not surprising.
You have to use a library like NineOldAndroids that allows to rotate views.

Are you sure that your need is not already satisfied by an existing library (e.g. TouchImageView or PinchZoomAndMove)?
 
Your code does not rotate anything except a text drawn on a canvas, so the result is not surprising.
You have to use a library like NineOldAndroids that allows to rotate views.

Are you sure that your need is not already satisfied by an existing library (e.g. TouchImageView or PinchZoomAndMove)?

Hello,

Im not sure. ı need drag few images on activity and pinch them on runtime. I thought this library best for this.

am i wrong ?
 
Test it on Release. On my Samsung S7 Edge i have no delay from 1 second.



set:
Code:
pnlTwoFingers.AddView(img, 0, 0, 100%x, 100%x)

1-It works great with release mode.
2- it doesnt help.
Code:
pnlTwoFingers.AddView(img, 0, 0, 100%x, 100%x)

thank you
 
how to use this library for google map
i want to detect touch actions on the map by this library
 
Back
Top