[Android] Mobile defender gesture swipe toggle screen

Source: Internet
Author: User

Defining gesture Recognizers

Get gesture recognizer gesturedetector object, via new Gesturedetector (Context,listener), Parameters: Context, listener

Anonymous inner class implements simple gesture listener simpleongesturelistener interface, overriding onfling () Sliding method

Pass in four parameters:

Motionevent E1 ,motionevent E2,velocityx,velocityy

E1 is the first point,E2 is the second point,the speed of thex -axis,the speed of they -axis

When the first point minus the second point is greater than ten , we think it is drawn from right to left, next page

When the second point minus the first point is greater than ten , we think it is drawn from left to right, previous page

Call the motionevent object's getrawx () to get the coordinates of the X -axis

Use gesture recognizer to identify gestures

Rewrite the activity 's ontouchevent () method to get the swipe event to the gesture on the interface

Pass in a parameter motionevent Object

Call the ontouchevent (event) method of the gesturedetector object , Parameter:motionevent Object , passing the acquired events in

Shielded diagonal Stroke

The distance between the y - axis coordinates of the two points is greater than the time we think it is oblique

Call the motionevent object's Getrawy () to get the coordinates of the Y - axis, and the difference between the two points takes the absolute value Math.Abs (), the judgment is greater than the number of returns true, does not go down

If the Simpleongesturelistener class is not found , use the new Gesturedetector.simpleongesturelistener ()

Extracting common methods into the base class abstract class basesecactivity , the activity only needs to inherit the base class, implement the abstract method of the upper and lower pages, can realize the left and right sliding effect

Basesecguideactivity.java

 PackageCom.qingguow.mobilesafe;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.GestureDetector;Importandroid.view.MotionEvent; Public Abstract classBasesecguideactivityextendsActivity {//defining gesture Recognizers    protectedGesturedetector Gesturedetector; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate); //instantiation ofGesturedetector =NewGesturedetector ( This,                NewGesturedetector.simpleongesturelistener () {@Override Public Booleanonfling (motionevent E1, motionevent E2,floatVelocityx,floatvelocityy) {                        //Shielded Diagonal Stroke                        if(Math.Abs (E1.getrawy ()-e2.getrawy ()) >100){                            return true; }                        if((E1.GETRAWX ()-e2.getrawx ()) > 100) {System.out.println ("From right to left, next page");                            Shownext (); return true; }                        if((E2.GETRAWX ()-e1.getrawx ()) > 100) {System.out.println ("Swipe from left to right, prev");                            Showpre (); return true; }                        return Super. Onfling (E1, E2, Velocityx, Velocityy);    }                }); }     Public Abstract voidShowpre (); @Override Public Booleanontouchevent (Motionevent event) {gesturedetector.ontouchevent (event); return Super. Ontouchevent (event); }     Public Abstract voidshownext ();}

[Android] Mobile defender gesture swipe toggle screen

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.