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

手势交互GestureDetector的教程

GsetureDetector

交互过程

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

内部类:两个**接口

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)

OnDoubleTapListener:双击

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

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/98130.html
东南亚头程物流:https://www.goluckyvip.com/tag/98131.html
东南亚cod物流价格:https://www.goluckyvip.com/tag/98133.html
东南亚智慧物流:https://www.goluckyvip.com/tag/98134.html
东南亚小包物流:https://www.goluckyvip.com/tag/98136.html
东南亚物流小包:https://www.goluckyvip.com/tag/98137.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流