GestureDetector的用法和介绍

来源:互联网 发布:明朝灭亡的原因 知乎 编辑:程序博客网 时间:2024/06/06 00:14

类描述:

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



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小米5手机变卡了怎么办 小米5变卡了怎么办 小米手机充电无反应怎么办 小米6相机卡顿怎么办 华为手机玩游戏发热怎么办 华为手机变慢了怎么办 华为p10手机变慢怎么办 华为手机账户密码忘记了怎么办 QQ浏览器无法加载插件怎么办 电脑开了机黑屏怎么办 扫描仪打不开运单扫描怎么办 打印机不支持64位系统怎么办 xp系统dnf闪退怎么办 w10电脑所有程序都打不开怎么办 安卓手机太卡怎么办 系统装到f盘了怎么办 虚拟机占c盘内存怎么办 外机连无线虚拟机显示受限怎么办 使用msdn下载解压后怎么办 路由80端口被占用怎么办 c盘拒绝粘贴文件怎么办 oracle数据库密码忘了怎么办 电脑开机时不显示用户名怎么办? xp系统忘记开机密码怎么办 电脑开机密码忘了怎么办 c盘满了怎么办win10 win10电脑开机密码忘了怎么办 win10的开机密码忘了怎么办 u盘中了exe病毒怎么办 眼睛长个麦粒豆怎么办 苹果手机sdk授权失败怎么办 小米5王者荣耀卡怎么办 华为p9手机电池不耐用怎么办 华为g9青春版耗电快怎么办 华为手机摄像头坏了怎么办 华为p10摄像头玻璃划痕怎么办? 华为g9手机音量小怎么办 华为7pius太卡怎么办 华为畅享7plus卡怎么办 华为p9屏幕进水变颜色怎么办? 玩王者荣耀卡退怎么办