B4A Library [Lib] Gesture Detector

Discussion in 'Additional libraries, classes and official updates' started by Informatix, Sep 20, 2012.

  1. Informatix

    Informatix Expert Licensed User

    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.
     

    Attached Files:

    Last edited: Oct 2, 2017
  2. susu

    susu Well-Known Member Licensed User

    Thank you so much.

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

    Informatix Expert Licensed User

    New version 1.01

    I added the Touch event. Not really needed, but you can't set its usual listener when you use the library.
     
  4. susu

    susu Well-Known Member Licensed User

    I wish your library can detect "pinch to zoom" action :)
     
  5. Informatix

    Informatix Expert Licensed User

    It's not a library for multi-touch gestures. There's already a library for that : Gestures.

    EDIT: this is no longer true. My lib replaces completely the Gestures lib.
     
    Last edited: May 16, 2013
  6. Gargoyle

    Gargoyle Member Licensed 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?
     
  7. Informatix

    Informatix Expert Licensed 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:
    Code:
    Dim t as Long
    t = 
    DateTime.Now
    ScrlAmount = 
    0
    Code:
    ScrlAmount = ScrlAmount + distanceX
    Code:
    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: Sep 21, 2012
    Peter Simpson likes this.
  8. Informatix

    Informatix Expert Licensed User

    I said in a previous post that my library wasn't for multi-touch gestures. I want to specify that you can handle multi-touch gestures with it, because you get all motion events, but that's not as easy as a dedicated library like Gestures.
     
    Peter Simpson likes this.
  9. Gargoyle

    Gargoyle Member Licensed 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.
     
  10. Informatix

    Informatix Expert Licensed 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.
     
  11. Gargoyle

    Gargoyle Member Licensed 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
     
  12. Informatix

    Informatix Expert Licensed User

    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
     
  13. Cableguy

    Cableguy Expert Licensed 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
     
    Peter Simpson likes this.
  14. Informatix

    Informatix Expert Licensed User

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

    Cableguy Expert Licensed User

    Thanks for all your efforts in helping this comunity grow.
     
  16. Informatix

    Informatix Expert Licensed 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.
     
  17. bluedude

    bluedude Well-Known Member Licensed 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.
     
  18. Informatix

    Informatix Expert Licensed 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.
     
    luke2012 likes this.
  19. vb1992

    vb1992 Well-Known Member Licensed User

    bluedude +1
     
  20. Informatix

    Informatix Expert Licensed User

    New version

    v2.01:
    I added the onDrag event, with a new demo.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice