Android monitoring: slide the screen from top to bottom

Source: Internet
Author: User
Tags gety

Android monitoring: slide the screen from top to bottom

When developing android programs, you sometimes need to monitor the sliding screen of your fingers. When your fingers slide toward the top, bottom, and left, different responses are made. How can this problem be solved?

Using the gesture Monitor provided by Android, you can easily implement it and directly upload the code (tested)

 

Public class CbMainActivity extends Activity implements android. view. gestureDetector. onGestureListener {// defines the GestureDetector detector of the gesture detector instance; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. cb_main); // create a gesture detector = new GestureDetector (this, this);} // send the touch event on the activity to GestureDetector to process public boolean onTouchEvent (MotionEvent me) {return detector. onTouchEvent (me) ;}@ Overridepublic boolean onDown (MotionEvent arg0) {return false;}/*** sliding screen monitoring ***/@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {float minMove = 120; // minimum sliding distance: float minVelocity = 0; // minimum Sliding Speed: float begbeginx = e1.getX (); float endX = e2.getX (); float beginY = e1.getY (); float endY = e2.getY (); if (beginX INX-endX> minMove & Math. abs (velocityX)> minVelocity) {// Toast. makeText (this, velocityX + "left slide", Toast. LENGTH_SHORT ). show ();} else if (endX-beginX> minMove & Math. abs (velocityX)> minVelocity) {// slide right Toast. makeText (this, velocityX + "slide right", Toast. LENGTH_SHORT ). show ();} else if (beginY-endY> minMove & Math. abs (velocityY)> minVelocity) {// slide Toast. makeText (this, velocityX + "slip", Toast. LENGTH_SHORT ). show ();} else if (endY-beginY> minMove & Math. abs (velocityY)> minVelocity) {// decline Toast. makeText (this, velocityX + "decline", Toast. LENGTH_SHORT ). show ();} return false;} @ Overridepublic void onShowPress (MotionEvent arg0) {// TODO Auto-generated method stub} @ Overridepublic boolean onSingleTapUp (MotionEvent arg0) {// TODO Auto-generated method stubreturn false;} @ Overridepublic void onLongPress (MotionEvent arg0) {// TODO Auto-generated method stub} @ Overridepublic boolean onScroll (MotionEvent e1, motionEvent e2, float velocityX, float velocityY) {return false ;}}


 

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.