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

TheMightySwe

Active Member
Licensed User
Longtime User
Thanks anyway, my objective for now is meet. Love it, now the resizing problem needs to be solved, because the GestureListner is not Dimmed in Globals, but it should work with some kind of workaround there.

But that is no hurry with that, lots of things more importnat for the moment.

Once again, Thanks!
 

luke2012

Well-Known Member
Licensed User
Longtime User
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.

How to catch a left or right scroll ?
 

RayLee

Member
Licensed User
Longtime User
I'm sorry to get an error message, when I tested the project of 'GestureDetector_Dispatch'.

If i click the green panel, it raises :

B4X:
Sub GD_Green_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Log("GREEN_Touch action=" & Action & ", x=" & X & ", y=" & Y & ", ev=" & MotionEvent)
    Return GD_Green.PassTouchEventTo(MotionEvent, pnlRed)
End Sub

GREEN_Touch action=0, x=119, y=94, ev=MotionEvent{b66f62a0 action=0 x=119.0 y=94.0 pressure=1.0 size=0.0}


Unexpected event (missing RaiseSynchronousEvents): gd_red_ontouch
Check the unfiltered logs for the full stack trace.
Error occurred on line: 61 (main)


java.lang.NullPointerException
at flm.b4a.gesturedetector.GestureDetectorForB4A$1.onTouch(GestureDetectorForB4A.java:111)
at android.view.View.dispatchTouchEvent(View.java:3881)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
at flm.b4a.gesturedetector.GestureDetectorForB4A.PassTouchEventTo(GestureDetectorForB4A.java:412)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at flm.b4a.gesturedetector.GestureDetectorForB4A$1.onTouch(GestureDetectorForB4A.java:111)
at android.view.View.dispatchTouchEvent(View.java:3881)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)

OP: android 2.3
 

Informatix

Expert
Licensed User
Longtime User
I'm sorry to get an error message, when I tested the project of 'GestureDetector_Dispatch'.

If i click the green panel, it raises :

B4X:
Sub GD_Green_onTouch(Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean
    Log("GREEN_Touch action=" & Action & ", x=" & X & ", y=" & Y & ", ev=" & MotionEvent)
    Return GD_Green.PassTouchEventTo(MotionEvent, pnlRed)
End Sub

GREEN_Touch action=0, x=119, y=94, ev=MotionEvent{b66f62a0 action=0 x=119.0 y=94.0 pressure=1.0 size=0.0}


Unexpected event (missing RaiseSynchronousEvents): gd_red_ontouch
Check the unfiltered logs for the full stack trace.
Error occurred on line: 61 (main)


java.lang.NullPointerException
at flm.b4a.gesturedetector.GestureDetectorForB4A$1.onTouch(GestureDetectorForB4A.java:111)
at android.view.View.dispatchTouchEvent(View.java:3881)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
at flm.b4a.gesturedetector.GestureDetectorForB4A.PassTouchEventTo(GestureDetectorForB4A.java:412)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:485)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:229)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:174)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:93)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:154)
at flm.b4a.gesturedetector.GestureDetectorForB4A$1.onTouch(GestureDetectorForB4A.java:111)
at android.view.View.dispatchTouchEvent(View.java:3881)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:903)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:869)

OP: android 2.3
It's probably due to the Rapid Debugger mode. Try to use the Legacy mode instead.
 

Swissmade

Well-Known Member
Licensed User
Longtime User
I wonder if CreateMotionEvent in version 2.1 can be used in a image stream.

Cheers,
Swissmade
 

Swissmade

Well-Known Member
Licensed User
Longtime User
I will,

Motion detection on Images when something is moving.
Images from a IP-Camera.
 

Swissmade

Well-Known Member
Licensed User
Longtime User
Ok thanks,

Sorry to not read all of the post.
Very busy here to read all.

Thanks for help

Cheers,
Swissmade
 

kiki78

Active Member
Licensed User
Longtime User
Hello Informatix,

I've made custom view with B4A class and I want add gesture detection on it.
Is it possible and how to receive event inside class ?

Regards
 

kiki78

Active Member
Licensed User
Longtime User
Sorry I make mistake with eventPrefix parameter and so I never receive events :oops:.

Regards
 

kiki78

Active Member
Licensed User
Longtime User
Thank you Informatix.
Since I corrected eventPrefix all work fine :)

Regards
 

kiki78

Active Member
Licensed User
Longtime User
Hello Informatix,

I try to use "onLongPress" event.
This event work, but when I want to use it to show preference screen, I obtain following error :
B4X:
java.lang.NullPointerException
    at flm.b4a.gesturedetector.GestureDetectorForB4A$1.onTouch(GestureDetectorForB4A.java:89)
    at android.view.View.dispatchTouchEvent(View.java:7345)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2464)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2197)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2470)
    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2212)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2151)
    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1480)
    at android.app.Activity.dispatchTouchEvent(Activity.java:2469)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2099)
    at android.view.View.dispatchPointerEvent(View.java:7535)
    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3492)
    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3424)
    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4534)
    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4512)
    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4616)
    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:125)
    at android.os.Looper.loop(Looper.java:124)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
    at dalvik.system.NativeStart.main(Native Method)

Have you some explanations or advises ?

Regards
 
Top