欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

实例详解用户输入 i. 检测常用手势

程序员文章站 2024-02-20 13:33:40
效果:一个手指实现(所有手势事件)和(部分事件的); a. 所有手势 activity_main.xml

效果:一个手指实现(所有手势事件)和(部分事件的);

a. 所有手势

activity_main.xml

<textview 
android:id="@+id/gesture" 
android:layout_width="match_parent" 
android:layout_height="150dp" 
android:gravity="center" 
android:text="单击手势变化" /><textview 
android:id="@+id/doubletap" 
android:layout_width="match_parent" 
android:layout_height="150dp" 
android:gravity="center" 
android:text="双击手势变化" /> 

mainactivity.java

public class mainactivity extends activity
{ 
private static final string log_tag = "hellogesture"; 
private gesturedetector mgesturedetector = null; 
private textview mgesturetextview = null; 
private textview mdoubletaptextview = null; 
@override 
protected void oncreate(bundle savedinstancestate) 
{ 
super.oncreate(savedinstancestate); 
setcontentview(r.layout.activity_main); 
mgesturetextview = (textview) findviewbyid(r.id.gesture); 
mdoubletaptextview = (textview) findviewbyid(r.id.doubletap); 
// 构造gesturedetector对象,传入监听器对象 
mgesturedetector = new gesturedetector(this, mongesturelistener); 
// 传入双击监听器对象 
mgesturedetector.setondoubletaplistener(mdoubletaplistener); 
} 
@override public boolean ontouchevent(motionevent event) 
{ 
// 在ontouchevent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的 
mgesturedetector.ontouchevent(event); 
return super.ontouchevent(event); 
} 
private ongesturelistener mongesturelistener = new ongesturelistener()
{ 
@override 
public boolean onsingletapup(motionevent e) 
{ 
log.i(log_tag, "onsingletapup: " + e.tostring()); 
mgesturetextview.settext("onsingletapup: "); 
return false; 
} 
@override 
public void onshowpress(motionevent e) 
{ 
log.i(log_tag, "onshowpress: " + e.tostring()); 
mgesturetextview.settext("onshowpress: "); 
} 
@override 
public boolean onscroll(motionevent e1, motionevent e2, 
float distancex, float distancey)
{ 
log.i(log_tag, "onscroll: " + e1.tostring() + ", " + e2.tostring()); 
mgesturetextview.settext("onscroll "); 
return false; 
} @override 
public void onlongpress(motionevent e)
{ 
log.i(log_tag, "onlongpress: " + e.tostring()); 
mgesturetextview.settext("onlongpress: "); 
} 
@override 
public boolean onfling(motionevent e1, motionevent e2, float velocityx, 
float velocityy) 
{ 
log.i(log_tag, "onfling: " + e1.tostring() + ", " + e2.tostring()); 
mgesturetextview.settext("onfling "); 
return false; 
} 
@override 
public boolean ondown(motionevent e) 
{ 
log.i(log_tag, "ondown: " + e.tostring()); 
mgesturetextview.settext("ondown: "); 
return false; 
} 
}; private ondoubletaplistener mdoubletaplistener = new ondoubletaplistener() 
{ 
@override 
public boolean onsingletapconfirmed(motionevent e) 
{ 
log.i("log_tag", "onsingletapconfirmed: " + e.tostring()); 
mdoubletaptextview.settext("onsingletapconfirmed: "); 
return false; 
} 
@override 
public boolean ondoubletapevent(motionevent e) 
{ 
log.i("log_tag", "ondoubletapevent: " + e.tostring()); 
mdoubletaptextview.settext("ondoubletapevent: "); 
return false; 
} 
@override 
public boolean ondoubletap(motionevent e) 
{ 
log.i("log_tag", "ondoubletap: " + e.tostring()); 
mdoubletaptextview.settext("ondoubletap: "); 
return false; 
} 
};} 

b. 部分手势

如果你仅仅只想处理几种手势,你可以选择继承gesturedetector.simpleongesturelistener类,而不是实现
gesturedetector.ongesturelistener接口

mainactivity.java

public class mainactivity extends activity 
{ 
private gesturedetector mgesturedetector = null; 
private textview mgesturetextview = null; 
private textview mdoubletaptextview = null; 
@override 
protected void oncreate(bundle savedinstancestate) 
{ 
super.oncreate(savedinstancestate); 
setcontentview(r.layout.activity_main); 
mgesturetextview = (textview) findviewbyid(r.id.gesture); 
mdoubletaptextview = (textview) findviewbyid(r.id.doubletap); 
// 构造gesturedetector对象,传入监听器对象 
mgesturedetector = new gesturedetector(this, new mygesturelistener()); 
// 传入双击监听器对象 
} 
@override 
public boolean ontouchevent(motionevent event) 
{ 
// 在ontouchevent方法中将事件传递给手势检测对象,否则手势监听对象中的回调函数是不会被调用的 
this.mgesturedetector.ontouchevent(event); 
return super.ontouchevent(event); 
} 
class mygesturelistener extends gesturedetector.simpleongesturelistener 
{ 
private static final string debug_tag = "gestures"; 
@override 
public boolean onfling(motionevent e1, motionevent e2, float velocityx, 
float velocityy) 
{ 
log.d(debug_tag, "onfling: " + e1.tostring() + ", " + e2.tostring()); 
mgesturetextview.settext("onfling "); 
return false; 
} 
@override 
public boolean ondown(motionevent e)
{ 
log.d(debug_tag, "ondown: " + e.tostring()); 
mgesturetextview.settext("ondown: "); 
return false; 
} 
}}