Android 屏幕点击手势判断

来源:互联网 发布:网络防骗小常识 编辑:程序博客网 时间:2024/04/30 15:40

近期在做一个项目,其中涉及到屏幕点击事件的手势判断,比如是单击还是双击,是左滑还是上滑等。


要获取到屏幕的点击事件并不难,只需要重载Acivity的onTouchevent(MotionEvent ev)方法,或者dispatchTouchEvent(MotionEvent ev)方法即可,其中传入的形参MotionEvent ev,ev即为点击事件,我们可以获取到ev,然后得到点击事件ev的信息,比如点击的x坐标可以通过ev.getX()得到等。


那么如何识别是什么手势呢?我们可以想到,单击与双击可以通过比较两次的时间差来判断,这个时间差阈值是由自己设定的。

而长按可以通过TouchEvent事件的Down动作的时间,和Up动作的时间差来判断,当然了,这个时间差阈值也是自己设定的。

此两种比较简单,不再列代码。


那么如何判断是否是滑动,并判断是什么滑动呢?

我们知道,TouchEvent有3中类型,一种是Down,就是按下的动作,一种是Move,就是滑动的动作,还有一种就是Up,于是,我们可以通过判断按下(Down)和抬起(Up)的位移来判断是什么类型的滑动,代码如下,

<span style="white-space:pre"></span>public boolean dispatchTouchEvent(MotionEvent ev) {<span style="white-space:pre"></span>// TODO Auto-generated method stub<span style="white-space:pre"></span>int iAction = ev.getAction();<span style="white-space:pre"></span>switch (iAction) {<span style="white-space:pre"></span>case MotionEvent.ACTION_DOWN:<span style="white-space:pre"></span>int down_x = (int) ev.getX();<span style="white-space:pre"></span>int down_y = (int) ev.getY();<span style="white-space:pre"></span><span style="white-space:pre"></span>break;<span style="white-space:pre"></span>case MotionEvent.ACTION_MOVE:<span style="white-space:pre"></span>int move_x = (int) ev.getX();<span style="white-space:pre"></span>int move_y = (int) ev.getY();<span style="white-space:pre"></span>if (touch_mode == 0) {<span style="white-space:pre"></span>touch_mode = 1;<span style="white-space:pre"></span>first_move_p_x = move_x;<span style="white-space:pre"></span>first_move_p_y = move_y;<span style="white-space:pre"></span>}<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>case MotionEvent.ACTION_UP:<span style="white-space:pre"></span>int up_x = (int) ev.getX();<span style="white-space:pre"></span>int up_y = (int) ev.getY();<span style="white-space:pre"></span><span style="white-space:pre"></span>int x_dis = up_x - first_move_p_x;<span style="white-space:pre"></span>int y_dis = up_y - first_move_p_y;<span style="white-space:pre"></span>if (Math.abs(x_dis) > Math.abs(y_dis)) {<span style="white-space:pre"></span>if ((x_dis) > 100) {<span style="white-space:pre"></span>System.out.println("右滑动");<span style="white-space:pre"></span>} else if ((x_dis) < -100) {<span style="white-space:pre"></span>System.out.println("左滑动");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>} else {<span style="white-space:pre"></span>if ((y_dis) > 100) {<span style="white-space:pre"></span>System.out.println("下滑动");<span style="white-space:pre"></span>} else if ((y_dis) < -100) {<span style="white-space:pre"></span>System.out.println("上滑动");<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}<span style="white-space:pre"></span>touch_mode = 0;<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>default:<span style="white-space:pre"></span>break;<span style="white-space:pre"></span>}<span style="white-space:pre"></span><span style="white-space:pre"></span>return super.dispatchTouchEvent(ev);<span style="white-space:pre"></span>}
其中,
touch_mode和<span style="font-family: Arial, Helvetica, sans-serif;">first_move_p_x,</span><span style="font-family: Arial, Helvetica, sans-serif;">first_move_p_y为全局变量,</span>

</pre>touch_mode的作用是,第一次滑动时改变其值,标记为滑动,并记下第一次滑动的位置<pre name="code" class="html">first_move_p_x和<span style="font-family: Arial, Helvetica, sans-serif;">first_move_p_y</span>

然后在Up动作中,判断x方向和y方向的位移来判断滑动即可,当然,也得设置一个阈值,来判断是否是滑动,本代码中设置的是100个像素值。



以上就是自己实现手势识别的代码。

其实,Android本身已经提供了判断手势的API,包括在GestureDetector中,代码如下,

<pre name="code" class="java">GestureDetector <span style="font-family: Arial, Helvetica, sans-serif;">getstureDetector = new GestureDetector(context, new mGestureListener());</span>
<span style="font-family: Arial, Helvetica, sans-serif;">其中,</span><span style="font-family: Arial, Helvetica, sans-serif;">mGestureListener重载了</span><span style="font-family: Arial, Helvetica, sans-serif;">SimpleOnGestureListener方法,</span>
<span style="font-family: Arial, Helvetica, sans-serif;">class mGestureListener extends GestureDetector.SimpleOnGestureListener</span>

<span style="font-family: Arial, Helvetica, sans-serif;">SimpleOnGestureListener内有几个接口,</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><p>public boolean onSingleTapUp(MotionEvent ev) {</p><p> <wbr>  <wbr> Log.d("onSingleTapUp",ev.toString());</wbr></wbr></p><p> <wbr>  <wbr> return true;</wbr></wbr></p><p> <wbr> }</wbr></p><p> <wbr> @Override</wbr></p><p> <wbr> public void onShowPress(MotionEvent ev) {</wbr></p><p> <wbr>  <wbr> Log.d("onShowPress",ev.toString());</wbr></wbr></p><p> <wbr> }</wbr></p><p> <wbr> @Override</wbr></p><p> <wbr> public void onLongPress(MotionEvent ev) {</wbr></p><p> <wbr>  <wbr> Log.d("onLongPress",ev.toString());</wbr></wbr></p><p> <wbr> }</wbr></p><p>…</p>
其具体接口的含义,可以看其文档,很容易看懂。

但是,关于该API是怎么实现的,是不是和自己实现的机制一样,我不确定,等有时间研究了源码再来分享。

第一次发博客,谢谢大家支持。






0 0
原创粉丝点击