类描述:
通过系统提供的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; } }