android使用gesturedetector手势识别示例分享
public class mygesturelintener extends simpleongesturelistener {
private context context;
public mygesturelintener(context context) {
super();
this.context = context;
}
// 单击,触摸屏按下时立刻触发
/*@override
public boolean ondown(motionevent e) {
// todo auto-generated method stub
toast.maketext(context, "down " + e.getaction(), toast.length_short)
.show();
return true;
}*/
// 双击,手指在触摸屏上迅速点击第二下时触发
@override
public boolean ondoubletap(motionevent e) {
// todo auto-generated method stub
return super.ondoubletap(e);
}
// 双击的按下跟抬起各触发一次
@override
public boolean ondoubletapevent(motionevent e) {
// todo auto-generated method stub
return super.ondoubletapevent(e);
}
// 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势
@override
public boolean onfling(motionevent e1, motionevent e2, float velocityx,
float velocityy) {
// todo auto-generated method stub
return super.onfling(e1, e2, velocityx, velocityy);
}
// 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发
@override
public void onlongpress(motionevent e) {
// todo auto-generated method stub
toast.maketext(context, "long " + e.getaction(), toast.length_short)
.show();
}
// 滚动,触摸屏按下后移动
@override
public boolean onscroll(motionevent e1, motionevent e2, float distancex,
float distancey) {
toast.maketext(context, "onscroll " + e2.getaction(), toast.length_short)
.show();
return true;
}
// 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会
@override
public void onshowpress(motionevent e) {
// todo auto-generated method stub
toast.maketext(context, "show " + e.getaction(), toast.length_short)
.show();
}
// 单击确认,即很快的按下并抬起,但并不连续点击第二下
/*@override
public boolean onsingletapconfirmed(motionevent e) {
// todo auto-generated method stub
toast.maketext(context, "onsingletapconfirmed " + e.getaction(), toast.length_short)
.show();
return true;
}*/
// 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势)
/*@override
public boolean onsingletapup(motionevent e) {
// todo auto-generated method stub
toast.maketext(context, "onsingletapup " + e.getaction(), toast.length_short)
.show();
return true;
}*/
public class mainactivity extends activity {
private gesturedetector mgesturedetector;//手势对象
private mygesturelintener mygesturelintener;//手势监听的接口对象
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
mygesturelintener = new mygesturelintener(this);
//手势对象的构造方法
mgesturedetector = new gesturedetector(this,
mygesturelintener);
}
/**gesturedetector类的ontouchevent方法用来辨别不同的手势*/
@override
public boolean ontouchevent(motionevent event) {
boolean b = false;
int i = event.getaction();
int j = motionevent.action_move;
system.out.println(i+"<----------------->"+j);
b = mgesturedetector.ontouchevent(event);
if (b) {
intent in = new intent();
in.setclass(this, testactivity.class);
startactivity(in);
}
return b;
}
@override
public boolean oncreateoptionsmenu(menu menu) {
// inflate the menu; this adds items to the action bar if it is present.
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
}