ITeye博客

Android Gesture Detector

anson_xu · 2012-01-06 16:09 · android · 移动开发 · Markdown

Android Gesture Detector

* 博客分类: Android

Android中国移动velocityUPBlog

感谢android中国开发者的众多先驱,本文主要内容来自于总结,一小部分是自己的体会。主要参考文章为:

http://www.williamhua.com/2009/04/23/android-touchscreen-gesture-recogniton/

http://goro.iteye.com/blog/402163

Android Touch Screen 与传统Click Touch Screen不同,会有一些手势(Gesture),例如Fling,Scroll等等。这些Gesture会使用户体验大大提升。Android中的 Gesture识别(detector)是通过GestureDetector.OnGestureListener接口实现的。

首先,Android事件处理机制是基于Listener实现的,比如触摸屏相关的事件,就是通过onTouchListener实现;

其次,所有View的子类都可以通过setOnTouchListener()、setOnKeyListener()等方法来添加对某一类事件的Listener;

第三,Listener一般会以Interface的方式来提供,其中包含一个或多个abstract方法,我们需要实现这些方法来完成 onTouch()、onKey()等操作。这样,程序便可以在特定的事件被dispatch到该view的时候,通过callback函数给予适当的响应。

  1. Touch Screen Click举例 Java代码 收藏代码

    1. public class MyGesture extends Activity implements OnTouchListener {
    2. public void onCreate(Bundle savedInstanceState) {  
      
    3.     super.onCreate(savedInstanceState);  
      
    4.     setContentView(R.layout.main);  
      
    5.     TextView tv = (TextView) findViewById(R.id.tv);  
      
    6.     tv.setOnTouchListener(this);  
      
    7. }  
      
    8. public boolean onTouch(View v, MotionEvent event) {  
      
    9.     Toast.makeText(this, "Touch Touch", Toast.LENGTH_SHORT).show();  
      
  2.     return false;  
    
  3. }  
    
  4. }

public class MyGesture extends Activity implements OnTouchListener { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); tv.setOnTouchListener(this); } public boolean onTouch(View v, MotionEvent event) { Toast.makeText(this, "Touch Touch", Toast.LENGTH_SHORT).show(); return false; } }

我们可以通过MotionEvent的getAction()方法来获取Touch事件的类型,包括 ACTION_DOWN(按下触摸屏), ACTION_MOVE(按下触摸屏后移动受力点), ACTION_UP(松开触摸屏)和ACTION_CANCEL(不会由用户直接触发)。借助对于用户不同操作的判断,结合getRawX()、 getRawY()、getX()和getY()等方法来获取坐标后,我们可以实现诸如拖动某一个按钮,拖动滚动条等功能。

  1. 回到今天的重点,当我们捕捉到Touch操作的时候,如何识别出用户的Gesture?这里我们需要GestureDetector.OnGestureListener接口的帮助,代码如下: Java代码 收藏代码

    1. public class MyGesture extends Activity implements OnTouchListener, OnGestureListener {
    2. private GestureDetector mGestureDetector;  
      
    3. public MyGesture() {  
      
    4.     mGestureDetector = new GestureDetector(this);  
      
    5. }  
      
    6. public void onCreate(Bundle savedInstanceState) {  
      
    7.     super.onCreate(savedInstanceState);  
      
    8.     setContentView(R.layout.main);  
      
    9.     TextView tv = (TextView) findViewById(R.id.tv);  
      
  2.     tv.setOnTouchListener(this);  
    
  3.     tv.setFocusable(true);  
    
  4.     tv.setClickable(true);  
    
  5.     tv.setLongClickable(true);  
    
  6.     mGestureDetector.setIsLongpressEnabled(true);  
    
  7. }  
    
  8. /* 
    
  9.  * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector 
    
  10.  * 来分析是否有合适的callback函数来处理用户的手势 
    
  11.  */   
    
  12. public boolean onTouch(View v, MotionEvent event) {  
    
  13.     return mGestureDetector.onTouchEvent(event);  
    
  14. }  
    
  15. // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发  
    
  16. public boolean onDown(MotionEvent arg0) {  
    
  17.     Log.i("MyGesture", "onDown");  
    
  18.     Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();  
    
  19.     return true;  
    
  20. }  
    
  21. /* 
    
  22.  * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发 
    
  23.  * 注意和onDown()的区别,强调的是没有松开或者拖动的状态 
    
  24.  */  
    
  25. public void onShowPress(MotionEvent e) {  
    
  26.     Log.i("MyGesture", "onShowPress");  
    
  27.     Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();  
    
  28. }  
    
  29. // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发  
    
  30. public boolean onSingleTapUp(MotionEvent e) {  
    
  31.     Log.i("MyGesture", "onSingleTapUp");  
    
  32.     Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();  
    
  33.     return true;  
    
  34. }  
    
  35. // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发  
    
  36. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
    
  37.     Log.i("MyGesture", "onFling");  
    
  38.     Toast.makeText(this, "onFling", Toast.LENGTH_LONG).show();  
    
  39.     return true;  
    
  40. }  
    
  41. // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发  
    
  42. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
    
  43.     Log.i("MyGesture", "onScroll");  
    
  44.     Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();  
    
  45.     return true;  
    
  46. }  
    
  47. // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发  
    
  48. public void onLongPress(MotionEvent e) {  
    
  49.     Log.i("MyGesture", "onLongPress");  
    
  50.     Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();  
    
  51. }  
    
  52. }

public class MyGesture extends Activity implements OnTouchListener, OnGestureListener { private GestureDetector mGestureDetector; public MyGesture() { mGestureDetector = new GestureDetector(this); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); mGestureDetector.setIsLongpressEnabled(true); }

/*
 * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector
 * 来分析是否有合适的callback函数来处理用户的手势
 */ 
public boolean onTouch(View v, MotionEvent event) {
	return mGestureDetector.onTouchEvent(event);
}

// 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
public boolean onDown(MotionEvent arg0) {
	Log.i("MyGesture", "onDown");
	Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
	return true;
}

/*
 * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发
 * 注意和onDown()的区别,强调的是没有松开或者拖动的状态
 */
public void onShowPress(MotionEvent e) {
	Log.i("MyGesture", "onShowPress");
	Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
}

// 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
public boolean onSingleTapUp(MotionEvent e) {
	Log.i("MyGesture", "onSingleTapUp");
	Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
	return true;
}

// 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	Log.i("MyGesture", "onFling");
	Toast.makeText(this, "onFling", Toast.LENGTH_LONG).show();
	return true;
}

// 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
	Log.i("MyGesture", "onScroll");
	Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();
	return true;
}

// 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
public void onLongPress(MotionEvent e) {
	Log.i("MyGesture", "onLongPress");
	Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();
}

}

  1. Fling事件的处理代码:除了第一个触发Fling的ACTION_DOWN和最后一个ACTION_MOVE中包含的坐标等信息外,我们还可以根据用户在X轴或者Y轴上的移动速度作为条件。比如下面的代码中我们就在用户移动超过100个像素,且X轴上每秒的移动速度大于200像素时才进行处理。 Java代码 收藏代码

    1. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    2. // 参数解释:  
      
    3. // e1:第1个ACTION_DOWN MotionEvent  
      
    4. // e2:最后一个ACTION_MOVE MotionEvent  
      
    5. // velocityX:X轴上的移动速度,像素/秒  
      
    6. // velocityY:Y轴上的移动速度,像素/秒  
      
    7. // 触发条件 :  
      
    8. // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒  
      
  2. final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;  
    
  3. if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
    
  4.     // Fling left  
    
  5.     Log.i("MyGesture", "Fling left");  
    
  6.     Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();  
    
  7. } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
    
  8.     // Fling right  
    
  9.     Log.i("MyGesture", "Fling right");  
    
  10.     Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();  
    
  11. }  
    
  12. return false;  
    
  13. }

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 参数解释: // e1:第1个ACTION_DOWN MotionEvent // e2:最后一个ACTION_MOVE MotionEvent // velocityX:X轴上的移动速度,像素/秒 // velocityY:Y轴上的移动速度,像素/秒

// 触发条件 :
// X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒

final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
	// Fling left
	Log.i("MyGesture", "Fling left");
	Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
	// Fling right
	Log.i("MyGesture", "Fling right");
	Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();
}
return false;

}

这个例子中,tv.setLongClickable( true )是必须的,因为 只有这样,view才能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),我们同样可以通过layout定义中的android:longClickable来做到这一点。

  1. SimpleOnGestureListener Java代码 收藏代码

    1. public class MyGesture extends Activity implements OnTouchListener {
    2. private GestureDetector mGestureDetector;  
      
    3. public MyGesture() {  
      
    4.     mGestureDetector = new GestureDetector(new MySimpleGesture());  
      
    5. }  
      
    6. public void onCreate(Bundle savedInstanceState) {  
      
    7.     super.onCreate(savedInstanceState);  
      
    8.     setContentView(R.layout.main);  
      
    9.     TextView tv = (TextView) findViewById(R.id.tv);  
      
  2.     tv.setOnTouchListener(this);  
    
  3.     tv.setFocusable(true);  
    
  4.     tv.setClickable(true);  
    
  5.     tv.setLongClickable(true);  
    
  6. }  
    
  7. public boolean onTouch(View v, MotionEvent event) {  
    
  8.     if (event.getAction() == MotionEvent.ACTION_UP) {  
    
  9.         Log.i("MyGesture", "MotionEvent.ACTION_UP");  
    
  10.     }  
    
  11.     return mGestureDetector.onTouchEvent(event);  
    
  12. }  
    
  13. // SimpleOnGestureListener implements GestureDetector.OnDoubleTapListener, GestureDetector.OnGestureListener  
    
  14. private class MySimpleGesture extends SimpleOnGestureListener {  
    
  15.     // 双击的第二下Touch down时触发   
    
  16.     public boolean onDoubleTap(MotionEvent e) {  
    
  17.         Log.i("MyGesture", "onDoubleTap");  
    
  18.         return super.onDoubleTap(e);  
    
  19.     }  
    
  20.     // 双击的第二下Touch down和up都会触发,可用e.getAction()区分  
    
  21.     public boolean onDoubleTapEvent(MotionEvent e) {  
    
  22.         Log.i("MyGesture", "onDoubleTapEvent");  
    
  23.         return super.onDoubleTapEvent(e);  
    
  24.     }  
    
  25.     // Touch down时触发   
    
  26.     public boolean onDown(MotionEvent e) {  
    
  27.         Log.i("MyGesture", "onDown");  
    
  28.         return super.onDown(e);  
    
  29.     }  
    
  30.     // Touch了滑动一点距离后,up时触发  
    
  31.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
    
  32.         Log.i("MyGesture", "onFling");  
    
  33.         return super.onFling(e1, e2, velocityX, velocityY);  
    
  34.     }  
    
  35.     // Touch了不移动一直Touch down时触发  
    
  36.     public void onLongPress(MotionEvent e) {  
    
  37.         Log.i("MyGesture", "onLongPress");  
    
  38.         super.onLongPress(e);  
    
  39.     }  
    
  40.     // Touch了滑动时触发  
    
  41.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
    
  42.         Log.i("MyGesture", "onScroll");  
    
  43.         return super.onScroll(e1, e2, distanceX, distanceY);  
    
  44.     }  
    
  45.     /* 
    
  46.      * Touch了还没有滑动时触发 
    
  47.      * (1)onDown只要Touch Down一定立刻触发 
    
  48.      * (2)Touch Down后过一会没有滑动先触发onShowPress再触发onLongPress 
    
  49.      * So: Touch Down后一直不滑动,onDown -> onShowPress -> onLongPress这个顺序触发。 
    
  50.      */  
    
  51.     public void onShowPress(MotionEvent e) {  
    
  52.         Log.i("MyGesture", "onShowPress");  
    
  53.         super.onShowPress(e);  
    
  54.     }  
    
  55.     /* 
    
  56.      * 两个函数都是在Touch Down后又没有滑动(onScroll),又没有长按(onLongPress),然后Touch Up时触发 
    
  57.      * 点击一下非常快的(不滑动)Touch Up: onDown->onSingleTapUp->onSingleTapConfirmed 
    
  58.      * 点击一下稍微慢点的(不滑动)Touch Up: onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed  
    
  59.      */   
    
  60.     public boolean onSingleTapConfirmed(MotionEvent e) {  
    
  61.         Log.i("MyGesture", "onSingleTapConfirmed");  
    
  62.         return super.onSingleTapConfirmed(e);  
    
  63.     }  
    
  64.     public boolean onSingleTapUp(MotionEvent e) {  
    
  65.         Log.i("MyGesture", "onSingleTapUp");  
    
  66.         return super.onSingleTapUp(e);  
    
  67.     }  
    
  68. }  
    
  69. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。