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,276
  • Java source - GestureDetector.zip
    5.5 KB · Views: 1,324
Last edited:

TelKel81

Active Member
Licensed User
How should I transition from a LongPress to "immediately being able to drag" right after the long press ?
The goal is to drag a view towards a new parent after a long press.
 
Last edited:

tufanv

Expert
Licensed User
Longtime User
I have a little problem here. I bind the gesture detector to scrollview. My scrollview has panels like a listview and each have a click eventwhich navigates to another page. I can get swipe msgbox When I swipe scrollview where there is no panel but if there are panels at the area where I swipe, I dont get messages and everytime panel inside the scrollview click event triggers. How can I solve this?
 

frutuopa

Member
Licensed User
Longtime User
Hello ... I've been trying this library and it looks really fantastic .....
I wanted to take advantage of it to create dynamic buttons and be able to drag them over a picture ....

Thanks

Sub Globals
Private pnlFilho2 As Panel
Dim GD As GestureDetector
end sub
Sub Activity_Create(FirstTime As Boolean)

For b=0 To 2

Dim btnDrag As Button
btnDrag.Initialize("btnDrag" & b)

pnlFilho2.AddView(btnDrag,5%x+b*50 , pnlFilho2.Height*0.8, 40dip,40dip)
btnDrag.Text = "B" & (b+1)
btnDrag.TAG = "t" & b
btnDrag.Color=Colors.Yellow
btnDrag.TextColor = Colors.Black

btnDrag.Visible=True
GD.SetOnGestureListener(btnDrag, "Gesture")
Next

end sub

Sub Gesture_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
Log("onTouch action=" & Action & ", x=" & X & ", y=" & Y & ", ev=" & MotionEvent)
Return True
End Sub
Sub Gesture_onDown(X As Float, Y As Float, MotionEvent As Object)
Log(" onDown x = " & X & ", y = " & Y & ", ev = " & MotionEvent)
Log(" pressure = " & GD.getPressure(MotionEvent, 0))
Log(" size = " & GD.getSize(MotionEvent, 0))
End Sub
Sub Gesture_onDrag(deltaX As Float, deltaY As Float, MotionEvent As Object)
Log(" onDrag deltaX = " & deltaX & ", deltaY = " & deltaY & ", ev = " & MotionEvent)
Dim butdrag As Button
butdrag = Sender
Log(butdrag.tag)
butdrag.Left = Max(0, Min(butdrag.Left + deltaX, 100%x - butdrag.Width))
butdrag.Top = Max(0, Min(butdrag.Top + deltaY, 100%y - butdrag.Height))
End Sub
Sub Gesture_onFling(velocityX As Float, velocityY As Float, MotionEvent1 As Object, MotionEvent2 As Object)
Log(" onFling velocityX = " & velocityX & ", velocityY = " & velocityY & ", ev1 = " & MotionEvent1 & ", ev2 = " & MotionEvent2)
End Sub
 

ehsan211

New Member
Hello ... I've been trying this library and it looks really fantastic .....
I wanted to take advantage of it to create dynamic buttons and be able to drag them over a picture ....

Thanks

Sub Globals
Private pnlFilho2 As Panel
Dim GD As GestureDetector
end sub
Sub Activity_Create(FirstTime As Boolean)

For b=0 To 2

Dim btnDrag As Button
btnDrag.Initialize("btnDrag" & b)

pnlFilho2.AddView(btnDrag,5%x+b*50 , pnlFilho2.Height*0.8, 40dip,40dip)
btnDrag.Text = "B" & (b+1)
btnDrag.TAG = "t" & b
btnDrag.Color=Colors.Yellow
btnDrag.TextColor = Colors.Black

btnDrag.Visible=True
GD.SetOnGestureListener(btnDrag, "Gesture")
Next

end sub

Sub Gesture_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
Log("onTouch action=" & Action & ", x=" & X & ", y=" & Y & ", ev=" & MotionEvent)
Return True
End Sub
Sub Gesture_onDown(X As Float, Y As Float, MotionEvent As Object)
Log(" onDown x = " & X & ", y = " & Y & ", ev = " & MotionEvent)
Log(" pressure = " & GD.getPressure(MotionEvent, 0))
Log(" size = " & GD.getSize(MotionEvent, 0))
End Sub
Sub Gesture_onDrag(deltaX As Float, deltaY As Float, MotionEvent As Object)
Log(" onDrag deltaX = " & deltaX & ", deltaY = " & deltaY & ", ev = " & MotionEvent)
Dim butdrag As Button
butdrag = Sender
Log(butdrag.tag)
butdrag.Left = Max(0, Min(butdrag.Left + deltaX, 100%x - butdrag.Width))
butdrag.Top = Max(0, Min(butdrag.Top + deltaY, 100%y - butdrag.Height))
End Sub
Sub Gesture_onFling(velocityX As Float, velocityY As Float, MotionEvent1 As Object, MotionEvent2 As Object)
Log(" onFling velocityX = " & velocityX & ", velocityY = " & velocityY & ", ev1 = " & MotionEvent1 & ", ev2 = " & MotionEvent2)
End Sub
hello . its my question Too . i try different method but cant solve it
 
This library is really what I have even wanted to achieve. I would be able to monitor finger movements, angle and even pressure strenght on some display place. But I have one programmers plea to all of us.

Because I do not see at all, I AM dependent on Android screen readerandEspecially on Explore by touch Android build in service. This service is not The part of GOogle Accessibility suite. It is Android service which can be called by every app. But it was strongdisadvantage. If Explore by touch service is being actively used, libraryes or code, which detect gestures related feetback can not work.Does somebody of us think,that this long time limitation of ANdroid could be overcomed on future Android versions? Now, visually impaired user must always suspend Android accessibilitysuite or disable Explore by touch feature of this screen reader. In this case, screen reader will never interact with Explore by touch Android service. In this case user can use what ever app he or she is wanting even with gestures feetback.

But one developer from our country have invented one work around. His app can detect, if visually impaired user has used two or three fingers and even The direction of those finger tips movement. I do notknow, if is it because Android accessibility suite export some external APIS of if Explore by touch service allow this.
COuld somebody look at this problem related to conflict between Explore by touch Android service and other apps, which would want to get gestures feetback?
Thank you very very much and thank You for yours very useful library.
 
Top