监听手指左右滑动屏幕的事件,封装好了一个类

来源:互联网 发布:2015年双十一销售数据 编辑:程序博客网 时间:2024/05/17 21:47


转载自:http://blog.csdn.net/qiantujava/article/details/22639353

在做Activity的左右滑动时,用到了这个连接中的封装类,解决了问题,简洁有效!

  1. package com.example.test;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.util.Log;  
  7. import android.widget.RelativeLayout;  
  8.   
  9. public class MainActivity extends Activity {  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);//这里的xml是一个空白的layout  
  14.           
  15.         //需要监听左右滑动事件的view  
  16.         RelativeLayout view = (RelativeLayout) this.findViewById(R.id.layout);  
  17.           
  18.         //setLongClickable是必须的  
  19.         view.setLongClickable(true);  
  20.         view.setOnTouchListener(new MyGestureListener(this));  
  21.     }  
  22.       
  23.     /** 
  24.      * 继承GestureListener,重写left和right方法 
  25.      */  
  26.     private class MyGestureListener extends GestureListener {  
  27.         public MyGestureListener(Context context) {  
  28.             super(context);  
  29.         }  
  30.   
  31.         @Override  
  32.         public boolean left() {  
  33.             Log.e("test""向左滑");  
  34.             return super.left();  
  35.         }  
  36.   
  37.         @Override  
  38.         public boolean right() {  
  39.             Log.e("test""向右滑");  
  40.             return super.right();  
  41.         }  
  42.     }  
  43. }  


              封装的类:



  1. package com.example.test;  
  2.   
  3. import android.content.Context;  
  4. import android.view.GestureDetector.SimpleOnGestureListener;  
  5. import android.view.GestureDetector;  
  6. import android.view.View;  
  7. import android.view.View.OnTouchListener;  
  8. import android.view.MotionEvent;  
  9.   
  10. /** 
  11.  * 实现监听左右滑动的事件,哪个view需要的时候直接setOnTouchListener就可以用了 
  12.  * @author LinZhiquan 
  13.  * 
  14.  */  
  15. public class GestureListener extends SimpleOnGestureListener implements OnTouchListener {  
  16.     /** 左右滑动的最短距离 */  
  17.     private int distance = 100;  
  18.     /** 左右滑动的最大速度 */  
  19.     private int velocity = 200;  
  20.       
  21.     private GestureDetector gestureDetector;  
  22.       
  23.     public GestureListener(Context context) {  
  24.         super();  
  25.         gestureDetector = new GestureDetector(context, this);  
  26.     }  
  27.   
  28.     /** 
  29.      * 向左滑的时候调用的方法,子类应该重写 
  30.      * @return 
  31.      */  
  32.     public boolean left() {  
  33.         return false;  
  34.     }  
  35.       
  36.     /** 
  37.      * 向右滑的时候调用的方法,子类应该重写 
  38.      * @return 
  39.      */  
  40.     public boolean right() {  
  41.         return false;  
  42.     }  
  43.       
  44.     @Override  
  45.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  46.             float velocityY) {  
  47.         // TODO Auto-generated method stub  
  48.         // e1:第1个ACTION_DOWN MotionEvent  
  49.         // e2:最后一个ACTION_MOVE MotionEvent  
  50.         // velocityX:X轴上的移动速度(像素/秒)  
  51.         // velocityY:Y轴上的移动速度(像素/秒)  
  52.   
  53.         // 向左滑  
  54.         if (e1.getX() - e2.getX() > distance  
  55.                 && Math.abs(velocityX) > velocity) {  
  56.             left();  
  57.         }  
  58.         // 向右滑  
  59.         if (e2.getX() - e1.getX() > distance  
  60.                 && Math.abs(velocityX) > velocity) {  
  61.             right();  
  62.         }  
  63.         return false;  
  64.     }  
  65.   
  66.     @Override  
  67.     public boolean onTouch(View v, MotionEvent event) {  
  68.         // TODO Auto-generated method stub  
  69.         gestureDetector.onTouchEvent(event);  
  70.         return false;  
  71.     }  
  72.   
  73.     public int getDistance() {  
  74.         return distance;  
  75.     }  
  76.   
  77.     public void setDistance(int distance) {  
  78.         this.distance = distance;  
  79.     }  
  80.   
  81.     public int getVelocity() {  
  82.         return velocity;  
  83.     }  
  84.   
  85.     public void setVelocity(int velocity) {  
  86.         this.velocity = velocity;  
  87.     }  
  88.   
  89.     public GestureDetector getGestureDetector() {  
  90.         return gestureDetector;  
  91.     }  
  92.   
  93.     public void setGestureDetector(GestureDetector gestureDetector) {  
  94.         this.gestureDetector = gestureDetector;  
  95.     }  
  96. }  





0 0
原创粉丝点击