Android屏幕监控上下左右滑动


简单写一下,Android 下 view 或者 activity 实现 OnGestureListener 接口。

在 onFling方法中实现左右滑动:

  1. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  2.             float distanceY) {  
  3.         float y1 = e1.getY(), y2 = e2.getY();  
  4.         if (y1 -y2 > 120) {    
  5.             if (mDirection != SOUTH) {  
  6.                 mNextDirection = NORTH;  
  7.             }  
  8.             Log.d(this.getClass().getName(), "To UP" + "(" + y1  
  9.                     + "," + y2 + ")");  
  10.             return (true);  
  11.         } else if (y1 - y2 < -120) {    
  12.             if (mDirection != NORTH) {  
  13.                 mNextDirection = SOUTH;  
  14.             }  
  15.             Log.d(this.getClass().getName(), "To Down" + "(" + y1  
  16.                     + "," + y2 + ")");  
  17.             return (true);  
  18.         }    
  19.         return false;  
  20.     }  
在 onScroll 方法中实现上下滑动:
  1. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  2.             float velocityY) {  
  3.         Log.d("Fling""Fling Happened!");    
  4.         float x1 = e1.getX(), x2 = e2.getX();  
  5.           
  6.         if (x1 -x2 > 120) {    
  7.             if (mDirection != EAST) {  
  8.                 mNextDirection = WEST;  
  9.             }  
  10.             Log.d(this.getClass().getName(), "To LEFT" + "(" + x1  
  11.                     + "," + x2 + ")");  
  12.             return (true);  
  13.         } else if (x1 - x2 < -120) {    
  14.             if (mDirection != WEST) {  
  15.                 mNextDirection = EAST;  
  16.             }  
  17.             Log.d(this.getClass().getName(), "To Right" + "(" + x1  
  18.                     + "," + x2 + ")");  
  19.             return (true);  
  20.         }    
  21.           
  22.         return false;  
  23.     }  

相关内容