GestureDetector的用法和介绍

本文深入探讨了如何利用MotionEvent监测并处理触摸事件,特别关注手势识别的功能,包括双击、长按、滑动等常见手势,并通过实例展示了如何实现这些功能以提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过系统提供的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 的类型了,然后做出一些处理。

下面贴出一个类做参考、

  1. class BookOnGestureListener implements 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(00);   
  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(00);   
  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(00);   
  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.     }  
class BookOnGestureListener implements OnGestureListener {
		public boolean onDown(MotionEvent event) {
			Log.d(LOG_TAG, "onDown");
			if (mState == BookState.ANIMATING)
				return false;
			float x = event.getX(), y = event.getY();
			int w = layoutWidth, h = layoutHeight;
			if (x * x + y * y < clickCornerLen) {
				if (indexPage > 0) {
					mSelectCorner = Corner.LeftTop;
					aniStartPos = new Point(0, 0);
				}
			} else if ((x - w) * (x - w) + y * y < clickCornerLen) {
				if (indexPage < totalPageNum - 1) {
					mSelectCorner = Corner.RightTop;
					aniStartPos = new Point(layoutWidth, 0);
				}
			} else if (x * x + (y - h) * (y - h) < clickCornerLen) {
				if (indexPage > 0) {
					mSelectCorner = Corner.LeftBottom;
					aniStartPos = new Point(0, layoutHeight);
				}
			} else if ((x - w) * (x - w) + (y - h) * (y - h) < clickCornerLen) {
				if (indexPage < totalPageNum - 1) {
					mSelectCorner = Corner.RightBottom;
					aniStartPos = new Point(layoutWidth, layoutHeight);
				}
			}
			if (mSelectCorner != Corner.None) {
				aniStopPos = new Point((int) x, (int) y);
				aniTime = 800;
				mState = BookState.ABOUT_TO_ANIMATE;
				closeBook = false;
				aniStartTime = new Date();
				mBookView.startAnimation();
			}
			return false;
		}

		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			Log.d(LOG_TAG, "onFling velocityX:" + velocityX + " velocityY:"
					+ velocityY);
			if (mSelectCorner != Corner.None) {
				if (mSelectCorner == Corner.LeftTop) {
					if (velocityX < 0) {
						aniStopPos = new Point(0, 0);
					} else {
						aniStopPos = new Point(2 * layoutWidth, 0);
					}
				} else if (mSelectCorner == Corner.RightTop) {
					if (velocityX < 0) {
						aniStopPos = new Point(-layoutWidth, 0);
					} else {
						aniStopPos = new Point(layoutWidth, 0);
					}
				} else if (mSelectCorner == Corner.LeftBottom) {
					if (velocityX < 0) {
						aniStopPos = new Point(0, layoutHeight);
					} else {
						aniStopPos = new Point(2 * layoutWidth, layoutHeight);
					}
				} else if (mSelectCorner == Corner.RightBottom) {
					if (velocityX < 0) {
						aniStopPos = new Point(-layoutWidth, layoutHeight);
					} else {
						aniStopPos = new Point(layoutWidth, layoutHeight);
					}
				}
				Log.d(LOG_TAG, "onFling animate");
				aniStartPos = new Point((int) scrollX, (int) scrollY);
				aniTime = 1000;
				mState = BookState.ABOUT_TO_ANIMATE;
				closeBook = true;
				aniStartTime = new Date();
				mBookView.startAnimation();
			}
			return false;
		}

		public void onLongPress(MotionEvent e) {
			Log.d(LOG_TAG, "onLongPress");
		}

		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			mState = BookState.TRACKING;
			if (mSelectCorner != Corner.None) {
				scrollX = e2.getX();
				scrollY = e2.getY();
				mBookView.startAnimation();
			}
			return false;
		}

		public void onShowPress(MotionEvent e) {
			Log.d(LOG_TAG, "onShowPress");
		}

		public boolean onSingleTapUp(MotionEvent e) {
			Log.d(LOG_TAG, "onSingleTapUp");

			if (mSelectCorner != Corner.None) {
				if (mSelectCorner == Corner.LeftTop) {
					if (scrollX < layoutWidth / 2) {
						aniStopPos = new Point(0, 0);
					} else {
						aniStopPos = new Point(2 * layoutWidth, 0);
					}
				} else if (mSelectCorner == Corner.RightTop) {
					if (scrollX < layoutWidth / 2) {
						aniStopPos = new Point(-layoutWidth, 0);
					} else {
						aniStopPos = new Point(layoutWidth, 0);
					}
				} else if (mSelectCorner == Corner.LeftBottom) {
					if (scrollX < layoutWidth / 2) {
						aniStopPos = new Point(0, layoutHeight);
					} else {
						aniStopPos = new Point(2 * layoutWidth, layoutHeight);
					}
				} else if (mSelectCorner == Corner.RightBottom) {
					if (scrollX < layoutWidth / 2) {
						aniStopPos = new Point(-layoutWidth, layoutHeight);
					} else {
						aniStopPos = new Point(layoutWidth, layoutHeight);
					}
				}
				aniStartPos = new Point((int) scrollX, (int) scrollY);
				aniTime = 800;
				mState = BookState.ABOUT_TO_ANIMATE;
				closeBook = true;
				aniStartTime = new Date();
				mBookView.startAnimation();
			}
			return false;
		}
	}




原文链接:http://blog.youkuaiyun.com/peijiangping1989/article/details/7227625

 

 

相关阅读http://blog.163.com/zmhot88@126/blog/static/16984664720109182392763/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值