Android uses Gesturedetector to achieve gesture swipe effects

Source: Internet
Author: User
Tags gety

See examples directly:
Package Com.example.gesturedetector;import Android.os.bundle;import Android.app.activity;import Android.view.gesturedetector;import Android.view.gesturedetector.ongesturelistener;import Android.view.Menu; Import Android.view.motionevent;import Android.widget.toast;public class Mainactivity extends Activity implements Ongesturelistener {private gesturedetector mgesturedetector; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); mGestureDetector = new Gesturedetector (this);} @Overridepublic boolean ontouchevent (Motionevent event) {//TODO auto-generated method Stubmgesturedetector.ontouchevent (event); return Super.ontouchevent (event);} @Overridepublic boolean Ondown (motionevent arg0) {//TODO auto-generated method Stub//toast.maketext (This, "Ondown", Toast.length_short). Show (); return false;} @Overridepublic boolean onfling (motionevent startevent, motionevent endevent,float Velocityx, float velocityy) {//TODO auto-generated Method Stubif (Startevent.gety ()-endevent.gety () >) {toast.maketext (this, "gesture swipe up", toast.le Ngth_short). Show (); return true;} else if (startevent.gety ()-endevent.gety () < -100) {Toast.maketext (this, "swipe down", Toast.length_short). Show (); return true;} else if (Startevent.getx ()-endevent.getx () >) {toast.maketext (this, "gesture swipe left", Toast.length_short). Show (); return true;} else if (Startevent.getx ()-Endevent.getx () < -100) {Toast.maketext (this, "gesture right swipe", Toast.length_short). Show (); return true;} return false;} @Overridepublic void Onlongpress (Motionevent arg0) {//TODO auto-generated method Stub//toast.maketext (This, " Onlongpress ", Toast.length_short). Show ();}  @Overridepublic boolean onscroll (motionevent arg0, motionevent arg1, float arg2,float arg3) {//TODO auto-generated method Stub//toast.maketext (This, "onscroll", Toast.length_short). Show (); return false;} @Overridepublic void Onshowpress (Motionevent arg0) {//TODO auto-generated method Stub//toaSt.maketext (This, "onshowpress", Toast.length_short). Show (); @Overridepublic boolean onsingletapup (motionevent arg0) {//TODO auto-generated method Stub//toast.maketext (This, " Onsingletapup ", Toast.length_short). Show (); return false;} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}}
Event Explanation:
1.   Ondown (motionevent e): down event; 2.   Onsingletapup (motionevent E): one click Up event; 3.   Onshowpress (motionevent E): When the Down event occurs and move or up does not occur before the event is triggered; 4.   Onlongpress (motionevent E): Long press event; 5.   Onfling (motionevent E1, motionevent E2, float Velocityx, float velocityy): swipe gesture event; 6.   Onscroll (motionevent E1, motionevent E2, float Distancex, float Distancey): Drag events on the screen.

Android uses Gesturedetector to achieve gesture swipe effects

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.