Android GestureDetector手势识别类

为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:

boolean onDoubleTap(MotionEvent e)
解释:双击的第二下Touch down时触发
boolean onDoubleTapEvent(MotionEvent e)
解释:双击的第二下Touch down和up都会触发,可用e.getAction()区分。
boolean onDown(MotionEvent e)
解释:Touch down时触发
boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
解释:Touch了滑动一点距离后,up时触发。
void onLongPress(MotionEvent e)
解释:Touch了不移动一直Touch down时触发
boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
解释:Touch了滑动时触发。
void onShowPress(MotionEvent e)
解释:Touch了还没有滑动时触发
(与onDown,onLongPress比较
onDown只要Touch down一定立刻触发。
而Touchdown后过一会没有滑动先触发onShowPress再是onLongPress。
所以Touchdown后一直不滑动,onDown->onShowPress->onLongPress这个顺序触发。


boolean onSingleTapConfirmed(MotionEvent e)
boolean onSingleTapUp(MotionEvent e)
解释:上面这两个函数都是在touch down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touchup时触发。

点击一下非常快的(不滑动)Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
点击一下稍微慢点的(不滑动)Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed

使用GestureDetector需要在View中重写onTouchEvent事件,例如:

[java] view plain copy print ?
  1. GestureDetector mGesture = null;
  2. @Override
  3. public boolean onTouch(View v, MotionEvent event)
  4. {
  5. // TODO Auto-generated method stub
  6. return mGesture.onTouchEvent(event);
  7. }
GestureDetector mGesture = null; @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return mGesture.onTouchEvent(event); }

详细的测试例子如下:

[cpp] view plain copy print ?
  1. package com.jiubang.android.gesturetest;
  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.view.GestureDetector.SimpleOnGestureListener;
  9. import android.view.View.OnTouchListener;
  10. import android.widget.Button;
  11. public class GestureActivity extends Activity
  12. implements OnTouchListener
  13. {
  14. private Button mButton = null;
  15. GestureDetector mGesture = null;
  16. /** Called when the activity is first created. */
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.main);
  21. Log.i("TEST", "onCreate");
  22. mButton = (Button)findViewById(R.id.button1);
  23. mButton.setOnTouchListener(this);
  24. mGesture = new GestureDetector(this, new GestureListener());
  25. }
  26. @Override
  27. public boolean onTouch(View v, MotionEvent event)
  28. {
  29. // TODO Auto-generated method stub
  30. return mGesture.onTouchEvent(event);
  31. }
  32. class GestureListener extends SimpleOnGestureListener
  33. {
  34. @Override
  35. public boolean onDoubleTap(MotionEvent e)
  36. {
  37. // TODO Auto-generated method stub
  38. Log.i("TEST", "onDoubleTap");
  39. return super.onDoubleTap(e);
  40. }
  41. @Override
  42. public boolean onDown(MotionEvent e)
  43. {
  44. // TODO Auto-generated method stub
  45. Log.i("TEST", "onDown");
  46. return super.onDown(e);
  47. }
  48. @Override
  49. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
  50. float velocityY)
  51. {
  52. // TODO Auto-generated method stub
  53. Log.i("TEST", "onFling:velocityX = " + velocityX + " velocityY" + velocityY);
  54. return super.onFling(e1, e2, velocityX, velocityY);
  55. }
  56. @Override
  57. public void onLongPress(MotionEvent e)
  58. {
  59. // TODO Auto-generated method stub
  60. Log.i("TEST", "onLongPress");
  61. super.onLongPress(e);
  62. }
  63. @Override
  64. public boolean onScroll(MotionEvent e1, MotionEvent e2,
  65. float distanceX, float distanceY)
  66. {
  67. // TODO Auto-generated method stub
  68. Log.i("TEST", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);
  69. return super.onScroll(e1, e2, distanceX, distanceY);
  70. }
  71. @Override
  72. public boolean onSingleTapUp(MotionEvent e)
  73. {
  74. // TODO Auto-generated method stub
  75. Log.i("TEST", "onSingleTapUp");
  76. return super.onSingleTapUp(e);
  77. }
  78. }
  79. }

你可能感兴趣的:(Android GestureDetector手势识别类)