GestureDetector的用法和介绍


类描述:

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

下面贴出一个类做参考、

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



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值