Implementation of gesture Interaction Based on Android

Source: Internet
Author: User

Having nothing to worry about, pondering the gesture interaction in Android, I found that there are not many articles on gestures on the Internet, and many references are of little value. This blog post is shared with you. In view of the fact that I did not have a deep research on Gesture interaction when I wrote this blog post, if there are any errors, I would like to ask you to criticize and correct them.

First, in the Android system, each gesture interaction is executed in the following order.

1. A MotionEvent event is triggered when you touch the screen.

2. The event is monitored by OnTouchListener and the MotionEvent object is obtained in the onTouch () method.

3. Forward the MotionEvent object to OnGestureListener through GestureDetector.

4. OnGestureListener obtains the object and makes appropriate feedback based on the encapsulated information of the object.

This sequence can be said to be the principle of gesture interaction. Let's take a look at MotionEvent, GestureDetector, and OnGestureListener.

MotionEvent: this class is used to encapsulate action events such as gestures, touch pens, and trackballs. It encapsulates two important attributes X and Y, which are used to record the coordinates of the horizontal and vertical axes respectively.

GestureDetector: identifies various gestures.

OnGestureListener: This is a listener interface for gesture interaction. It provides multiple Abstract methods and calls corresponding methods based on GestureDetector's gesture recognition results.

Next, I will switch between beautiful pictures (and beautiful pictures again. Let's take a look at the sample code to demonstrate the implementation of gesture interaction, so that everyone can have a deeper understanding and memory of the execution sequence above and the distinction between gesture actions.

First, provide a layout file named main. xml with only ImageView.

Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: orientation = "vertical" android: layout_width = "fill_parent" android: layout_height = "fill_parent">
<ImageView android: id = "@ + id/image" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: layout_gravity = "center"/>
</LinearLayout>

Then, complete our Activity. To listen to touch events and gesture time on the touch screen, the Activity must implement the OnTouchListener and OnGestureListener interfaces and rewrite the methods. The Code is as follows:Copy codeThe Code is as follows: public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {

// Create a GestureDetector object to identify and tidy up
Private GestureDetector detector = new GestureDetector (this );
// Define an array for beautiful girls
Int [] girls = new int [] {R. drawable. girl1, R. drawable. girl2, R. drawable. girl3 };
// Define the array subscript for easy viewing of girls
Private int index;
Private ImageView image;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

Image = (ImageView) findViewById (R. id. image );
// Set an initial girl.
Image. setImageResource (girls [index]);
// Listen to the touch screen time on this ImageView component
Image. setOnTouchListener (this );
// Remember to set the following two items. Otherwise, you won't be able to handle events other than the touch, such as throwing.
Image. setLongClickable (true );
Detector. setIsLongpressEnabled (true );
}

// Method used to shout for the next girl
Public void goNext (){
Index ++;
Index = Math. abs (index % girls. length );
Image. setImageResource (girls [index]);
}

// Method for calling a girl
Public void goPrevious (){
Index --;
Index = Math. abs (index % girls. length );
Image. setImageResource (girls [index]);
}

// Override the onTouch method of OnTouchListener
// This method is called when a touch screen is touched, that is, a touch event (touch and touch) is triggered.
@ Override
Public boolean onTouch (View v, MotionEvent event ){
Detector. onTouchEvent (event );
Return true;
}

// Called when you press the action
@ Override
Public boolean onDown (MotionEvent e ){
Return false;
}

// Called during throwing
@ Override
Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX,
Float velocityY ){
// VelocityX indicates horizontal movement, and the girl is switched Based on the direction of the finger movement.
If (velocityX <0 ){
GoNext ();
} Else if (velocityX> 0 ){
GoPrevious ();
}
Return false;
}

// Called on time
@ Override
Public void onLongPress (MotionEvent e ){
}

// Called during scrolling
@ Override
Public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX,
Float distanceY ){
Return false;
}

// Called during press and hold
@ Override
Public void onShowPress (MotionEvent e ){
}

// Called when lifted
@ Override
Public boolean onSingleTapUp (MotionEvent e ){
Return false;
}
}

When I first started learning Android, I felt that Google's documents were not the same. When I was studying gestures, I felt that Google's documents were too poorly written. Many constants, attributes, and methods are not even described. There is no description, but there are so many gestures in OnGestureListener, and there is no description. Who can understand onLongPress and onShowPress before trying them continuously, is the relationship and difference between onScroll and onFling? Google really needs to perform a major operation on the documentation. However, after repeated attempts. These gesture actions are defined from an individual's perspective.

Press (onDown): the moment when the finger touched the touch screen, it was the touch.
OnFling: the action of quickly moving and releasing fingers on the touch screen.
OnLongPress: the finger is pressed for a period of time and has not been released.
OnScroll: slide your finger on the touch screen.
OnShowPress: the finger is pressed on the touch screen, and its time range is effective after being pressed.
OnSingleTapUp: the moment when your finger leaves the touch screen.

In addition to these definitions, I have also summarized some experience and shared it with you here.

Any gesture action will first execute the onDown action.
Before the onLongPress action, you must execute the onShowPress action.
After the onShowPress action and the onDown action, the onSingleTapUp action is executed.
The onSingleTapUp action is not executed after the onLongPress, onScroll, and onFling actions.

Here, it is generally over. The rest is to check the running results together.

I. No throwing:

2. throw to the right

3. Roll again to the right

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.