Android development gesture Swipe (swipe gesture monitor)

Source: Internet
Author: User
Tags gety

In Android applications, gesture swipe is often required, such as swipe up or down, or swipe left or right, and there are usually two ways to handle gesture swiping: one is to implement Setontouchlistener listeners individually, and the other is to build gesture detectors

The first method is to realize the sliding view, the implementation of Ontouchlistener monitoring events, and then judge the Keydonw and KeyUp direct location distance to determine the direction of the slide, the core implementation code is as follows:

/*** Set swipe up and down as listener *@authorcaizhiming*/    Private voidSetgesturelistener () {Myview.setontouchlistener (NewOntouchlistener () {@Override Public BooleanOnTouch (View V, motionevent event) {//TODO auto-generated Method Stub                Switch(Event.getaction ()) { CaseMotionEvent.ACTION_DOWN:mPosX=Event.getx (); Mposy=event.gety ();  Break;  CaseMotionEvent.ACTION_MOVE:mCurPosX=Event.getx (); Mcurposy=event.gety ();  Break;  Casemotionevent.action_up:if(Mcurposy-mposy > 0 && (Math.Abs (mcurposy-mposy) > 25)) {                        //to Slide                                            } Else if(Mcurposy-mposy < 0 && (Math.Abs (mcurposy-mposy) > 25)) {                        //swipe upcollapse (); }                                         Break; }                return true;    }        }); }

The second method is to construct a gesture detector, such as gesturedetector mygesture = new Gesturedetector (this), and then in the Onfling method according to the two parameters of the Motionevent Press and slide and release the position and distance to determine the direction of the slide and sliding speed, and so on. To build the Gesturedetector, you must use it with Ontouchlistener, as you must set up touch monitoring, which is the core implementation example:

ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.GestureDetector;ImportAndroid.view.GestureDetector.OnGestureListener;ImportAndroid.view.LayoutInflater;Importandroid.view.MotionEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnTouchListener;Importandroid.view.animation.Animation;Importandroid.view.animation.AnimationUtils;Importandroid.widget.FrameLayout;Importandroid.widget.LinearLayout; Public classTagscrollviewextendsFramelayoutImplementsOntouchlistener, ongesturelistener{Private floatmposx, Mposy, Mcurposx, Mcurposy; Private Static Final intFling_min_distance = 20;//Move minimum Distance    Private Static Final intfling_min_velocity = 200;//Moving Maximum speed//Building Gesture DetectorsGesturedetector mygesture =NewGesturedetector ( This);  PublicTagscrollview (Context context) { This(Context,NULL); }         PublicTagscrollview (Context context, AttributeSet attrs) { This(context, Attrs, 0); }         PublicTagscrollview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); //Setgesturelistener (); //setting up Touch monitoring         This. Setontouchlistener ( This); //Allow Long Press         This. setlongclickable (true); } @Override Public BooleanOnTouch (View V, motionevent event) {//TODO auto-generated Method Stub        returnmygesture.ontouchevent (event); } @Override Public BooleanOndown (motionevent e) {//TODO auto-generated Method Stub        return false; } @Override Public voidonshowpress (motionevent e) {//TODO auto-generated Method Stub} @Override Public BooleanOnsingletapup (motionevent e) {//TODO auto-generated Method Stub        return false; } @Override Public BooleanOnscroll (motionevent E1, motionevent E2,floatDistancex,floatDistancey) {        //TODO auto-generated Method Stub        return false; } @Override Public voidonlongpress (motionevent e) {//TODO auto-generated Method Stub} @Override Public BooleanOnfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {        //TODO auto-generated Method Stub//E1:1th action_down motionevent//E2: The last Action_move motionevent//movement Speed (pixels/sec) on the velocityx:x axis//movement Speed (pixels/sec) on the velocityy:y axis//x-axis coordinate displacement greater than fling_min_distance, and moving faster than fling_min_velocity pixels per second//to        if(E1.gety ()-e2.gety () >fling_min_distance) {      //&& Math.Abs (Velocityx) > Fling_min_velocity) {      collapse (); }           //Upward        if(E2.gety ()-e1.gety () >fling_min_distance&& Math.Abs (Velocityx) >fling_min_velocity) {                    }                 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.