AndroidUIGestureRecognizer


Source link: https://github.com/sephiroth74/AndroidUIGestureRecognizer

Android UIGestureRecognizer


AndroidGestureRecognizer is an Android implementation of the Apple's UIGestureRecognizer framework. https://developer.apple.com/reference/uikit/uigesturerecognizer

For more info about the ui gesture recognizers look this WWDC video https://developer.apple.com/videos/play/wwdc2012/233/

From Apple API reference:

UIGestureRecognizer is an abstract base class for concrete gesture-recognizer classes. A gesture-recognizer object—or, simply, a gesture recognizer—decouples the logic for recognizing a gesture and acting on that recognition. When one of these objects recognizes a common gesture or, in some cases, a change in the gesture, it sends an action message to each designated target object.

Available Recognizers

UIGestureRecognizer that looks for single or multiple taps. For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.

Pinching is a continuous gesture. The gesture begins (began) when the two touches have moved enough to be considered a pinch gesture. The gesture changes (changed) when a finger moves (with both fingers remaining pressed). The gesture ends (ended) when both fingers lift from the view.

Rotation is a continuous gesture. It begins when two touches have moved enough to be considered a rotation. The gesture changes when a finger moves while the two fingers are down. It ends when both fingers have lifted. At each stage in the gesture, the gesture recognizer sends its action message.

UISwipeGestureRecognizer recognizes a swipe when the specified number of touches (numberOfTouchesRequired) have moved mostly in an allowable direction (direction) far enough to be considered a swipe. Swipes can be slow or fast. A slow swipe requires high directional precision but a small distance; a fast swipe requires low directional precision but a large distance.

A panning gesture is continuous. It begins (began) when the minimum number of fingers allowed (minimumNumberOfTouches) has moved enough to be considered a pan. It changes (changed) when a finger moves while at least the minimum number of fingers are pressed down. It ends (ended) when all fingers are lifted.

Long-press gestures are continuous. The gesture begins (began) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (ended) when any of the fingers are lifted.

Discrete vs Continuous

The gesture interpreted by a gesture recognizer can be either discrete or continuous. A discrete gesture, such as a double tap, occurs but once in a multi-touch sequence and results in a single action sent. However, when a gesture recognizer interprets a continuous gesture such as a rotation gesture, it sends an action message for each incremental change until the multi-touch sequence concludes. (from https://developer.apple.com/reference/uikit/uigesturerecognizer)

There are 2 types of UI GestureRecognizers: UIContinuousRecognizer and UIDiscreteGestureRecognizer

UIDiscreteGestureRecognizer

Gesture Recognizers that implement this interface will only fire the Ended state change. Internally they will switch between Possible, Ended, Failed or Cancelled state.

UIContinuousRecognizer

A continuous gesture which will dispatch Began, Changed and Ended state changed events (for instance a pinch gesture, or a rotate gesture).

Installation

The library is currently under heavy development, so there are no stable artifacts so far. I'm publishing snapshots to the sonatype repository at this time. In order to use it, you need to have the sonatype url inside your repositories container:

repositories {

  maven {
 url uri("https://oss.sonatype.org/content/repositories/snapshots") 
}
 
}
 

Then add the library dependency:

compile 'it.sephiroth.android.library.uigestures:uigesture-recognizer:1.0.0-r1-SNAPSHOT' 

Example

 public class MainActivity extends AppCompatActivity

implements UIGestureRecognizer.OnActionListener, UIGestureRecognizerDelegate.Callback {

 @Override

protected void onCreate(Bundle savedInstanceState) {

 ...

  final UIGestureRecognizerDelegate delegate = new UIGestureRecognizerDelegate(null);

  // optional, override the delegate behaviors

 delegate.setCallback(this);

  // single tap gesture

 UITapGestureRecognizer recognizer1 = new UITapGestureRecognizer(this);

 recognizer1.setNumberOfTapsRequired(1);

 recognizer1.setNumberOfTouchesRequired(1);

 recognizer1.setTag("single-tap");

 recognizer1.setActionListener(this);

  // double tap gesture

 UITapGestureRecognizer recognizer2 = new UITapGestureRecognizer(this);

 recognizer2.setTag("double-tap");

 recognizer2.setNumberOfTapsRequired(2);

 recognizer2.setNumberOfTouchesRequired(1);

 recognizer2.setActionListener(this);

  // We want to recognize a single tap and a double tap separately. Normally, when the user

 // performs a double tap, the single tap would be triggered twice.

 // In this way, however, the single tap will wait until the double tap will fail. So a single tap

 // and a double tap will be triggered separately.

 recognizer1.requireFailureOf(recognizer2);

  // add both gestures to the delegate

 delegate.addGestureRecognizer(recognizer);

 delegate.addGestureRecognizer(recognizer2);

  // forward the touch events to the delegate

 findViewById(R.id.root).setOnTouchListener(new View.OnTouchListener() {

  @Override

  public boolean onTouch(final View view, final MotionEvent motionEvent) {

return delegate.onTouchEvent(view, motionEvent);

  
}

 
}
);

}

 // ui gesture recognizer event callback

@Override

public void onGestureRecognized(@NonNull final UIGestureRecognizer recognizer) {

 Log.d(getClass().getSimpleName(), "onGestureRecognized(" + recognizer + "). state: " + recognizer.getState());

}

 // delegate methods

 /** 

* @see https://developer.apple.com/reference/uikit/uigesturerecognizerdelegate/1624213-gesturerecognizershouldbegin 

*/

@Override

public boolean shouldBegin(final UIGestureRecognizer recognizer) {

 return true;

}

 /** 

* @see https://developer.apple.com/reference/uikit/uigesturerecognizerdelegate/1624208-gesturerecognizer 

*/

@Override

public boolean shouldRecognizeSimultaneouslyWithGestureRecognizer(

 final UIGestureRecognizer current, final UIGestureRecognizer recognizer) {

 return true;

}

 /** 

* @see https://developer.apple.com/reference/uikit/uigesturerecognizerdelegate/1624214-gesturerecognizer 

*/

@Override

public boolean shouldReceiveTouch(final UIGestureRecognizer recognizer) {

 return true;

}

  

JavaDocs

JavaDocs are available here: javadoc.zip

Resources

BadgedView allows you show a badge into any View.

PianoView provides a ViewPager Indicator looks like piano's keyboard

A custom time picker library for Android.

This is quite simple toast library, that make it easier to show and create custom toast.

Custom EditTextView to show/hide password with customizable icon.

Cube grid animation for Android.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes