现在的位置: 首页 > 综合 > 正文

android学习——GestureDetector.OnGestureListener 详解

2013年07月09日 ⁄ 综合 ⁄ 共 4276字 ⁄ 字号 评论关闭

原文来自: http://blog.csdn.net/xiezhenxiang/article/details/6659506

感谢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();
      
  10.         return false;
      
  11.     }   
  12. }  
[java] view
plain
copy

  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();  
  10.         return false;  
  11.     }  
  12. }  

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

 

2. 回到今天的重点,当我们捕捉到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);   
  10.         tv.setOnTouchListener(this);
      
  11.         tv.setFocusable(true);
      
  12.         tv.setClickable(true);
      
  13.         tv.setLongClickable(true);
      
  14.         mGestureDetector.setIsLongpressEnabled(true);
      
  15.     }   
  16.        
  17.     /*  
  18.      * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector  
  19.      * 来分析是否有合适的callback函数来处理用户的手势  
  20.      */ 
      
  21.     public boolean onTouch(View v, MotionEvent event) {
      
  22.         return mGestureDetector.onTouchEvent(event);
      
  23.     }   
  24.   
  25.     // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发   
  26.     public boolean onDown(MotionEvent arg0) {
      
  27.         Log.i("MyGesture""onDown");
      
  28.         Toast.makeText(this"onDown", Toast.LENGTH_SHORT).show();
      
  29.         return true;
      
  30.     }   
  31.        
  32.     /*  
  33.      * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发  
  34.      * 注意和onDown()的区别,强调的是没有松开或者拖动的状态  
  35.      */  
  36.     public void onShowPress(MotionEvent e) {
      
  37.         Log.i("MyGesture""onShowPress");
      
  38.         Toast.makeText(this"onShowPress", Toast.LENGTH_SHORT).show();
      
  39.     }   
  40.        
  41.     // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发   
  42.     public boolean onSingleTapUp(MotionEvent e) {
      
  43.         Log.i("MyGesture""onSingleTapUp");
      
  44.         Toast.makeText(this"onSingleTapUp", Toast.LENGTH_SHORT).show();
      
  45.         return true;
      
  46.     }   
  47.        
  48.     // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发   
  49.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
      
  50.         Log.i("MyGesture""onFling");
      
  51.         Toast.makeText(this"onFling", Toast.LENGTH_LONG).show();
      
  52.         return true;
      
  53.     }   
  54.        
  55.     // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发   
  56.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
      
  57.         Log.i("MyGesture""onScroll");
      
  58.         Toast.makeText(this"onScroll", Toast.LENGTH_LONG).show();
      
  59.         return true;
      
  60.   

抱歉!评论已关闭.