通常用到的手势识别包括OnTouchListener和GestureDetector.OnGestureListener。
OnTouchListener是一切触摸事件的监听器,它会接收到一切的触摸事件。当我们点击屏幕、滑动、长按都会出现各种效果,其实这是由OnTouchListener监听到了,然后交给GestureDetector处理,GestureDetector通过分析知道当前的触摸事件是什么样的,再告诉OnGestureListener,就调用OnGestureListener中的对应方法处理事件。因为OnGestureListener是GestureDetector类用的接口,当然是通过 . 来引用了。说白了,GestureDetector只是封装了一些常用的手势,方便我们使用。在OnTouchListener中,我们完全可以自定义手势,而不用GestureDetector来分析。
让事实说话吧,举例子
package org.robam.touchtest;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnTouchListener,
OnGestureListener {
TextView textview;
ScrollView scrollview;
private static final String TAGTOUCH = "OnTouchListener";
private static final String TAGGESTRURE = "OnGestureListener";
private GestureDetector GD;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.textview);
textview.setOnTouchListener(this); //给TextView设置触摸的监听器,它才会响应触摸事件
GD = new GestureDetector(this); //new 一个手势识别器
}
//这是OnTouchListener接口的唯一方法,TextView的触摸事件首先由它处理
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.e(TAGTOUCH, "onTouchListener -- onTouch");
return GD.onTouchEvent(event); //这一句话很关键,调用GestureDetector的onTouchEvent方法,意思是交GestureDetector处理
}
//GestureDetector处理完后,如果匹配一下6个方法,就给相应的方法处理,以下6个方法都是OnGestureListener中定义的
/* 顾名思义手指按下的时候就会触发
* @see android.view.GestureDetector.OnGestureListener#onDown(android.view.MotionEvent)
*/
@Override
public boolean onDown(MotionEvent e) {
Log.e(TAGGESTRURE, "OnGestureListener -- onDown");
return true; //注意这个返回值好像要true,后面的onFling和onScroll才能被处理哦!
}
/*
* The user has performed a down MotionEvent and not performed a move or up yet.
* This event is commonly used to provide visual feedback to the user to let them know that
* their action has been recognized i.e. highlight an element.
* 当用户按下没有移动或者放开手指时触发,根据名字可以看出,它是用来show显示一些提示信息的。
* 这个方法在按紧0.18秒触发,不能滑动也不能放开
* 注意跟下面的onLongPress区别,其实只不过这个时间短一点,本质是一样的
* @see android.view.GestureDetector.OnGestureListener#onShowPress(android.view.MotionEvent)
*/
@Override
public void onShowPress(MotionEvent e) {
Log.e(TAGGESTRURE, "OnGestureListener -- onShowPress");
}
/*
* 手指按下后弹开的时候触发
* 注意,只要手指滑动了再弹开,它是不会触发的。而且长按后也不会触发。也就是在按下0到0.68秒间放开触发
* @see android.view.GestureDetector.OnGestureListener#onSingleTapUp(android.view.MotionEvent)
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.e(TAGGESTRURE, "OnGestureListener -- onSingleTabUp");
return true;
}
/*
* 从名字可以看出,它是滚动的时候触发。
* 那它在滚动的过程中会触发多少次呢?、
* 一秒钟内会触发很多次,但我暂时不知道在哪里找确切的答案,onTouch触发多少次就是多少。
* 还有,这也是要滑动一定的距离才会触发,到底多少,暂时找不到答案,原谅我!
* @see android.view.GestureDetector.OnGestureListener#onScroll(android.view.MotionEvent, android.view.MotionEvent, float, float)
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
Log.e(TAGGESTRURE, "OnGestureListener -- onScroll --- " + e1.getX()
+ "----" + e2.getX());
return true;
}
/*
* 长按触发事件,不用多说,前提是不能滑动。
* 关键是:多少秒算长按呢?没错,就是 0.18 + 0.5 = 0.68 秒
* @see android.view.GestureDetector.OnGestureListener#onLongPress(android.view.MotionEvent)
*/
@Override
public void onLongPress(MotionEvent e) {
Log.e(TAGGESTRURE, "OnGestureListener -- onLongPress");
}
/*
* 这就是滑动之后放开手指触发的事件,当然触发也是有条件的,
* 就是... 滑动速度要达到系统定义的最小值。跟手机的dip息息相关。
* 手机dip/160 * 100 * 50 + 0.5,就是每秒要划过这么多像素才会触发。
* @see android.view.GestureDetector.OnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
Log.e(TAGGESTRURE, "OnGestureListener -- onFling");
return true;
}
}
Layout文件如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_world"
android:textSize="30sp" />
</LinearLayout>
运行程序后在LogCat可以看到各个事件的触发情况。
其实上面解释 都差不多了,就不再啰嗦!我是从分析GestureDetector源码得到的,最关键的当然是识别手势的具体函数 onTouchEvent……
有什么不对请拍砖~
本文详细阐述了Android中手势识别的基本原理,包括如何使用OnTouchListener和GestureDetector.OnGestureListener接口进行手势事件处理,以及这些组件如何协同工作来实现丰富的触摸交互体验。通过实例代码演示了如何在实际应用中实现特定的手势识别功能。

2331

被折叠的 条评论
为什么被折叠?



