Android sdk给我们提供了GestureDetector类,通过这个类我们可以识别很多的手势,主要是通过他的onTouchEvent(event)方法完成了不同手势的识别。
GestureDetector提供了两个接口:OnGestrueListener、OnDoubleTapListener,从接口的名称来看,前者是手势监听器,后者是双击手势监听器。
OnGestureListener接口含有6个回调方法:
1、onDown(MotionEvent e)
2、onSingleTapUp(MotionEvent e)
3、onShowPress(MotionEvent e)
4、onLongPress(MotionEvent e)
5、onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
6、onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
OnDoubleTapListener接口含有3个回调方法:
1、onDoubleTap(MotionEvent e)
2、onDoubleTapEvent(MotionEvent e)
3、onSingleTapConfirmed(MotionEvent e)
为使手势编程更加简洁,Android SDK提供了SimpleOnGestureListener这个内部类,它继承了OnGestureListener、OnDoubleTapListener两个接口,并实现了所有的回调方法,只不过方法体为空,需要程序员在应用程序中重写需要的方法。
以下用一个程序实例来演示SimpleOnGestureListener的用法,以及各回调方法的效果。实例程序界面放置了TextView用来显示屏幕触摸事件的坐标、位移、速度等信息。
- 界面XML文件代码(activity_gesture_test.xml)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/container" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" > 7 8 <LinearLayout 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 android:orientation="horizontal" > 12 13 <LinearLayout 14 android:layout_width="0dp" 15 android:layout_height="wrap_content" 16 android:layout_weight="1" 17 android:background="#ccc" 18 android:orientation="vertical" > 19 20 <TextView 21 android:id="@+id/showX" 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:text="X:" /> 25 26 <TextView 27 android:id="@+id/showY" 28 android:layout_width="fill_parent" 29 android:layout_height="wrap_content" 30 android:text="Y:" /> 31 </LinearLayout> 32 33 <LinearLayout 34 android:layout_width="0dp" 35 android:layout_height="wrap_content" 36 android:layout_weight="1" 37 android:background="#ddd" 38 android:orientation="vertical" > 39 40 <TextView 41 android:id="@+id/showXV" 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:text="XV:" /> 45 46 <TextView 47 android:id="@+id/showYV" 48 android:layout_width="fill_parent" 49 android:layout_height="wrap_content" 50 android:text="YV:" /> 51 </LinearLayout> 52 </LinearLayout> 53 54 <TextView 55 android:layout_width="fill_parent" 56 android:layout_height="wrap_content" 57 android:text="Scroll Gesture" /> 58 59 <LinearLayout 60 android:layout_width="fill_parent" 61 android:layout_height="wrap_content" 62 android:orientation="horizontal" > 63 64 <LinearLayout 65 android:layout_width="0dp" 66 android:layout_height="wrap_content" 67 android:layout_weight="1" 68 android:background="#ccc" 69 android:orientation="vertical" > 70 71 <TextView 72 android:id="@+id/scrollX1" 73 android:layout_width="fill_parent" 74 android:layout_height="wrap_content" 75 android:text="e1.X:" /> 76 77 <TextView 78 android:id="@+id/scrollY1" 79 android:layout_width="fill_parent" 80 android:layout_height="wrap_content" 81 android:text="e1.Y:" /> 82 83 <TextView 84 android:id="@+id/distanceX" 85 android:layout_width="fill_parent" 86 android:layout_height="wrap_content" 87 android:text="distanceX:" /> 88 </LinearLayout> 89 90 <LinearLayout 91 android:layout_width="0dp" 92 android:layout_height="wrap_content" 93 android:layout_weight="1" 94 android:background="#ddd" 95 android:orientation="vertical" > 96 97 <TextView 98 android:id="@+id/scrollX2" 99 android:layout_width="fill_parent" 100 android:layout_height="wrap_content" 101 android:text="e2.X:" /> 102 103 <TextView 104 android:id="@+id/scrollY2" 105 android:layout_width="fill_parent" 106 android:layout_height="wrap_content" 107 android:text="e2.Y:" /> 108 109 <TextView 110 android:id="@+id/distanceY" 111 android:layout_width="fill_parent" 112 android:layout_height="wrap_content" 113 android:text="distanceY:" /> 114 </LinearLayout> 115 </LinearLayout> 116 117 </LinearLayout>
- java程序代码(GestureTest.java)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 package org.kit.test.dataio; 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.GestureDetector.SimpleOnGestureListener; 8 import android.view.MotionEvent; 9 import android.view.View; 10 import android.view.View.OnTouchListener; 11 import android.widget.LinearLayout; 12 import android.widget.TextView; 13 import android.widget.Toast; 14 15 public class GestureTest extends Activity implements OnTouchListener { 16 17 GestureDetector detector; 18 19 TextView posX, posY, velX, velY; 20 TextView scrollX1, scrollY1, scrollX2, scrollY2, _distanceX, _distanceY; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 // TODO Auto-generated method stub 25 super.onCreate(savedInstanceState); 26 setContentView(R.layout.activity_gesture_test); 27 28 detector = new GestureDetector(this, new MySimpleGestureDetector()); 29 30 LinearLayout ll = (LinearLayout) findViewById(R.id.container); 31 ll.setOnTouchListener(this); 32 ll.setClickable(true); 33 ll.setLongClickable(true); 34 35 posX = (TextView) findViewById(R.id.showX); 36 posY = (TextView) findViewById(R.id.showY); 37 38 velX = (TextView) findViewById(R.id.showXV); 39 velY = (TextView) findViewById(R.id.showYV); 40 41 scrollX1 = (TextView) findViewById(R.id.scrollX1); 42 scrollY1 = (TextView) findViewById(R.id.scrollY1); 43 scrollX2 = (TextView) findViewById(R.id.scrollX2); 44 scrollY2 = (TextView) findViewById(R.id.scrollY2); 45 _distanceX = (TextView) findViewById(R.id.distanceX); 46 _distanceY = (TextView) findViewById(R.id.distanceY); 47 48 } 49 50 @Override 51 public boolean onTouch(View v, MotionEvent event) { 52 posX.setText("X:" + event.getX()); 53 posY.setText("Y:" + event.getY()); 54 // 55 return detector.onTouchEvent(event); 56 } 57 58 // 内部类inner class 59 private class MySimpleGestureDetector extends SimpleOnGestureListener { 60 61 @Override 62 public boolean onDoubleTap(MotionEvent e) { 63 // TODO Auto-generated method stub 64 65 Toast.makeText(GestureTest.this, "double tap!", Toast.LENGTH_SHORT).show(); 66 return super.onDoubleTap(e); 67 } 68 69 @Override 70 public boolean onDown(MotionEvent event) { 71 // TODO Auto-generated method stub 72 73 posX.setText("X:" + event.getX()); 74 posY.setText("Y:" + event.getY()); 75 return false; 76 } 77 78 @Override 79 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 80 // TODO Auto-generated method stub 81 velX.setText("XV:" + velocityX); 82 velY.setText("YV:" + velocityY); 83 84 Toast.makeText( getApplicationContext(), 85 "Fling!\ne1.X=" + e1.getX() + " e1.Y=" + e1.getY() + "\ne2.X=" + e2.getX() + " e2.Y=" 86 + e2.getY(), 87 Toast.LENGTH_LONG).show(); 88 89 return false; 90 } 91 92 @Override 93 public void onLongPress(MotionEvent arg0) { 94 // TODO Auto-generated method stub 95 96 Toast.makeText(getApplicationContext(), "Long Press", Toast.LENGTH_SHORT).show(); 97 } 98 99 @Override 100 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 101 // TODO Auto-generated method stub 102 103 scrollX1.setText("e1.X:" + e1.getX()); 104 scrollY1.setText("e1.Y:" + e1.getY()); 105 scrollX2.setText("e2.X:" + e2.getX()); 106 scrollY2.setText("e2.Y:" + e2.getY()); 107 _distanceX.setText("distanceX:" + distanceX); 108 _distanceY.setText("distanceY:" + distanceY); 109 110 return false; 111 } 112 113 @Override 114 public void onShowPress(MotionEvent arg0) { 115 // TODO Auto-generated method stub 116 Toast.makeText(getApplicationContext(), "Show Press!", Toast.LENGTH_LONG).show(); 117 118 } 119 120 @Override 121 public boolean onSingleTapUp(MotionEvent e) { 122 // TODO Auto-generated method stub 123 124 Toast.makeText(getApplicationContext(), "Single Tap", Toast.LENGTH_LONG).show(); 125 return false; 126 } 127 128 @Override 129 public boolean onSingleTapConfirmed(MotionEvent e) { 130 // TODO Auto-generated method stub 131 132 Toast.makeText(getApplicationContext(), "Single Tap confirmed", Toast.LENGTH_LONG).show(); 133 134 return super.onSingleTapConfirmed(e); 135 } 136 137 @Override 138 public boolean onDoubleTapEvent(MotionEvent e) { 139 // TODO Auto-generated method stub 140 switch (e.getAction()) { 141 case MotionEvent.ACTION_DOWN: 142 Toast.makeText(getApplicationContext(), "double click down", Toast.LENGTH_SHORT).show(); 143 break; 144 case MotionEvent.ACTION_UP: 145 Toast.makeText(getApplicationContext(), "double click up", Toast.LENGTH_SHORT).show(); 146 break; 147 148 default: 149 break; 150 } 151 return super.onDoubleTapEvent(e); 152 } 153 } 154 155 }