Android GestureDetector Application

Source: Internet
Author: User

Code: [java] import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. gestureDetector; import android. view. gestureDetector. onGestureListener; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. widget. textView; import android. widget. toast; public class GestureActivity extends Activity implements OnTouchListener, OnGestureListener {includetector; public GestureActivity () {detector = new role (this);} public void onCreate (Bundle savedInstanceState. onCreate (savedInstanceState); setContentView (R. layout. main); TextView TV = (TextView) findViewById (R. id. textView001); // set the TV ****** TV. setOnTouchListener (this); TV. setFocusable (true); // required. view can process hold TV different from Tap. setClickable (true); TV. setLongClickable (true); detector. setIsLongpressEnabled (true);}/** in the onTouch () method, we call the onTouchEvent () method of GestureDetector, assign the captured MotionEvent to GestureDetector * to analyze whether there is an appropriate callback function to process the user's gesture */public boolean onTouch (View v, MotionEvent event) {return detector. onTouchEvent (event);} // you can touch the touch screen. One MotionEvent ACTION_DOWN triggers public boolean onDown (MotionEvent arg0) {Log. I ("MyGesture", "onDown"); Toast. makeText (this, "onDown", Toast. LENGTH_SHORT ). show (); return true;}/** when you touch the touch screen, you have not released or dragged it. It is triggered by one MotionEvent ACTION_DOWN * note the difference between it and onDown, it is emphasized that the status is not released or dragged */public void onShowPress (MotionEvent e) {Log. I ("MyGesture", "onShowPress"); Toast. makeText (this, "onShowPress", Toast. LENGTH_SHORT ). show ();} // the user (after touching the touch screen) is released. One MotionEvent ACTION_UP triggers public boolean onSingleTapUp (MotionEvent e) {Log. I ("MyGesture", "onSingleTapUp"); Toast. makeText (this, "onSingleTapUp", Toast. LENGTH_SHORT ). show (); return true;} // The user presses the touch screen and moves quickly. One MotionEvent ACTION_DOWN, multiple ACTION_MOVE, and one ACTION_UP triggers public boolean onFling (MotionEvent e1, motionEvent e2, float velocityX, float velocityY) {Log. I ("MyGesture", "onFling"); // parameter explanation: // e1: 1st ACTION_DOWN MotionEvent // e2: The last ACTION_MOVE MotionEvent // velocityX: moving speed on the X axis, pixel/second // velocityY: moving speed on the Y axis, pixel/second // trigger condition: // coordinate displacement of the X axis is greater than FLING_MIN_DISTANCE, the moving speed is greater than FLING_MIN_VELOCITY pixels/Second final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200; if (e1.getX ()-e2.getX ()> FLING_MIN_DISTANCE & Math. abs (velocityX)> FLING_MIN_VELOCITY) {// Fling left Log. I ("MyGesture", "Fling left"); Toast. makeText (this, "Fling Left", Toast. LENGTH_SHORT ). show ();} else if (e2.getX ()-e1.getX ()> FLING_MIN_DISTANCE & Math. abs (velocityX)> FLING_MIN_VELOCITY) {// Fling right Log. I ("MyGesture", "Fling right"); Toast. makeText (this, "Fling Right", Toast. LENGTH_SHORT ). show ();} else if (e2.getY ()-e1.getY ()> FLING_MIN_DISTANCE & Math. abs (velocityY)> FLING_MIN_VELOCITY) {// Fling down Log. I ("MyGesture", "Fling down"); Toast. makeText (this, "Fling down", Toast. LENGTH_SHORT ). show ();} else if (e1.getY ()-e2.getY ()> FLING_MIN_DISTANCE & Math. abs (velocityY)> FLING_MIN_VELOCITY) {// Fling up Log. I ("MyGesture", "Fling up"); Toast. makeText (this, "Fling up", Toast. LENGTH_SHORT ). show ();} return false;} // press the touch screen and drag it. One MotionEvent ACTION_DOWN and multiple ACTION_MOVE triggers public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {Log. I ("MyGesture", "onScroll"); Toast. makeText (this, "onScroll", Toast. LENGTH_LONG ). show (); return true;} // press the touch screen to trigger public void onLongPress (MotionEvent e) {Log. I ("MyGesture", "onLongPress"); Toast. makeText (this, "onLongPress", Toast. LENGTH_LONG ). show ();}}

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.