android GestureDetector 手势基础


1. 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(View v, MotionEvent event)方法,我们可以处理一些touch事件,但是这个方法太过简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦(因为我们要自己根据用户触摸的轨迹去判断是什么手势)Android sdk给我们提供了GestureDetector(Gesture:手势Detector:识别)类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。虽然他能识别手势,但是不同的手势要怎么处理,应该是提供给程序员实现的,因此这个类对外提供了两个接口:OnGestureListener,OnDoubleTapListener,还有一个内部类SimpleOnGestureListener,SimpleOnGestureListener类是GestureDetector提供给我们的一个更方便的响应不同手势的类,这个类实现了上述两个接口(但是所有的方法体都是空的),该类是static class,也就是说它实际上是一个外部类。程序员可以在外部继承这个类,重写里面的手势处理方法。
通过GestureDetector的构造方法可以将SimpleOnGestureListener对象传递进去,这样GestureDetector能处理不同的手势了。

2. 具体用法:

2.1

DefaultGestureListener.java

  1. private class DefaultGestureListener extends SimpleOnGestureListener{
  2. @Override
  3. public boolean onSingleTapUp(MotionEvent e) {
  4. return false;
  5. }
  6. @Override
  7. public void onLongPress(MotionEvent e) {
  8. }
  9. /**
  10. * @param e1 The first down motion event that started the scrolling.
  11. @param e2 The move motion event that triggered the current onScroll.
  12. @param distanceX The distance along the X axis(轴) that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
  13. @param distanceY The distance along the Y axis that has been scrolled since the last call to onScroll. This is NOT the distance between e1 and e2.
  14. 无论是用手拖动view,或者是以抛的动作滚动,都会多次触发 ,这个方法在ACTION_MOVE动作发生时就会触发 参看GestureDetector的onTouchEvent方法源码
  15. * */
  16. @Override
  17. public boolean onScroll(MotionEvent e1, MotionEvent e2,
  18. float distanceX, float distanceY) {
  19. return false;
  20. }
  21. /**
  22. * @param e1 第1个ACTION_DOWN MotionEvent 并且只有一个
  23. * @param e2 最后一个ACTION_MOVE MotionEvent
  24. * @param velocityX X轴上的移动速度,像素/秒
  25. * @param velocityY Y轴上的移动速度,像素/秒
  26. * 这个方法发生在ACTION_UP时才会触发 参看GestureDetector的onTouchEvent方法源码
  27. *
  28. * */
  29. @Override
  30. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  31. float velocityY) {
  32. return false;
  33. }
  34. @Override
  35. public void onShowPress(MotionEvent e) {
  36. }
  37. @Override
  38. public boolean onDown(MotionEvent e) {
  39. return false;
  40. }
  41. @Override
  42. public boolean onDoubleTap(MotionEvent e) {
  43. return false;
  44. }
  45. @Override
  46. public boolean onDoubleTapEvent(MotionEvent e) {
  47. return false;
  48. }
  49. /**
  50. * 这个方法不同于onSingleTapUp,他是在GestureDetector确信用户在第一次触摸屏幕后,没有紧跟着第二次触摸屏幕,也就是不是“双击”的时候触发
  51. * */
  52. @Override
  53. public boolean onSingleTapConfirmed(MotionEvent e) {
  54. return false;
  55. }
  56. }

复制代码

2.2   public GestureDetector (Context context, GestureDetector.OnGestureListener listener)     //通过构造方法将手势响应交给手势识别类2.3   在OnTouchListener的onTouch方法中

  1. private OnTouchListener gestureTouchListener = new OnTouchListener() {
  2. @Override
  3. public boolean onTouch(View v, MotionEvent event) {
  4. return gDetector.onTouchEvent(event);
  5. }
  6. };

复制代码

ok,到此为止就结束了.3.案例:Res.java

  1. package com.android;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.GestureDetector;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.widget.Button;
  9. public class Res extends Activity implements View.OnTouchListener {
  10. Button btn = null;
  11. private GestureDetector mGestureDetector = null;
  12. /** Called when the activity is first created. */
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. btn = (Button) findViewById(R.id.button);
  18. btn.setOnTouchListener(this);
  19. mGestureDetector = new GestureDetector(this, new LearnGestureListener());
  20. }
  21. public boolean onTouch(View view, MotionEvent event) {
  22. return mGestureDetector.onTouchEvent(event);
  23. }
  24. class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
  25. @Override
  26. public boolean onSingleTapUp(MotionEvent ev) {
  27. Log.d("DEBUG","onSingleTapUp");
  28. return true;
  29. }
  30. @Override
  31. public void onShowPress(MotionEvent ev) {
  32. Log.d("DEBUG","onShowPress");
  33. }
  34. @Override
  35. public void onLongPress(MotionEvent ev) {
  36. Log.d("DEBUG","onLongPress");
  37. }
  38. @Override
  39. public boolean onScroll(MotionEvent e1, MotionEvent e2,
  40. float distanceX, float distanceY) {
  41. Log.d("DEBUG","onScroll");
  42. return true;
  43. }
  44. @Override
  45. public boolean onDown(MotionEvent ev) {
  46. Log.d("DEBUG","onDownd");
  47. return true;
  48. }
  49. @Override
  50. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  51. float velocityY) {
  52. Log.d("DEBUG","onFling");
  53. return true;
  54. }
  55. public boolean onDoubleTap(MotionEvent event){
  56. Log.d("DEBUG","onDoubleTap");
  57. return true;
  58. }
  59. }
  60. }

复制代码

4.遇到的问题:
1. onFling(***)无法触发
通过设置 mListView.setLongClickable(true);即可(我处理的是ListView的手势事件),只有这样,view才能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),我们同样可以通过layout定义中的android:longClickable来做到这一点。
2. 用户长按手机屏幕,就会触发长按事件,离开屏幕时,就会触发up事件,但是SimpleOnGestureListener没有对longPress事件的up事件对外提供接口
解决办法:
类似于这样,截获up事件,因为所有的都是有OnTouchListener 先获得,然后传递给SimpleOnGestureListener的,这里有一点必须要注意:
截获到up事件,我们进行了处理后,必须要将这个事件再交给SimpleOnGestureListener处理,虽然我们只截获长按事件的up,但是SimpleOnGestureListener对于长按事件的up也做了一些处理,只是没有对外提供接口。做了什么处理: 

  1. if (mInLongPress) {
  2. mHandler.removeMessages(TAP);
  3. mInLongPress = false;
  4. }

复制代码

如果不交给SimpleOnGestureListener处理,那么单击动作也会触发onLongPress方法。

  1. private OnTouchListener gestureTouchListener = new OnTouchListener() {
  2. @Override
  3. public boolean onTouch(View v, MotionEvent event) {
  4. switch (event.getAction()) {
  5. case MotionEvent.ACTION_DOWN:
  6. return gDetector.onTouchEvent(event);
  7. case MotionEvent.ACTION_UP:
  8. MyGesture.FlagInfo info = mGesture.getFlagInfo();
  9. if(info.isConnected==true){
  10. int firstVisiblePosition = mListView.getFirstVisiblePosition();
  11. View view = mListView.getChildAt(info.position-firstVisiblePosition);
  12. if(view!=null){
  13. view.setBackgroundResource(R.drawable.listitem_background_blue);
  14. info.isConnected = false;
  15. }
  16. }
  17. return gDetector.onTouchEvent(event);
  18. case MotionEvent.ACTION_MOVE:
  19. return gDetector.onTouchEvent(event);
  20. }
  21. return false;
  22. }
  23. };

复制代码

总结:1. 点击屏幕上的某项的执行流程 有两种情况,一种是时间很短,一种时间稍长
时间很短:onDown——–》onSingleTapUp——–》onSingleTapConfirmed
时间稍长:onDown——–》onShowPress——》onSingleTapUp——–》onSingleTapConfirmed
2. 长按事件
onDown——–》onShowPress——》onLongPress
3.抛:手指触动屏幕后,稍微滑动后立即松开
onDown—–》onScroll—-》onScroll—-》onScroll—-》………—–>onFling
4.拖动
onDown——》onScroll—-》onScroll——》onFiling
注意:有的时候会触发onFiling,但是有的时候不会触发,个人理解是人的动作不标准所致。

时间: 2024-08-24 20:32:20

android GestureDetector 手势基础的相关文章

Android 触摸手势基础 官方文档概览2

Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: MotionEvent 兼容版的: MotionEventCompat  (Note that MotionEventCompat is not a replacement for the MotionEvent class. Rather, it provides static utility metho

Android GestureDetector手势识别类

为了加强鼠标响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up.Show Press.Long Press.Scroll.Down.Fling),具体包括以下几种: boolean  onDoubleTap(MotionEvent e) 解释:双击的第二下Touch down时触发 boolean  onDoubleTapEvent(MotionEve

Android 触摸手势基础 官方文档概览

触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: MotionEvent 兼容版的: MotionEventCompat  (Note that MotionEventCompat is not a replacement for the MotionEvent class. Rather, it provides static utility methods to which you pass y

android GestureDetector 手势的判断

import android.app.Activity;import android.os.Bundle;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.O

Android的手势操作识别

摘要 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchListener监听,在其onTouch()方法里获得该MotionEvent对象. 3. 通过GestureDetector(手势识别器)转发次MotionEvent对象 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchLi

Android的手势操作识别-转

Android的手势操作识别 时间:2012-10-20 23:09 来源:未知 作者:admin 点击: 282 次 摘要 首先,在Android系统中,每一次手势交互都会依照以下顺序执行. 1. 接触接触屏一刹那,触发一个MotionEvent事件. 2. 该事件被OnTouchListener监听,在其onTouch()方法里获得该MotionEvent对象. 3. 通过GestureDetector(手势识别器)转发次MotionEvent对象 首先,在Android系统中,每一次手势交

Android GestureDetector方法详解

为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up.Show Press.Long Press.Scroll.Down.Fling),具体包括以下几种: Java代码   boolean  onDoubleTap(MotionEvent e) 解释:双击的第二下down时触发 Java代码   boolean  onDoubleTap

android 上下左右手势判断 根据别人的改的

GestureUtils.java package com.gesture; import android.content.Context;import android.util.DisplayMetrics;import android.view.WindowManager; public class GestureUtils { //获取屏幕的大小 public static Screen getScreenPix(Context context) { DisplayMetrics dm =

Qt for android触摸手势事件QGestureEvent

在触摸设备上可以使用Qt的手势事件 要激活手势事件,需要执行以下操作: 第一步,为QWidget控件注册手势事件 QList<Qt::GestureType> gestures; gestures << Qt::PanGesture; gestures << Qt::PinchGesture; gestures << Qt::SwipeGesture; gestures << Qt::TapGesture; gestures << Q