OnTouchEvent method, ontouchevent Method

Source: Internet
Author: User

(Conversion) Use of onTouchEvent method, ontouchevent Method

OnTouchEvent. This method is defined in the View class, And all View subclasses overwrite this method. Applications can use this method to process touch events on the mobile phone screen. The signature of this method is as follows.

public boolean onTouchEvent(MotionEvent event)  

Parameter event: the parameter event is an object of the mobile phone screen touch event encapsulation class, which encapsulates all information about the event, such as the touch position, touch type, and touch time. This object will be created when you touch the mobile phone screen.

Return Value: the return value mechanism of this method is the same as that of the keyboard response event. It also returns true if the event has been completely processed and other callback methods are not expected to be processed again. Otherwise, false is returned.

This method does not handle only one event as described previously. Generally, all the events in the following three cases are handled by the onTouchEvent method, but the action values are different in three cases.

Screen pressed: when the screen is pressed, this method is automatically called to handle the event. In this case, MotionEvent. the value of getAction () is MotionEvent. ACTION_DOWN: If you need to process the screen-pressed event in the application, you only need to re-call the callback method and then judge the action in the method.

Screen lift: The event triggered when the touch pen leaves the screen. The event also needs to be captured by the onTouchEvent method, and then the action is judged in the method. When the value of MotionEvent. getAction () is MotionEvent. ACTION_UP, it indicates that the screen is lifted.

Drag on the screen: This method is also responsible for processing the event where the touch pen slides on the screen. It also calls the MotionEvent. getAction () method to determine whether the action value is MotionEvent. ACTION_MOVE and then process it.

Unlike the traditional Click Touch Screen, Android Touch Screen has some Gesture gestures, such as Fling and Scroll. These Gesture greatly improves user experience.
In Android, Gesture recognition (detector) is implemented through the GestureDetector. OnGestureListener interface.
First, the Android event processing mechanism is implemented based on Listener. For example, a touch screen event is implemented through onTouchListener;
Secondly, all View subclasses can add Listener for a certain type of events by using methods such as setOnTouchListener () and setOnKeyListener;
Third, Listener is generally provided in Interface mode, which contains one or more abstract methods. We need to implement these methods to complete onTouch (), onKey (), and other operations. In this way, the program can use the callback function to give an appropriate response when a specific event is sent to the view by dispatch.

1. Touch Screen Click example

 1 public class MyGesture extends Activity implements OnTouchListener { 2     public void onCreate(Bundle savedInstanceState) { 3         super.onCreate(savedInstanceState); 4  5         setContentView(R.layout.main); 6  7         TextView tv = (TextView) findViewById(R.id.tv); 8  9         tv.setOnTouchListener(this);10     }11 12     public boolean onTouch(View v, MotionEvent event)13 14     {15         Toast.makeText(this, "Touch Touch", Toast.LENGTH_SHORT).show();16 17         return false;18     }19 20 }

 

We can use the getAction () method of MotionEvent to obtain the Touch event type, including ACTION_DOWN (by pressing the Touch screen), ACTION_MOVE (by pressing the Touch screen), ACTION_UP (by releasing the Touch screen) and ACTION_CANCEL (not directly triggered by the user ). After obtaining coordinates using getRawX (), getRawY (), getX (), getX (), and getY () methods based on different user operations, we can implement operations such as dragging a button, drag a scroll bar. 2. How can we identify users' Gesture when capturing Touch operations? Here we need help from the GestureDetector. OnGestureListener interface. The Code is as follows:

 

1 public class MyGesture extends Activity implements OnTouchListener, OnGestureListener 2 3 {4 5 private GestureDetector listener; 6 7 public MyGesture () 8 9 {10 11 listener = new GestureDetector (this ); 12 13} 14 15 public void onCreate (Bundle savedInstanceState) 16 17 {18 19 super. onCreate (savedInstanceState); 20 21 setContentView (R. layout. main); 22 23 TextView TV = (TextView) findViewById (R. id. TV); 24 25 TV. setOnTouchListener (this); 26 27 TV. setFocusable (true); 28 29 TV. setClickable (true); 30 31 TV. setLongClickable (true); 32 33 mGestureDetector. setIsLongpressEnabled (true); 34 35} 36 37/* 38 ** in the onTouch () method, we call the onTouchEvent () method of GestureDetector, 39 * assign the captured MotionEvent to GestureDetector * to analyze whether there is a proper callback function to process the user's gesture 40 */41 42 public boolean onTouch (View v, MotionEvent event) 43 44 {45 46 return mGestureDetector. onTouchEvent (event); 47 48} 49 50 // you can touch the touch screen to trigger 51 52 public boolean onDown (MotionEvent arg0) 53 54 {55 56 Log by one MotionEvent ACTION_DOWN. I ("MyGesture", "onDown"); 57 58 Toast. makeText (this, "onDown", Toast. LENGTH_SHORT ). show (); 59 60 return true; 61 62} 63 64/* 65 ** touch the touch screen, not released or dragged, triggered by a MotionEvent ACTION_DOWN * 66 * note the difference with onDown (). It is emphasized that the status 67 */68 69 public void onShowPress (MotionEvent e) is not released or dragged) 70 71 {72 73 Log. I ("MyGesture", "onShowPress"); 74 75 Toast. makeText (this, "onShowPress", Toast. LENGTH_SHORT ). show (); 76 77} 78 79 // the user (after touching the touch screen) is released. A MotionEvent ACTION_UP triggers 80 81 public boolean onSingleTapUp (MotionEvent e) 82 83 {84 85 Log. I ("MyGesture", "onSingleTapUp"); 86 87 Toast. makeText (this, "onSingleTapUp", Toast. LENGTH_SHORT ). show (); 88 89 return true; 90 91} 92 93 // After the user presses the touch screen and moves quickly, the system is released by one MotionEvent ACTION_DOWN, multiple ACTION_MOVE, one ACTION_UP triggers 94 95 public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 96 97 {98 99 Log. I ("MyGesture", "onFling"); 100 101 Toast. makeText (this, "onFling", Toast. LENGTH_LONG ). show (); 102 103 return true; 104 105} 106 107 // The user presses the touch screen and drags it down by one MotionEvent ACTION_DOWN, multiple ACTION_MOVE triggers 108 109 public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) 110 111 {112 113 Log. I ("MyGesture", "onScroll"); 114 115 Toast. makeText (this, "onScroll", Toast. LENGTH_LONG ). show (); 116 117 return true; 118 119} 120 121 // press the touch screen for the user to trigger 122 123 public void onLongPress (MotionEvent e) by multiple MotionEvent ACTION_DOWN) 124 125 {126 127 Log. I ("MyGesture", "onLongPress"); 128 129 Toast. makeText (this, "onLongPress", Toast. LENGTH_LONG ). show (); 130 131} 132 133}

 

3. fling event processing code: Except for the first ACTION_DOWN that triggers Fling and the coordinates contained in the last ACTION_MOVE, we can also use the user's moving speed on the X or Y axis as a condition. For example, in the following code, we can only process a user moving more than 100 pixels and moving more than 200 pixels per second on the X axis.

1 public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, 2 float velocityY) 3 4 {5 6 // parameter explanation: 7 8 // e1: 1st ACTION_DOWN MotionEvent 9 10 // e2: Last ACTION_MOVE MotionEvent11 12 // velocityX: moving speed on the X axis, pixel/second 13 14 // velocityY: moving speed on the Y axis, pixel/second 15 16 // trigger condition: 17 18 // The coordinate displacement of the X axis is greater than that of FLING_MIN_DISTANCE, and the moving speed is greater than that of FLING_MIN_VELOCITY. pixel/second 19 20 final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200; 21 22 if (e1.getX ()-e2.getX ()> FLING_MIN_DISTANCE23 & Math. abs (velocityX)> FLING_MIN_VELOCITY) 24 25 {26 27 // Fling left28 29 Log. I ("MyGesture", "Fling left"); 30 31 Toast. makeText (this, "Fling Left", Toast. LENGTH_SHORT ). show (); 32 33} 34 35 else if (e2.getX ()-e1.getX ()> FLING_MIN_DISTANCE36 & Math. abs (velocityX)> FLING_MIN_VELOCITY) 37 38 {39 40 // Fling right41 42 Log. I ("MyGesture", "Fling right"); 43 44 Toast. makeText (this, "Fling Right", Toast. LENGTH_SHORT ). show (); 45 46} 47 48 return false; 49 50}

 

In this example, TV. setLongClickable (true) is required, because only in this way can the view process different from the hold (I .e., ACTION_MOVE, or multiple ACTION_DOWN) of the Tap ), we can also achieve this through android: longClickable in the layout definition.

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.