android GestureDetector的用法和介绍

来源:互联网 发布:福州橙子网络 编辑:程序博客网 时间:2024/06/12 06:47

 

类描述:

 

通过系统提供的MotionEvent来监测各种手势和(触摸)事件。当一个指定的手势事件发生时,GestureDetector.OnGestureListener回调函数将通告用户。这个类仅仅处理由触摸引发的MotionEvent(不能处理由轨迹球引发的事件)。

类中的一些手势类型:

   1.boolean  onDoubleTap(MotionEvent e)解释:双击的第二下Touch down时触发
   2.boolean  onDoubleTapEvent(MotionEvent e)解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
   3.boolean  onDown(MotionEvent e)解释:Touch down时触发
   4.boolean  onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)解释:Touch了滑动一点距离后,up时触发。
   5.void  onLongPress(MotionEvent e)解释:Touch了不移动一直Touch down时触发
   6.boolean  onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)解释:Touch了滑动时触发。
   7.void  onShowPress(MotionEvent e)解释:Touch了还没有滑动时触发(与onDown,onLongPress)比较onDown只要Touch down一定立刻触发。而Touchdown后过一               会没有滑动先触发onShowPress再是onLongPress。所以Touchdown后一直不 滑动,onDown->onShowPress->onLongPress这个顺序触发。
   8.boolean  onSingleTapConfirmed(MotionEvent e)
   9.boolean  onSingleTapUp(MotionEvent e)解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。
      点击一下非常快的(不滑动)Touchup:onDown->onSingleTapUp->onSingleTapConfirmed
      点击一下稍微慢点的(不滑 动)Touchup:onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

使用方法:

构造出来,mGestureDetector = new GestureDetector(mGestureListener);

mGestureListener = new BookOnGestureListener();

class BookOnGestureListener implements OnGestureListener {
时要public boolean onTouchEvent(MotionEvent event) {
    mGestureListener.onTouchEvent(event);
}

这样就可以判断手势MotionEvent 的类型了,然后做出一些处理。

下面贴出一个类做参考、

[java] view plaincopyprint?
  1. class BookOnGestureListenerimplements OnGestureListener { 
  2.         public boolean onDown(MotionEvent event) { 
  3.             Log.d(LOG_TAG, "onDown"); 
  4.             if (mState == BookState.ANIMATING) 
  5.                 return false
  6.             float x = event.getX(), y = event.getY(); 
  7.             int w = layoutWidth, h = layoutHeight; 
  8.             if (x * x + y * y < clickCornerLen) { 
  9.                 if (indexPage >0) { 
  10.                     mSelectCorner = Corner.LeftTop; 
  11.                     aniStartPos = new Point(0,0); 
  12.                 } 
  13.             } else if ((x - w) * (x - w) + y * y < clickCornerLen) { 
  14.                 if (indexPage < totalPageNum -1) { 
  15.                     mSelectCorner = Corner.RightTop; 
  16.                     aniStartPos = new Point(layoutWidth,0); 
  17.                 } 
  18.             } else if (x * x + (y - h) * (y - h) < clickCornerLen) { 
  19.                 if (indexPage >0) { 
  20.                     mSelectCorner = Corner.LeftBottom; 
  21.                     aniStartPos = new Point(0, layoutHeight); 
  22.                 } 
  23.             } else if ((x - w) * (x - w) + (y - h) * (y - h) < clickCornerLen) { 
  24.                 if (indexPage < totalPageNum -1) { 
  25.                     mSelectCorner = Corner.RightBottom; 
  26.                     aniStartPos = new Point(layoutWidth, layoutHeight); 
  27.                 } 
  28.             } 
  29.             if (mSelectCorner != Corner.None) { 
  30.                 aniStopPos = new Point((int) x, (int) y); 
  31.                 aniTime = 800
  32.                 mState = BookState.ABOUT_TO_ANIMATE; 
  33.                 closeBook = false
  34.                 aniStartTime = new Date(); 
  35.                 mBookView.startAnimation(); 
  36.             } 
  37.             return false
  38.         } 
  39.  
  40.         public boolean onFling(MotionEvent e1, MotionEvent e2,float velocityX, 
  41.                 float velocityY) { 
  42.             Log.d(LOG_TAG, "onFling velocityX:" + velocityX +" velocityY:" 
  43.                     + velocityY); 
  44.             if (mSelectCorner != Corner.None) { 
  45.                 if (mSelectCorner == Corner.LeftTop) { 
  46.                     if (velocityX < 0) { 
  47.                         aniStopPos = new Point(0, 0); 
  48.                     } else
  49.                         aniStopPos = new Point(2 * layoutWidth, 0); 
  50.                     } 
  51.                 } else if (mSelectCorner == Corner.RightTop) { 
  52.                     if (velocityX < 0) { 
  53.                         aniStopPos = new Point(-layoutWidth, 0); 
  54.                     } else
  55.                         aniStopPos = new Point(layoutWidth, 0); 
  56.                     } 
  57.                 } else if (mSelectCorner == Corner.LeftBottom) { 
  58.                     if (velocityX < 0) { 
  59.                         aniStopPos = new Point(0, layoutHeight); 
  60.                     } else
  61.                         aniStopPos = new Point(2 * layoutWidth, layoutHeight); 
  62.                     } 
  63.                 } else if (mSelectCorner == Corner.RightBottom) { 
  64.                     if (velocityX < 0) { 
  65.                         aniStopPos = new Point(-layoutWidth, layoutHeight); 
  66.                     } else
  67.                         aniStopPos = new Point(layoutWidth, layoutHeight); 
  68.                     } 
  69.                 } 
  70.                 Log.d(LOG_TAG, "onFling animate"); 
  71.                 aniStartPos = new Point((int) scrollX, (int) scrollY); 
  72.                 aniTime = 1000
  73.                 mState = BookState.ABOUT_TO_ANIMATE; 
  74.                 closeBook = true
  75.                 aniStartTime = new Date(); 
  76.                 mBookView.startAnimation(); 
  77.             } 
  78.             return false
  79.         } 
  80.  
  81.         public void onLongPress(MotionEvent e) { 
  82.             Log.d(LOG_TAG, "onLongPress"); 
  83.         } 
  84.  
  85.         public boolean onScroll(MotionEvent e1, MotionEvent e2, 
  86.                 float distanceX, float distanceY) { 
  87.             mState = BookState.TRACKING; 
  88.             if (mSelectCorner != Corner.None) { 
  89.                 scrollX = e2.getX(); 
  90.                 scrollY = e2.getY(); 
  91.                 mBookView.startAnimation(); 
  92.             } 
  93.             return false
  94.         } 
  95.  
  96.         public void onShowPress(MotionEvent e) { 
  97.             Log.d(LOG_TAG, "onShowPress"); 
  98.         } 
  99.  
  100.         public boolean onSingleTapUp(MotionEvent e) { 
  101.             Log.d(LOG_TAG, "onSingleTapUp"); 
  102.  
  103.             if (mSelectCorner != Corner.None) { 
  104.                 if (mSelectCorner == Corner.LeftTop) { 
  105.                     if (scrollX < layoutWidth /2) { 
  106.                         aniStopPos = new Point(0,0); 
  107.                     } else
  108.                         aniStopPos = new Point(2 * layoutWidth,0); 
  109.                     } 
  110.                 } else if (mSelectCorner == Corner.RightTop) { 
  111.                     if (scrollX < layoutWidth /2) { 
  112.                         aniStopPos = new Point(-layoutWidth,0); 
  113.                     } else
  114.                         aniStopPos = new Point(layoutWidth,0); 
  115.                     } 
  116.                 } else if (mSelectCorner == Corner.LeftBottom) { 
  117.                     if (scrollX < layoutWidth /2) { 
  118.                         aniStopPos = new Point(0, layoutHeight); 
  119.                     } else
  120.                         aniStopPos = new Point(2 * layoutWidth, layoutHeight); 
  121.                     } 
  122.                 } else if (mSelectCorner == Corner.RightBottom) { 
  123.                     if (scrollX < layoutWidth /2) { 
  124.                         aniStopPos = new Point(-layoutWidth, layoutHeight); 
  125.                     } else
  126.                         aniStopPos = new Point(layoutWidth, layoutHeight); 
  127.                     } 
  128.                 } 
  129.                 aniStartPos = new Point((int) scrollX, (int) scrollY); 
  130.                 aniTime = 800
  131.                 mState = BookState.ABOUT_TO_ANIMATE; 
  132.                 closeBook = true
  133.                 aniStartTime = new Date(); 
  134.                 mBookView.startAnimation(); 
  135.             } 
  136.             return false
  137.         } 
  138.     } 

 

 

转自   :http://blog.csdn.net/peijiangping1989/article/details/7227625