当前位置: 移动技术网 > 移动技术>移动开发>Android > android中处理各种触摸事件的方法浅谈

android中处理各种触摸事件的方法浅谈

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

android里有两个类
android.view.gesturedetector
android.view.gesturedetector.simpleongesturelistener
(另外android.widget.gallery好像是更牛x的ongesturelistener )
1)
新建一个类继承simpleongesturelistener,hahagesturedetectorlistener
可以实现以下event事件。
boolean ondoubletap(motionevent e)
解释:双击的第二下touch down时触发
boolean ondoubletapevent(motionevent e)
解释:双击的第二下touch down和up都会触发,可用e.getaction()区分。
boolean ondown(motionevent e)
解释:touch down时触发
boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy)
解释:touch了滑动一点距离后,up时触发。
void onlongpress(motionevent e)
解释:touch了不移动一直touch down时触发
boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey)
解释:touch了滑动时触发。
void onshowpress(motionevent e)
解释:touch了还没有滑动时触发
(与ondown,onlongpress比较
ondown只要touch down一定立刻触发。
而touchdown后过一会没有滑动先触发onshowpress再是onlongpress。
所以touchdown后一直不滑动,ondown->onshowpress->onlongpress这个顺序触发。

boolean onsingletapconfirmed(motionevent e)
boolean onsingletapup(motionevent e)
解释:上面这两个函数都是在touch down后又没有滑动(onscroll),又没有长按(onlongpress),然后touchup时触发。
点击一下非常快的(不滑动)touchup:
ondown->onsingletapup->onsingletapconfirmed
点击一下稍微慢点的(不滑动)touchup:
ondown->onshowpress->onsingletapup->onsingletapconfirmed
2)在view的新建一个gesturedetector的对象。
构造函数里
gesturedetector = new gesturedetector(new hahagesturedetectorlistener());
然后在view的ontouchevent里以下这样用,就可以在刚才1)弄的事件里写自己的代码了。

复制代码 代码如下:

@override
public boolean ontouchevent(motionevent event) {
gesturedetector.ontouchevent(event);
}
mtouchlistener = new ontouchlistener() {
 @override
 public boolean ontouch(view v, motionevent event) {
 // todo auto-generated method stub
 float x = event.getxprecision()*event.getx()+event.getx();
 float y = event.getyprecision()*event.gety()+event.gety();
 switch (event.getaction()) {
 case motionevent.action_down:

 break;
 case motionevent.action_move:
 mtouchtimes++;
 if (mtouchtimes > touch_times) {
// 根据方向计算角度
 if (mcurrentorientation==deviceorientation.landscape) {
 mangle = math.todegrees(math.atan2(y - 480 / 2, x))+90;
 } else {
 mangle = -math.todegrees(math.atan2(y - 480 / 2, 320-x))+90;
 }

 log.w("angle", "mangle:"+mangle);
 }
 break;
 case motionevent.action_up:
 if (mtouchtimes > touch_times) {

 } else {

 }
 mtouchtimes = 0;
 break;
 default:
 break;
 }
 return true;
 }
 };
mview.setontouchlistener(mtouchlistener);

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

相关文章:

验证码:
移动技术网