android 手势识别,Android GestureDetector:那些你一定要学会的手势识别应用(含实例讲解)...

2cb7ec3d3d5a

前言

手势识别在Android开发的应用非常常见

今天carson将详细给大家讲解Android手势识别类:GestureDetector类的使用。(含实例讲解)

目录

2cb7ec3d3d5a

简介

2cb7ec3d3d5a

下面,我将结合实例,详细介绍GestureDetector的使用接口 & 使用类。

接口1:OnGestureListener

1. 作用

检测用户在屏幕的以下操作:按下瞬间、按压、长按、轻击、快速滑屏、拖动

2. 使用步骤

// 步骤1:创建手势检测器实例 & 传入OnGestureListener接口(需要复写对应方法)

// 构造函数有3个,常用的是第二个

// 1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);

// 2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);

// 3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

// 1. 用户轻触触摸屏

public boolean onDown(MotionEvent e) {

Log.i("MyGesture", "onDown");

return false;

}

// 2. 用户轻触触摸屏,尚未松开或拖动

// 与onDown()的区别:无松开 / 拖动

// 即:当用户点击的时,onDown()就会执行,在按下的瞬间没有松开 / 拖动时onShowPress就会执行

public void onShowPress(MotionEvent e) {

Log.i("MyGesture", "onShowPress");

}

// 3. 用户长按触摸屏

public void onLongPress(MotionEvent e) {

Log.i("MyGesture", "onLongPress");

}

// 4. 用户轻击屏幕后抬起

public boolean onSingleTapUp(MotionEvent e) {

Log.i("MyGesture", "onSingleTapUp");

return true;

}

// 5. 用户按下触摸屏 & 拖动

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY) {

Log.i("MyGesture", "onScroll:");

return true;

}

// 6. 用户按下触摸屏、快速移动后松开

// 参数:

// e1:第1个ACTION_DOWN MotionEvent

// e2:最后一个ACTION_MOVE MotionEvent

// velocityX:X轴上的移动速度,像素/秒

// velocityY:Y轴上的移动速度,像素/秒

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

Log.i("MyGesture", "onFling");

return true;

}

});

// 步骤2-1:让某个View检测手势 - 重写View的onTouch函数,将View的触屏事件交给GestureDetector处理,从而对用户手势作出响应

View.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

mGestureDetector.onTouchEvent(event);

return true; // 注:返回true才能完整接收触摸事件

}

});

// 步骤2-2:让某个Activity检测手势:重写Activity的dispatchTouchEvent函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

mGestureDetector.onTouchEvent(ev); // 让GestureDetector响应触碰事件

super.dispatchTouchEvent(ev); // 让Activity响应触碰事件

return false;

}

3. 实例说明

现在对一个TextView进行手势检测

activity_main.xml

android:orientation="vertical"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

>

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="600dp"

android:text="carson_ho Test"

android:textAppearance="?android:attr/textAppearanceLarge" />

MainActivity.java

public class MainActivity extends AppCompatActivity {

TextView mTextView;

GestureDetector mGestureDetector;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 步骤1:创建手势检测器实例 & 传入OnGestureListener接口(需要复写对应方法)

mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

// 1. 用户轻触触摸屏

public boolean onDown(MotionEvent e) {

Log.i("MyGesture", "onDown");

return false;

}

// 2. 用户轻触触摸屏,尚未松开或拖动

// 与onDown()的区别:无松开 / 拖动

// 即:当用户点击的时,onDown()就会执行,在按下的瞬间没有松开 / 拖动时onShowPress就会执行

public void onShowPress(MotionEvent e) {

Log.i("MyGesture", "onShowPress");

}

// 3. 用户长按触摸屏

public void onLongPress(MotionEvent e) {

Log.i("MyGesture", "onLongPress");

}

// 4. 用户轻击屏幕后抬起

public boolean onSingleTapUp(MotionEvent e) {

Log.i("MyGesture", "onSingleTapUp");

return true;

}

// 5. 用户按下触摸屏 & 拖动

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY) {

Log.i("MyGesture", "onScroll:");

return true;

}

// 6. 用户按下触摸屏、快速移动后松开

// 参数:

// e1:第1个ACTION_DOWN MotionEvent

// e2:最后一个ACTION_MOVE MotionEvent

// velocityX:X轴上的移动速度,像素/秒

// velocityY:Y轴上的移动速度,像素/秒

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

Log.i("MyGesture", "onFling");

return true;

}

});

// 步骤2:让TextView检测手势:重写View的onTouch函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应

mTextView = (TextView) findViewById(R.id.textView);

mTextView.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

mGestureDetector.onTouchEvent(event);

return false;

}

});

}

}

4. 示意图

我在屏幕作出一系列手势进行测试

2cb7ec3d3d5a

接口2:OnDoubleTapListener

1. 作用

检测用户单击、双击屏幕

2. 使用步骤

// 步骤1:创建手势检测器实例

// 注:使用OnDoubleTapListener接口时,需要使用GestureDetector,而GestureDetector的创建则必须传入OnGestureListener接口

// 所以在使用OnDoubleTapListener接口时,也必须实现OnGestureListener接口

// 构造函数有3个,常用的是第二个

// 1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);

// 2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);

// 3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

// 1. 用户轻触触摸屏

public boolean onDown(MotionEvent e) {

Log.i("MyGesture", "onDown");

return false;

}

// 2. 用户轻触触摸屏,尚未松开或拖动

// 与onDown()的区别:无松开 / 拖动

// 即:当用户点击的时,onDown()就会执行,在按下的瞬间没有松开 / 拖动时onShowPress就会执行

public void onShowPress(MotionEvent e) {

Log.i("MyGesture", "onShowPress");

}

// 3. 用户长按触摸屏

public void onLongPress(MotionEvent e) {

Log.i("MyGesture", "onLongPress");

}

// 4. 用户轻击屏幕后抬起

public boolean onSingleTapUp(MotionEvent e) {

Log.i("MyGesture", "onSingleTapUp");

return true;

}

// 5. 用户按下触摸屏 & 拖动

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY) {

Log.i("MyGesture", "onScroll:");

return true;

}

// 6. 用户按下触摸屏、快速移动后松开

// 参数:

// e1:第1个ACTION_DOWN MotionEvent

// e2:最后一个ACTION_MOVE MotionEvent

// velocityX:X轴上的移动速度,像素/秒

// velocityY:Y轴上的移动速度,像素/秒

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

Log.i("MyGesture", "onFling");

return true;

}

});

// 步骤2:创建 & 设置OnDoubleTapListener接口实现类

mGestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {

// 1. 单击事件

// 关于OnDoubleTapListener.onSingleTapConfirmed()和 OnGestureListener.onSingleTapUp()的区别

// onSingleTapConfirmed:再次点击(即双击),则不会执行

// onSingleTapUp:手抬起就会执行

public boolean onSingleTapConfirmed(MotionEvent e) {

Log.i("MyGesture", "onSingleTapConfirmed");

return false;

}

// 2. 双击事件

public boolean onDoubleTap(MotionEvent e) {

Log.i("MyGesture", "onDoubleTap");

return false;

}

// 3. 双击间隔中发生的动作

// 指触发onDoubleTap后,在双击之间发生的其它动作,包含down、up和move事件;

public boolean onDoubleTapEvent(MotionEvent e) {

Log.i("MyGesture", "onDoubleTapEvent");

return false;

}

});

// 步骤3-1:让某个View检测手势 - 重写View的onTouch函数,将View的触屏事件交给GestureDetector处理,从而对用户手势作出响应

View.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

mGestureDetector.onTouchEvent(event);

return true; // 注:返回true才能完整接收触摸事件

}

});

// 步骤3-2:让某个Activity检测手势:重写Activity的dispatchTouchEvent函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

mGestureDetector.onTouchEvent(ev); // 让GestureDetector响应触碰事件

super.dispatchTouchEvent(ev); // 让Activity响应触碰事件

return false;

}

3. 实例说明

现在对一个TextView进行手势检测

activity_main.xml

android:orientation="vertical"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

>

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="600dp"

android:text="carson_ho Test"

android:textAppearance="?android:attr/textAppearanceLarge" />

MainActivity.java

public class MainActivity extends AppCompatActivity {

TextView mTextView;

GestureDetector mGestureDetector;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 步骤1:创建手势检测器实例 & 传入OnGestureListener接口(需要复写对应方法)

mGestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {

// 1. 用户轻触触摸屏

public boolean onDown(MotionEvent e) {

Log.i("MyGesture1", "onDown");

return false;

}

// 2. 用户轻触触摸屏,尚未松开或拖动

// 与onDown()的区别:无松开 / 拖动

// 即:当用户点击的时,onDown()就会执行,在按下的瞬间没有松开 / 拖动时onShowPress就会执行

public void onShowPress(MotionEvent e) {

Log.i("MyGesture", "onShowPress");

}

// 3. 用户长按触摸屏

public void onLongPress(MotionEvent e) {

Log.i("MyGesture", "onLongPress");

}

// 4. 用户轻击屏幕后抬起

public boolean onSingleTapUp(MotionEvent e) {

Log.i("MyGesture", "onSingleTapUp");

return true;

}

// 5. 用户按下触摸屏 & 拖动

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY) {

Log.i("MyGesture", "onScroll:");

return true;

}

// 6. 用户按下触摸屏、快速移动后松开

// 参数:

// e1:第1个ACTION_DOWN MotionEvent

// e2:最后一个ACTION_MOVE MotionEvent

// velocityX:X轴上的移动速度,像素/秒

// velocityY:Y轴上的移动速度,像素/秒

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

Log.i("MyGesture", "onFling");

return true;

}

});

// 步骤2:创建 & 设置OnDoubleTapListener接口实现类

mGestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {

// 1. 单击事件

// 关于OnDoubleTapListener.onSingleTapConfirmed()和 OnGestureListener.onSingleTapUp()的区别

// onSingleTapConfirmed:再次点击(即双击),则不会执行

// onSingleTapUp:手抬起就会执行

public boolean onSingleTapConfirmed(MotionEvent e) {

Log.i("MyGesture", "onSingleTapConfirmed");

return false;

}

// 2. 双击事件

public boolean onDoubleTap(MotionEvent e) {

Log.i("MyGesture", "onDoubleTap");

return false;

}

// 3. 双击间隔中发生的动作

// 指触发onDoubleTap后,在双击之间发生的其它动作,包含down、up和move事件;

public boolean onDoubleTapEvent(MotionEvent e) {

Log.i("MyGesture", "onDoubleTapEvent");

return false;

}

});

// 步骤3:重写View的onTouch函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应

mTextView = (TextView) findViewById(R.id.textView);

mTextView.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

mGestureDetector.onTouchEvent(event);

return true;

}

});

}

}

4. 测试效果

日志效果如下

2cb7ec3d3d5a

使用类:SimpleOnGestureListener

1. 作用

集成了两个接口的手势检测功能

2. 与上述两个接口的区别

OnGestureListener和OnDoubleTapListener接口里的函数都是强制必须重写的

而SimpleOnGestureListener类的函数则可根据需要选择性复写,因为SimpleOnGestureListener类本身已经实现了这两个接口的所有函数,只是里面全是空的而已

3. 使用步骤

// 步骤1:创建手势检测器实例

// 构造函数有3个,此处用的是第三个

// 1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);

// 2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);

// 3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);

GestureDetector mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

// OnGestureListener接口的函数

// 1. 用户轻触触摸屏

public boolean onDown(MotionEvent e) {

Log.i("MyGesture1", "onDown");

return false;

}

// 2. 用户轻触触摸屏,尚未松开或拖动

// 与onDown()的区别:无松开 / 拖动

// 即:当用户点击的时,onDown()就会执行,在按下的瞬间没有松开 / 拖动时onShowPress就会执行

public void onShowPress(MotionEvent e) {

Log.i("MyGesture", "onShowPress");

}

// 3. 用户长按触摸屏

public void onLongPress(MotionEvent e) {

Log.i("MyGesture", "onLongPress");

}

// 4. 用户轻击屏幕后抬起

public boolean onSingleTapUp(MotionEvent e) {

Log.i("MyGesture", "onSingleTapUp");

return true;

}

// 5. 用户按下触摸屏 & 拖动

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY) {

Log.i("MyGesture", "onScroll:");

return true;

}

// 6. 用户按下触摸屏、快速移动后松开

// 参数:

// e1:第1个ACTION_DOWN MotionEvent

// e2:最后一个ACTION_MOVE MotionEvent

// velocityX:X轴上的移动速度,像素/秒

// velocityY:Y轴上的移动速度,像素/秒

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

Log.i("MyGesture", "onFling");

return true;

}

// OnDoubleTapListener的函数

// 1. 单击事件

// 关于OnDoubleTapListener.onSingleTapConfirmed()和 OnGestureListener.onSingleTapUp()的区别

// onSingleTapConfirmed:再次点击(即双击),则不会执行

// onSingleTapUp:手抬起就会执行

public boolean onSingleTapConfirmed(MotionEvent e) {

Log.i("MyGesture", "onSingleTapConfirmed");

return false;

}

// 2. 双击事件

public boolean onDoubleTap(MotionEvent e) {

Log.i("MyGesture", "onDoubleTap");

return false;

}

// 3. 双击间隔中发生的动作

// 指触发onDoubleTap后,在双击之间发生的其它动作,包含down、up和move事件;

public boolean onDoubleTapEvent(MotionEvent e) {

Log.i("MyGesture", "onDoubleTapEvent");

return false;

}

});

// 步骤2-1:让某个View检测手势 - 重写View的onTouch函数,将View的触屏事件交给GestureDetector处理,从而对用户手势作出响应

View.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

mGestureDetector.onTouchEvent(event);

return true; // 注:返回true才能完整接收触摸事件

}

});

// 步骤2-2:让某个Activity检测手势:重写Activity的dispatchTouchEvent函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应

@Override

public boolean dispatchTouchEvent(MotionEvent ev) {

mGestureDetector.onTouchEvent(ev); // 让GestureDetector响应触碰事件

super.dispatchTouchEvent(ev); // 让Activity响应触碰事件

return false;

}

4. 实例说明

现在对一个TextView进行手势检测

activity_main.xml

android:orientation="vertical"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

>

android:id="@+id/textView"

android:layout_width="match_parent"

android:layout_height="600dp"

android:text="carson_ho Test"

android:textAppearance="?android:attr/textAppearanceLarge" />

MainActivity.java

public class MainActivity extends AppCompatActivity {

TextView mTextView;

GestureDetector mGestureDetector;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// 步骤1:创建手势检测器实例 & 传入OnGestureListener接口(需要复写对应方法)

mGestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {

// OnGestureListener接口的函数

// 1. 用户轻触触摸屏

public boolean onDown(MotionEvent e) {

Log.i("MyGesture1", "onDown");

return false;

}

// 2. 用户轻触触摸屏,尚未松开或拖动

// 与onDown()的区别:无松开 / 拖动

// 即:当用户点击的时,onDown()就会执行,在按下的瞬间没有松开 / 拖动时onShowPress就会执行

public void onShowPress(MotionEvent e) {

Log.i("MyGesture", "onShowPress");

}

// 3. 用户长按触摸屏

public void onLongPress(MotionEvent e) {

Log.i("MyGesture", "onLongPress");

}

// 4. 用户轻击屏幕后抬起

public boolean onSingleTapUp(MotionEvent e) {

Log.i("MyGesture", "onSingleTapUp");

return true;

}

// 5. 用户按下触摸屏 & 拖动

public boolean onScroll(MotionEvent e1, MotionEvent e2,

float distanceX, float distanceY) {

Log.i("MyGesture", "onScroll:");

return true;

}

// 6. 用户按下触摸屏、快速移动后松开

// 参数:

// e1:第1个ACTION_DOWN MotionEvent

// e2:最后一个ACTION_MOVE MotionEvent

// velocityX:X轴上的移动速度,像素/秒

// velocityY:Y轴上的移动速度,像素/秒

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,

float velocityY) {

Log.i("MyGesture", "onFling");

return true;

}

// OnDoubleTapListener的函数

// 1. 单击事件

// 关于OnDoubleTapListener.onSingleTapConfirmed()和 OnGestureListener.onSingleTapUp()的区别

// onSingleTapConfirmed:再次点击(即双击),则不会执行

// onSingleTapUp:手抬起就会执行

public boolean onSingleTapConfirmed(MotionEvent e) {

Log.i("MyGesture", "onSingleTapConfirmed");

return false;

}

// 2. 双击事件

public boolean onDoubleTap(MotionEvent e) {

Log.i("MyGesture", "onDoubleTap");

return false;

}

// 3. 双击间隔中发生的动作

// 指触发onDoubleTap后,在双击之间发生的其它动作,包含down、up和move事件;

public boolean onDoubleTapEvent(MotionEvent e) {

Log.i("MyGesture", "onDoubleTapEvent");

return false;

}

});

// 步骤2:重写View的onTouch函数,将触屏事件交给GestureDetector处理,从而对用户手势作出响应

mTextView = (TextView) findViewById(R.id.textView);

mTextView.setOnTouchListener(new View.OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

mGestureDetector.onTouchEvent(event);

return true;

}

});

}

}

5. 测试效果

日志效果如下

2cb7ec3d3d5a

至此,关于Android手势识别类GestureDetector类使用讲解完毕。

总结

本文主要对Android手势识别类:GestureDetector类的使用进行全面讲解

接下来我将继续介绍 Android开发中的相关知识,感兴趣的同学可以继续关注本人博客Carson_Ho的开发笔记

请点赞!因为你的鼓励是我写作的最大动力!

不定期分享关于安卓开发的干货,追求短、平、快,但却不缺深度。

2cb7ec3d3d5a

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值