为了加强鼠标响应事件,Android提供了GestureDetector手势识别类。通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具体包括以下几种:标签:MotionEvent,GestureDetector,TODO,Auto,Override,import,Android,手势,android From: https://blog.51cto.com/u_15907753/5926517
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
4. {
5. // TODO Auto-generated method stub
6. return
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.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.util.Log;
6. import android.view.GestureDetector;
7. import android.view.MotionEvent;
8. import android.view.View;
9. import android.view.GestureDetector.SimpleOnGestureListener;
10. import android.view.View.OnTouchListener;
11. import android.widget.Button;
12.
13. public class
14. implements OnTouchListener
15. {
16. private
17. GestureDetector mGesture = null;
18.
19. /** Called when the activity is first created. */
20. @Override
21. public void
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.main);
24.
25. "TEST", "onCreate");
26. mButton = (Button)findViewById(R.id.button1);
27. this);
28. new GestureDetector(this, new
29. }
30.
31. @Override
32. public
33. {
34. // TODO Auto-generated method stub
35. return
36. }
37.
38. class
39. {
40.
41. @Override
42. public
43. {
44. // TODO Auto-generated method stub
45. "TEST", "onDoubleTap");
46. return
47. }
48.
49. @Override
50. public
51. {
52. // TODO Auto-generated method stub
53. "TEST", "onDown");
54. return
55. }
56.
57. @Override
58. public boolean onFling(MotionEvent e1, MotionEvent e2, float
59. float
60. {
61. // TODO Auto-generated method stub
62. "TEST", "onFling:velocityX = " + velocityX + " velocityY"
63. return
64. }
65.
66. @Override
67. public void
68. {
69. // TODO Auto-generated method stub
70. "TEST", "onLongPress");
71. super.onLongPress(e);
72. }
73.
74. @Override
75. public
76. float distanceX, float
77. {
78. // TODO Auto-generated method stub
79. "TEST", "onScroll:distanceX = " + distanceX + " distanceY = "
80. return
81. }
82.
83. @Override
84. public
85. {
86. // TODO Auto-generated method stub
87. "TEST", "onSingleTapUp");
88. return
89. }
90.
91. }
92. }
package com.jiubang.android.gesturetest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class GestureActivity extends Activity
implements OnTouchListener
{
private Button mButton = null;
GestureDetector mGesture = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("TEST", "onCreate");
mButton = (Button)findViewById(R.id.button1);
mButton.setOnTouchListener(this);
mGesture = new GestureDetector(this, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
return mGesture.onTouchEvent(event);
}
class GestureListener extends SimpleOnGestureListener
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDoubleTap");
return super.onDoubleTap(e);
}
@Override
public boolean onDown(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDown");
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onFling:velocityX = " + velocityX + " velocityY" + velocityY);
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onLongPress(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onLongPress");
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onSingleTapUp");
return super.onSingleTapUp(e);
}
}
}
//以上部分内容选自网络
为了加强鼠标响应事件,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
4. {
5. // TODO Auto-generated method stub
6. return
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.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.util.Log;
6. import android.view.GestureDetector;
7. import android.view.MotionEvent;
8. import android.view.View;
9. import android.view.GestureDetector.SimpleOnGestureListener;
10. import android.view.View.OnTouchListener;
11. import android.widget.Button;
12.
13. public class
14. implements OnTouchListener
15. {
16. private
17. GestureDetector mGesture = null;
18.
19. /** Called when the activity is first created. */
20. @Override
21. public void
22. super.onCreate(savedInstanceState);
23. setContentView(R.layout.main);
24.
25. "TEST", "onCreate");
26. mButton = (Button)findViewById(R.id.button1);
27. this);
28. new GestureDetector(this, new
29. }
30.
31. @Override
32. public
33. {
34. // TODO Auto-generated method stub
35. return
36. }
37.
38. class
39. {
40.
41. @Override
42. public
43. {
44. // TODO Auto-generated method stub
45. "TEST", "onDoubleTap");
46. return
47. }
48.
49. @Override
50. public
51. {
52. // TODO Auto-generated method stub
53. "TEST", "onDown");
54. return
55. }
56.
57. @Override
58. public boolean onFling(MotionEvent e1, MotionEvent e2, float
59. float
60. {
61. // TODO Auto-generated method stub
62. "TEST", "onFling:velocityX = " + velocityX + " velocityY"
63. return
64. }
65.
66. @Override
67. public void
68. {
69. // TODO Auto-generated method stub
70. "TEST", "onLongPress");
71. super.onLongPress(e);
72. }
73.
74. @Override
75. public
76. float distanceX, float
77. {
78. // TODO Auto-generated method stub
79. "TEST", "onScroll:distanceX = " + distanceX + " distanceY = "
80. return
81. }
82.
83. @Override
84. public
85. {
86. // TODO Auto-generated method stub
87. "TEST", "onSingleTapUp");
88. return
89. }
90.
91. }
92. }
package com.jiubang.android.gesturetest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
public class GestureActivity extends Activity
implements OnTouchListener
{
private Button mButton = null;
GestureDetector mGesture = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("TEST", "onCreate");
mButton = (Button)findViewById(R.id.button1);
mButton.setOnTouchListener(this);
mGesture = new GestureDetector(this, new GestureListener());
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
// TODO Auto-generated method stub
return mGesture.onTouchEvent(event);
}
class GestureListener extends SimpleOnGestureListener
{
@Override
public boolean onDoubleTap(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDoubleTap");
return super.onDoubleTap(e);
}
@Override
public boolean onDown(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onDown");
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onFling:velocityX = " + velocityX + " velocityY" + velocityY);
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onLongPress(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onLongPress");
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY)
{
// TODO Auto-generated method stub
Log.i("TEST", "onScroll:distanceX = " + distanceX + " distanceY = " + distanceY);
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
// TODO Auto-generated method stub
Log.i("TEST", "onSingleTapUp");
return super.onSingleTapUp(e);
}
}
}