Android "swipe" vs "Fling"

Source: Internet
Author: User
Tags gety

Onfling would get executed when a user makes a "fling" motion, and said motion have a velocity with it to determine the type of fling it was. However,

If a user simply touches the device and moves slowly across the screen, which would not being considered a fling, but a swipe.

It comes down to what type of motion you expect the users to perform. The ideal case would is to implement the Onfling function to capture that

Motion, and also implement Ondrag and ondragfinished to capture the more subtle motions this should still be considered a Swipe.

 Public classMyActivityextendsActivity {Private voidonCreate () {FinalGesturedetector GDT =NewGesturedetector (NewGesturelistener ()); FinalImageView ImageView =(ImageView) Findviewbyid (R.id.image_view); Imageview.setontouchlistener (NewOntouchlistener () {@Override Public BooleanOnTouch (FinalView View,Finalmotionevent Event)                {gdt.ontouchevent (event); return true;    }        }); }                   Private Static Final intSwipe_min_distance = 120; Private Static Final intswipe_threshold_velocity = 200; Private classGesturelistenerextendsSimpleongesturelistener {@Override Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {            if(E1.getx ()-E2.getx () > Swipe_min_distance && math.abs (Velocityx) >swipe_threshold_velocity) {                return false;// Right-left}Else if(E2.getx ()-E1.getx () > Swipe_min_distance && math.abs (Velocityx) >swipe_threshold_velocity) {                return false;// left to right            }            if(E1.gety ()-e2.gety () > Swipe_min_distance && math.abs (velocityy) >swipe_threshold_velocity) {                return false;//Bottom to top}Else if(E2.gety ()-e1.gety () > Swipe_min_distance && math.abs (velocityy) >swipe_threshold_velocity) {                return false;//Top to bottom            }            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.