之前一直不知道这个类,在Android就以为只有鼠标的down和up事件,原来android为了增加用户体验,新增了GestureDetector类,也就是手势识别类,感觉就是将手指触摸屏幕的touch事件更加细分了,构造GestureDetector类时设置一个GestureDetector.OnGestureListener对象来实时监控用户的操作,OnGestureListener对象提供一些常见手势的重写方法,这样就能对不同的手势做不同的处理。
view plaincopy to clipboardprint?01.public class GestureActivity extends Activity implements View.OnTouchListener { 02. Button btn = null; 03. private GestureDetector mGestureDetector = null; 04. /** Called when the activity is first created. */ 05. public void onCreate(Bundle savedInstanceState) { 06. super.onCreate(savedInstanceState); 07. setContentView(R.layout.main); 08. btn = (Button) findViewById(R.id.button); 09. btn.setOnTouchListener(this); 10. mGestureDetector = new GestureDetector(this, new LearnGestureListener()); 11. } 12. public boolean onTouch(View view, MotionEvent event) { 13. return mGestureDetector.onTouchEvent(event); 14. } 15. class LearnGestureListener extends GestureDetector.SimpleOnGestureListener { 16. @Override 17. public boolean onSingleTapUp(MotionEvent ev) { 18. Log.d("DEBUG", "onSingleTapUp"); 19. return true; 20. } 21. // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发 22. // 注意和onDown()的区别,强调的是没有松开或者拖动的状态 23. @Override 24. public void onShowPress(MotionEvent ev) { 25. Log.d("DEBUG", "onShowPress"); 26. } 27. // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发 28. @Override 29. public void onLongPress(MotionEvent ev) { 30. Log.d("DEBUG", "onLongPress"); 31. } 32. // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发 33. @Override 34. public boolean onScroll(MotionEvent e1, MotionEvent e2, 35. float distanceX, float distanceY) { 36. Log.d("DEBUG", "onScroll"); 37. return true; 38. } 39. // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发 40. @Override 41. public boolean onDown(MotionEvent ev) { 42. Log.d("DEBUG", "onDownd"); 43. return true; 44. } 45. // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 46. // 多个ACTION_MOVE, 1个ACTION_UP触发 47. // e1:第1个ACTION_DOWN MotionEvent 48. // e2:最后一个ACTION_MOVE MotionEvent 49. // velocityX:X轴上的移动速度,像素/秒 50. // velocityY:Y轴上的移动速度,像素/秒 51. @Override 52. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 53. float velocityY) { 54. Log.d("DEBUG", "onFling"); 55. return true; 56. } 57. // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发 58. public boolean onDoubleTap(MotionEvent event) { 59. Log.d("DEBUG", "onDoubleTap"); 60. return true; 61. } 62. } 63.} public class GestureActivity extends Activity
implements View.OnTouchListener {标签:MotionEvent,GestureDetector,Log,DEBUG,ACTION,return,Android,public From: https://blog.51cto.com/u_15907753/5926323
Button btn = null;
private GestureDetector mGestureDetector = null;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button);
btn.setOnTouchListener(this);
mGestureDetector = new GestureDetector(this, new LearnGestureListener());
}
public boolean onTouch(View view, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onSingleTapUp(MotionEvent ev) {
Log.d("DEBUG", "onSingleTapUp");
return true;
}
// 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
// 注意和onDown()的区别,强调的是没有松开或者拖动的状态
@Override
public void onShowPress(MotionEvent ev) {
Log.d("DEBUG", "onShowPress");
}
// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
@Override
public void onLongPress(MotionEvent ev) {
Log.d("DEBUG", "onLongPress");
}
// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.d("DEBUG", "onScroll");
return true;
}
// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
@Override
public boolean onDown(MotionEvent ev) {
Log.d("DEBUG", "onDownd");
return true;
}
// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,
// 多个ACTION_MOVE, 1个ACTION_UP触发
// e1:第1个ACTION_DOWN MotionEvent
// e2:最后一个ACTION_MOVE MotionEvent
// velocityX:X轴上的移动速度,像素/秒
// velocityY:Y轴上的移动速度,像素/秒
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.d("DEBUG", "onFling");
return true;
}
// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
public boolean onDoubleTap(MotionEvent event) {
Log.d("DEBUG", "onDoubleTap");
return true;
}
}
}