Understanding Android Gesture Recognition to improve APP user experience

Source: Internet
Author: User

For touch screens, the native messages are nothing more than pressing, lifting, and moving. We only need to simply reload onTouch or set the touch listener setOnTouchListener for processing. However, to improve the user experience of our apps, sometimes we need to recognize users' gestures. The gesture recognition tool GestureDetector provided by Android can help us a lot.

Basic
The working principle of GestureDetector is that when we receive a user touch message, we send the message to GestureDetector for processing. We get the GestureDetector's processed gesture by setting the listener.

GestureDetector provides two listener interfaces. OnGestureListener processes clickable messages and OnDoubleTapListener processes double-click messages.

The OnGestureListener interfaces include the following:
// Click. triggered immediately when the touch screen is pressed
Abstract boolean onDown (MotionEvent e );
// Lift, triggered when your finger leaves the touch screen (this gesture is not triggered when you press, scroll, or slide)
Abstract boolean onSingleTapUp (MotionEvent e );
// Short press, the touch screen is lifted after a moment, will trigger this gesture, if quickly lifted will not
Abstract void onShowPress (MotionEvent e );
// Long press, the touch screen is neither lifted nor moved after being pressed, triggered after a period of time
Abstract void onLongPress (MotionEvent e );
// Scroll, move after the touch screen is pressed
Abstract boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY );
// Slide. After the touch screen is pressed, it moves and lifted quickly. The scroll gesture is triggered first, followed by a sliding gesture.
Abstract boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY );
OnDoubleTapListener interfaces include the following:
// Double-click, triggered when the finger quickly clicks the second button on the touch screen
Abstract boolean onDoubleTap (MotionEvent e );
// Trigger each push by double-clicking
Abstract boolean onDoubleTapEvent (MotionEvent e );
// Click confirm, that is, quickly press and raise, but does not continuously click the second
Abstract boolean onSingleTapConfirmed (MotionEvent e );
Sometimes we don't need to deal with all the above gestures. For convenience, Android provides another class SimpleOnGestureListener to implement the above interface. We only need to inherit SimpleOnGestureListener and then reload the gestures of interest.

Application
STEP 1: Create a gesture listening object Copy codeThe Code is as follows: package noodies.blog.csdn.net;
Import android. content. Context;
Import android. view. MotionEvent;
Import android. view. GestureDetector. SimpleOnGestureListener;
Import android. widget. Toast;
Public class MyGestureListener extends SimpleOnGestureListener {
Private Context mContext;
MyGestureListener (Context context ){
MContext = context;
}
@ Override
Public boolean onDown (MotionEvent e ){
Toast. makeText (mContext, "DOWN" + e. getAction (), Toast. LENGTH_SHORT). show ();
Return false;
}
@ Override
Public void onShowPress (MotionEvent e ){
Toast. makeText (mContext, "SHOW" + e. getAction (), Toast. LENGTH_SHORT). show ();
}
@ Override
Public boolean onSingleTapUp (MotionEvent e ){
Toast. makeText (mContext, "single up" + e. getAction (), Toast. LENGTH_SHORT). show ();
Return false;
}
@ Override
Public boolean onScroll (MotionEvent e1, MotionEvent e2,
Float distanceX, float distanceY ){
Toast. makeText (mContext, "SCROLL" + e2.getAction (), Toast. LENGTH_SHORT). show ();
Return false;
}
@ Override
Public void onLongPress (MotionEvent e ){
Toast. makeText (mContext, "LONG" + e. getAction (), Toast. LENGTH_SHORT). show ();
}
@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
Float velocityY ){
Toast. makeText (mContext, "FLING" + e2.getAction (), Toast. LENGTH_SHORT). show ();
Return false;
}
@ Override
Public boolean onDoubleTap (MotionEvent e ){
Toast. makeText (mContext, "DOUBLE" + e. getAction (), Toast. LENGTH_SHORT). show ();
Return false;
}
@ Override
Public boolean onDoubleTapEvent (MotionEvent e ){
Toast. makeText (mContext, "double event" + e. getAction (), Toast. LENGTH_SHORT). show ();
Return false;
}
@ Override
Public boolean onSingleTapConfirmed (MotionEvent e ){
Toast. makeText (mContext, "single conf" + e. getAction (), Toast. LENGTH_SHORT). show ();
Return false;
}
}

STEP 2: Set Gesture Recognition
We can set Gesture Recognition in Activity:Copy codeThe Code is as follows: package noodies.blog.csdn.net;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. GestureDetector;
Import android. view. MotionEvent;
Public class GestureTestActivity extends Activity {
Private GestureDetector mGestureDetector;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MGestureDetector = new GestureDetector (this, new MyGestureListener (this ));
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Return mGestureDetector. onTouchEvent (event );
}
}

You can also set Gesture Recognition in the Custom View:
Copy codeThe Code is as follows: package noodies.blog.csdn.net;
Import android. content. Context;
Import android. util. AttributeSet;
Import android. view. GestureDetector;
Import android. view. MotionEvent;
Import android. view. View;
Public class MyView extends View {
Private GestureDetector mGestureDetector;
Public MyView (Context context, AttributeSet attrs ){
Super (context, attrs );
MGestureDetector = new GestureDetector (context, new MyGestureListener (context ));
SetLongClickable (true );
This. setOnTouchListener (new OnTouchListener (){
Public boolean onTouch (View v, MotionEvent event ){
Return mGestureDetector. onTouchEvent (event );
}
});
}
}

Traps
For custom views, two traps in gesture recognition may waste a lot of time.
1: You must set longClickable to true for the View. Otherwise, gesture recognition cannot work correctly. Only the Down, Show, and Long gestures are returned.
2: gesture recognition must be called in onTouchListener of the View, but onTouchEvent cannot be reloaded like Activity; otherwise, the same gesture recognition cannot work correctly.

Test Results
The following is the gesture sequence returned by various operations. The value 0 indicates that the touch screen is pressed, and the value 1 indicates that the touch screen is lifted.
Click: down 0, single up 1, single conf 0
Short Press: down 0, show 0, single up 1
Long press: down 0, show 0, long 0
Double-click: down 0, single up 1, double 0, double event 0, down 0, double event 1
Scroll: down 0, (show 0), scrool 2...
Slide: down 0, (show 0), scrool 2..., fling 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.