加入收藏 | 设为首页 | 会员中心 | 我要投稿 焦作站长网 (https://www.0391zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 系统 > 正文

Android 手势检测 - GestureDetector 全方位分析

发布时间:2021-10-19 10:39:08 所属栏目:系统 来源:互联网
导读:前言 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等;Android sdk给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势;今天就来学习下。 GestureDetector介绍 GestureDetector这个类对外提供了两个接口和一个外部
前言
当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等;Android sdk给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势;今天就来学习下。
 
GestureDetector介绍
GestureDetector这个类对外提供了两个接口和一个外部类
 
接口:OnGestureListener,OnDoubleTapListener
内部类:SimpleOnGestureListener
 
GestureDetector类介绍
 
private class Gesturelistener implements GestureDetector.OnGestureListener{ 
public boolean onDown(MotionEvent e) { 
// TODO Auto-generated method stub 
return false; 
public void onShowPress(MotionEvent e) { 
// TODO Auto-generated method stub 
public boolean onSingleTapUp(MotionEvent e) { 
// TODO Auto-generated method stub 
return false; 
public boolean onScroll(MotionEvent e1, MotionEvent e2, 
float distanceX, float distanceY) { 
// TODO Auto-generated method stub 
return false; 
public void onLongPress(MotionEvent e) { 
// TODO Auto-generated method stub 
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
float velocityY) { 
// TODO Auto-generated method stub 
return false; 
这里总共重写了六个函数
 
1、OnDown(MotionEvent e):用户按下屏幕就会触发;
 
2、onShowPress(MotionEvent e):如果是按下的时间超过瞬间,而且在按下的时候没有松开或者是拖动的,那么onShowPress就会执行
 
3、onLongPress(MotionEvent e):长按触摸屏,超过一定时长,就会触发这个事件,触发顺序:onDown->onShowPress->onLongPress
 
4、onSingleTapUp(MotionEvent e):一次单独的轻击抬起操作,也就是轻击一下屏幕,立刻抬起来,才会有这个触发,当然,如果除了Down以外还有其它操作,那就不再算是Single操作了,所以也就不会触发这个事件;触发顺序:Touchup:onDown->onSingleTapUp->onSingleTapConfirmed ;
 
onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发;
 
参数解释:
 
e1:第1个ACTION_DOWN MotionEvent
e2:最后一个ACTION_MOVE MotionEvent
velocityX:X轴上的移动速度,像素/秒
velocityY:Y轴上的移动速度,像素/秒
5、onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖动事件;无论是用手拖动view,或者是以抛的动作滚动,都会多次触发,这个方法在ACTION_MOVE动作发生时就会触发;
 
滑屏:手指触动屏幕后,稍微滑动后立即松开
 
onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
 
拖动
 
onDown------》onScroll----》onScroll------》onFiling
 
无论是滑屏,还是拖动,影响的只是中间OnScroll触发的数量多少而已,最终都会触发onFling事件;

(编辑:焦作站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读