首页 > 其他分享 >Android GestureDetector手势识别类

Android GestureDetector手势识别类

时间:2022-12-09 21:04:50浏览次数:37  
标签:MotionEvent GestureDetector TODO Auto Override import Android 手势 android

为了加强鼠标响应事件,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);
}

}
}

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


为了加强鼠标响应事件,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);
}

}
}

标签:MotionEvent,GestureDetector,TODO,Auto,Override,import,Android,手势,android
From: https://blog.51cto.com/u_15907753/5926517

相关文章

  • Android中的横竖屏、资源、国际化的使用
    Android中的资源与国际化的问题,通常我们新建一个Android工程,目录结构如下图所示: 我们主要看一下layout与values目录,layout里的xml文件的我们应用使用布局的文件,values里......
  • 从头学Android之Android的数据存储--SQLite
    应用场景:在前面几讲我们讲了Android的数据存储使用文件或SharedPreferences存储数据,除此之外呢,有时候我们需要用到一个小型的数据库用于来保存我们的一些持久型的数据。所以......
  • Android Menu 回调
    先看错错误,使用openOptionsMenu()的时候onMenuVisibilityChanged没有回调,到真机跑了跑确是有回调的取了下小米的堆栈信息这里先贴下调用流程:Activity.openOpti......
  • 最强 Android Studio 使用小技巧和快捷键总结
    写在前面本文翻译自 AndroidStudioTipsbyPhilippeBreault,一共收集了62个AndroidStudio使用小技巧和快捷键。根据这些小技巧的使用场景,本文将这62个小技巧分为常用......
  • 解决下载Android源码时遇到的 download error
    我下载和编译环境ubuntu12.0464位lenovothinkpadi32G最近要做android的浏览器插件要用到android的源码参与编译(不是指framework层源码)按照google的官网设......
  • Android UI布局 —— 仿QQ登录界面(转载)
    ​最近,有点空闲的时间就拿QQ登录界面来模仿练手,做了个简单的登录界面。界面一般般吧,不算很漂亮,现在拿出来分享,希望大家一起学习与进步。有什么不足之处,请各位大侠多多......
  • 让Android设备永不锁屏(转载)
    锁定屏幕对于移动终端来说是非常有必要的,但是对于机顶盒产品就没有这个必要了。所以本文介绍一下怎样让Android设备永不锁屏。    Android系统的锁屏时间存放在Settin......
  • mac环境下Android 反编译
    一、需要的工具apktool:反编译APK文件,得到classes.dex文件,同时也能获取到资源文件以及布局文件。dex2jar:将反编译后的classes.dex文件转化为.jar文件。jd-gui:用于将.jar文件......
  • Android 轻量级存储方案的前世今生【SharedPreferences、MMKV、Jetpack DataStore】
    背景对于Android轻量级存储方案,有大多数人都很熟悉的SharedPreferences;也有基于mmap的高性能组件MMKV,底层序列化/反序列化使用protobuf实现,性能高,稳定性强;还有Jetp......
  • 基于Android的校园二手闲置物品交易系统设计与实现
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......