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事件,例如:

  1. GestureDetectormGesture=null;
  2. @Override
  3. publicbooleanonTouch(Viewv,MotionEventevent)
  4. {
  5. //TODOAuto-generatedmethodstub
  6. returnmGesture.onTouchEvent(event);
  7. }

详细的测试例子如下:

  1. packagecom.jiubang.android.gesturetest;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.util.Log;
  5. importandroid.view.GestureDetector;
  6. importandroid.view.MotionEvent;
  7. importandroid.view.View;
  8. importandroid.view.GestureDetector.SimpleOnGestureListener;
  9. importandroid.view.View.OnTouchListener;
  10. importandroid.widget.Button;
  11. publicclassGestureActivityextendsActivity
  12. implementsOnTouchListener
  13. {
  14. privateButtonmButton=null;
  15. GestureDetectormGesture=null;
  16. /**Calledwhentheactivityisfirstcreated.*/
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  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=newGestureDetector(this,newGestureListener());
  25. }
  26. @Override
  27. publicbooleanonTouch(Viewv,MotionEventevent)
  28. {
  29. //TODOAuto-generatedmethodstub
  30. returnmGesture.onTouchEvent(event);
  31. }
  32. classGestureListenerextendsSimpleOnGestureListener
  33. {
  34. @Override
  35. publicbooleanonDoubleTap(MotionEvente)
  36. {
  37. //TODOAuto-generatedmethodstub
  38. Log.i("TEST","onDoubleTap");
  39. returnsuper.onDoubleTap(e);
  40. }
  41. @Override
  42. publicbooleanonDown(MotionEvente)
  43. {
  44. //TODOAuto-generatedmethodstub
  45. Log.i("TEST","onDown");
  46. returnsuper.onDown(e);
  47. }
  48. @Override
  49. publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,
  50. floatvelocityY)
  51. {
  52. //TODOAuto-generatedmethodstub
  53. Log.i("TEST","onFling:velocityX="+velocityX+"velocityY"+velocityY);
  54. returnsuper.onFling(e1,e2,velocityX,velocityY);
  55. }
  56. @Override
  57. publicvoidonLongPress(MotionEvente)
  58. {
  59. //TODOAuto-generatedmethodstub
  60. Log.i("TEST","onLongPress");
  61. super.onLongPress(e);
  62. }
  63. @Override
  64. publicbooleanonScroll(MotionEvente1,MotionEvente2,
  65. floatdistanceX,floatdistanceY)
  66. {
  67. //TODOAuto-generatedmethodstub
  68. Log.i("TEST","onScroll:distanceX="+distanceX+"distanceY="+distanceY);
  69. returnsuper.onScroll(e1,e2,distanceX,distanceY);
  70. }
  71. @Override
  72. publicbooleanonSingleTapUp(MotionEvente)
  73. {
  74. //TODOAuto-generatedmethodstub
  75. Log.i("TEST","onSingleTapUp");
  76. returnsuper.onSingleTapUp(e);
  77. }
  78. }
  79. }

//以上部分内容选自网络

为了加强鼠标响应事件,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事件,例如:

  1. GestureDetectormGesture=null;
  2. @Override
  3. publicbooleanonTouch(Viewv,MotionEventevent)
  4. {
  5. //TODOAuto-generatedmethodstub
  6. returnmGesture.onTouchEvent(event);
  7. }

详细的测试例子如下:

  1. packagecom.jiubang.android.gesturetest;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.util.Log;
  5. importandroid.view.GestureDetector;
  6. importandroid.view.MotionEvent;
  7. importandroid.view.View;
  8. importandroid.view.GestureDetector.SimpleOnGestureListener;
  9. importandroid.view.View.OnTouchListener;
  10. importandroid.widget.Button;
  11. publicclassGestureActivityextendsActivity
  12. implementsOnTouchListener
  13. {
  14. privateButtonmButton=null;
  15. GestureDetectormGesture=null;
  16. /**Calledwhentheactivityisfirstcreated.*/
  17. @Override
  18. publicvoidonCreate(BundlesavedInstanceState){
  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=newGestureDetector(this,newGestureListener());
  25. }
  26. @Override
  27. publicbooleanonTouch(Viewv,MotionEventevent)
  28. {
  29. //TODOAuto-generatedmethodstub
  30. returnmGesture.onTouchEvent(event);
  31. }
  32. classGestureListenerextendsSimpleOnGestureListener
  33. {
  34. @Override
  35. publicbooleanonDoubleTap(MotionEvente)
  36. {
  37. //TODOAuto-generatedmethodstub
  38. Log.i("TEST","onDoubleTap");
  39. returnsuper.onDoubleTap(e);
  40. }
  41. @Override
  42. publicbooleanonDown(MotionEvente)
  43. {
  44. //TODOAuto-generatedmethodstub
  45. Log.i("TEST","onDown");
  46. returnsuper.onDown(e);
  47. }
  48. @Override
  49. publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,
  50. floatvelocityY)
  51. {
  52. //TODOAuto-generatedmethodstub
  53. Log.i("TEST","onFling:velocityX="+velocityX+"velocityY"+velocityY);
  54. returnsuper.onFling(e1,e2,velocityX,velocityY);
  55. }
  56. @Override
  57. publicvoidonLongPress(MotionEvente)
  58. {
  59. //TODOAuto-generatedmethodstub
  60. Log.i("TEST","onLongPress");
  61. super.onLongPress(e);
  62. }
  63. @Override
  64. publicbooleanonScroll(MotionEvente1,MotionEvente2,
  65. floatdistanceX,floatdistanceY)
  66. {
  67. //TODOAuto-generatedmethodstub
  68. Log.i("TEST","onScroll:distanceX="+distanceX+"distanceY="+distanceY);
  69. returnsuper.onScroll(e1,e2,distanceX,distanceY);
  70. }
  71. @Override
  72. publicbooleanonSingleTapUp(MotionEvente)
  73. {
  74. //TODOAuto-generatedmethodstub
  75. Log.i("TEST","onSingleTapUp");
  76. returnsuper.onSingleTapUp(e);
  77. }
  78. }
  79. }

//以上部分内容选自网络

你可能感兴趣的:(gesturedetector)