Packaged Android monitor finger left and right sliding screen event class sharing _android

Source: Internet
Author: User
Tags abs stub

The right and left sliding is the most commonly used mobile phone, this simple encapsulation a bit, later directly to use on it.

Just a few lines is all it takes, and the following class is encapsulated.

 package com.example.test;
Import Android.os.Bundle;
Import android.app.Activity;
Import Android.content.Context;
Import Android.util.Log;

Import Android.widget.RelativeLayout; public class Mainactivity extends activity {@Override protected void onCreate (Bundle savedinstancestate) {Super.oncre
 Ate (savedinstancestate); Setcontentview (R.layout.activity_main);//The XML here is a blank layout//view Relativelayout view of the left and right sliding events (relativelayout
 
 ) This.findviewbyid (r.id.layout);
 Setlongclickable is a necessary view.setlongclickable (true);
 View.setontouchlistener (This) (new Mygesturelistener); /** * Inherits Gesturelistener, overrides left and right methods * * Private class Mygesturelistener extends Gesturelistener {public mygest
 Urelistener (context) {super (context);
  @Override public Boolean Left () {LOG.E ("test");
 return Super.left ();
  @Override public Boolean Right () {LOG.E ("test", "slide to starboard");
 return Super.right (); }
 }
}


Package com.example.test;
Import Android.content.Context;
Import Android.view.GestureDetector.SimpleOnGestureListener;
Import Android.view.GestureDetector;
Import Android.view.View;
Import Android.view.View.OnTouchListener;

Import android.view.MotionEvent; /** * To monitor the right and left sliding events, which view needs when the direct Setontouchlistener can be used * @author Linzhiquan * */public class Gesturelistener extends
 Simpleongesturelistener implements Ontouchlistener {/** the shortest distance between left and right sliding * * private int distance = 100;
 
 /** the maximum speed of sliding left and right/private int velocity = 200;
 
 Private Gesturedetector Gesturedetector;
 Public Gesturelistener {super ();
 Gesturedetector = new Gesturedetector (context, this);
 /** * The method to invoke when sliding to the left, subclasses should override * @return * */public Boolean Left () {return false;
 /** * The method called when sliding to the right, the subclass should override * @return * * * ()-Public Boolean right () {return false; @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {//TODO Auto -generated Method StUB//E1:1th action_down motionevent//E2: Move speed on last Action_move//motionevent axis (Pixel/sec)//velocityx:x movement speed on the axis
 Degrees (Pixels/sec)//left slip if (E1.getx ()-e2.getx () > Distance && math.abs (Velocityx) > Velocity) () ();
 //Rightwards if (E2.getx ()-e1.getx () > Distance && math.abs (Velocityx) > Velocity) {right ();
 return false; @Override public boolean Ontouch (View v., motionevent event) {//TODO auto-generated method stub Gesturedetector.on
 TouchEvent (event);
 return false;
 public int getdistance () {return distance;
 The public void setdistance (int distance) {this.distance = distance;
 public int getvelocity () {return velocity;
 public void setvelocity (int velocity) {this.velocity = velocity;
 Public Gesturedetector Getgesturedetector () {return gesturedetector;
 public void Setgesturedetector (Gesturedetector gesturedetector) {this.gesturedetector = Gesturedetector;

 }
}
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.