当前位置: 移动技术网 > 移动技术>移动开发>Android > 详谈OnTouchListener与OnGestureListener的区别

详谈OnTouchListener与OnGestureListener的区别

2019年07月24日  | 移动技术网移动技术  | 我要评论

android事件处理机制是基于listener实现的,比如触摸屏相关的事件,是通过ontouchlistener实现的;而手势是通过ongesturelistener实现的,那么这两者有什么关联呢?

ontouchlistener

ontouchlistener接口中包含一个ontouch()方法,直接看一个例子:

public class mainactivity extends activity implements ontouchlistener {
	public void oncreate(bundle outstate) {
  	super.oncreate(outstate);
  	setcontentview(r.layout.main);
  	textview tv = (textview) findviewbyid(r.id.tv);
  	tv.setontouchlistener(this);
  }

  public boolean ontouch(view v, motionevent event) {
  	toast.maketext(this, "touch touch", toast.length_short).show();
		return false ; 
  }
}

我们可以通过motionevent的getaction()方法来获取touch事件的类型,包括 action_down(按下触摸屏), action_move(按下触摸屏后移动受力点), action_up(松开触摸屏)和action_cancel(不会由用户直接触发)。借助对于用户不同操作的判断,结合getrawx()、 getrawy()、getx()和gety()等方法来获取坐标后,我们可以实现诸如拖动某一个按钮,拖动滚动条等功能。

可以看到ontouchlistener只能监听到三种触摸事件,即按下,移动,松开,如果想要监听到双击、滑动、长按等复杂的手势操作,这个时候就必须得用到ongesturelistener了。

ongesturelistener

接着上面的例子,这次加入手势监听:

public class mainactivity extends activity implements ontouchlistener, ongesturelistener {
	private gesturedetector mgesturedetector; 

	public void oncreate(bundle outstate) {
  	super.oncreate(outstate);
  	setcontentview(r.layout.main);

  	mgesturedetector = new gesturedetector(this);
  	textview tv = (textview) findviewbyid(r.id.tv);
  	tv.setontouchlistener(this);
  }

  public boolean ontouch(view v, motionevent event) {
  	return mgesturedetector.ontouchevent(event);
  }

  // 用户轻触触摸屏,由1个motionevent action_down触发 
  public boolean ondown(motionevent arg0) {
		log.i("mygesture", "ondown");
		toast.maketext(this, "ondown", toast.length_short).show();   
		return true;
	}

	// 用户轻触触摸屏,尚未松开或拖动,由一个1个motionevent action_down触发, 注意和ondown()的区别,强调的是没有松开或者拖动的状态
	public void onshowpress(motionevent e) {
		log.i("mygesture", "onshowpress");      
		toast.maketext(this, "onshowpress", toast.length_short).show();
	}

	// 用户(轻触触摸屏后)松开,由一个1个motionevent action_up触发

	public  boolean onsingletapup(motionevent e) {
		log.i("mygesture", "onsingletapup");
		toast.maketext(this, "onsingletapup", toast.length_short).show();
		return true;
	}

	// 用户按下触摸屏、快速移动后松开,由1个motionevent action_down, 多个action_move, 1个action_up触发    

	public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) {  
		log.i("mygesture", "onfling");
		toast.maketext(this, "onfling", toast.length_long).show();   
		return true;
	}

 	// 用户按下触摸屏,并拖动,由1个motionevent action_down, 多个action_move触发

	public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) {      
		log.i("mygesture", "onscroll");
 		toast.maketext(this, "onscroll", toast.length_long).show();
		return true;
 	}

 	// 用户长按触摸屏,由多个motionevent action_down触发

	public  void onlongpress(motionevent e) {
		log.i("mygesture", "onlongpress");
		toast.maketext(this, "onlongpress", toast.length_long).show();
 	}
}

以上这篇详谈ontouchlistener与ongesturelistener的区别就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网