星空网 > 软件开发 > 操作系统

手势交互之GestureDetector

GsetureDetector

交互过程

  1. 触屏的一刹那,触发MotionEvent事件
  2. 被OnTouchListener监听,在onTouch()中获得MotionEvent对象
  3. GestureDetector转发MotionEvent对象到OnGestureListener
  4. OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈

内部类:

1.OnGestureListener:单击类

方法
  1. 单击onDown(MotionEvent e)
  2. 抬起onSingleTapUp(MotionEvent e)
  3. 短按onShowPress(MotionEvent e)
  4. 长按onLongPress(MotionEvent e)
  5. 滚动onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY)
  6. 滑动onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY)

2.OnDoubleTapListener:双击

方法
  1. 双击OnDoubleTap(MotionEvent e)
  2. 双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)?应用例子?
  3. 单击确定onSingleTapConfirmed(MotionEvent e)?

3.SimpleOnGestureListener

概述: SimpleOnGestureListener实现了上面的连个接口 OnGestureListener and OnDoubleTapListener,可以通过继承这个类来实现你所想实现的手势交互动作。需要的动作可以在接口里找对应的方法,如果方法返回false就是什么都不做。

运用例子

效果

这个例子在主界面加载一个图片,然后左右滑动提示相应的文字。

过程
  1. OnTouchListener监听,在onTouch中获得MotionEvent对象
img.setOnTouchListener(new OnTouchListener() {						@Override//捕获的触摸屏发生的event事件			public boolean onTouch(View v, MotionEvent event) {				// TODO Auto-generated method stub				return true;//记得这里改为ture			}		});
  1. Activity建立一个继承于SimpleOnGestureListener的类
class MyGestureListener extends SimpleOnGestureListener{  	@Override  	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  			float velocityY) {  		// TODO Auto-generated method stub  		//e1和e2分别是开始和结束的动作对象  		//通过e1和e2位置的比较判断手势的动作  		if (e1.getX() - e2.getX() > 50) {		      Toast.makeText(MainActivity.this, "从右向左滑动",0).show();			}else if(e2.getX() - e1.getX() >50){			  Toast.makeText(MainActivity.this, "从左向右滑动",0).show();			}  		return super.onFling(e1, e2, velocityX, velocityY);  	}  }

      3.获得一个GestureDetextor对象(声明省略了)

  gestureDetector = new GestureDetector(MainActivity.this, new MyGestureListener());

      4.在onTouch方法中gestureDetector转发MotionEvent对象到OnGestureListener(这里是MyGestureListener)

  gestureDetector.onTouchEvent(event);

      5.MyGestureListener获得该对象,根据该对象封装的信息做出合适的反馈(在图片上左右滑动提示对应的文字)




原标题:手势交互之GestureDetector

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

发林查班海运:https://www.goluckyvip.com/tag/99267.html
亚历山大港海运:https://www.goluckyvip.com/tag/99268.html
东南亚港口:https://www.goluckyvip.com/tag/99269.html
如何入驻Joom:https://www.goluckyvip.com/tag/9927.html
青岛海运东南亚:https://www.goluckyvip.com/tag/99270.html
东南亚航运:https://www.goluckyvip.com/tag/99271.html
南京到普陀风景区 南京到普陀风景区怎么走:https://www.vstour.cn/a/404228.html
十堰有哪些官方网站:https://www.vstour.cn/a/404229.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流