Android gesture Process Analysis

Source: Internet
Author: User

SummaryFirst, 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. motionevent object forwarding through gesturedetector

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 use a code example to demonstrate the implementation of gesture interaction, so that everyone can have a deeper understanding and memory of the execution sequence of the above and the distinction between gesture actions.

First, provide a layout file named main. XML with only imageview.

1234567891011 <?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:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {
  // Create a gesturedetector object waiyuwu.blogcn.com
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]);
}   // 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
; }   // Method for calling a girlpublic void goPrevious(){
index--; index = Math.abs(index % girls.length);
image.setImageResource(girls[index]);
}   // 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.

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.