Android手勢識別GestureDetector詳解


一、概述

當用戶觸摸屏幕的時候,會產生許多手勢,例如down,up,scroll,filing等等。
一般情況下,我們知道View類有個View.OnTouchListener內部接口,通過重寫他的onTouch(View v, MotionEvent event)方法,我們可以處理一些touch事件,但是這個方法太過簡單,如果需要處理一些複雜的手勢,用這個接口就會很麻煩(因爲我們要自己根據用戶觸摸的軌跡去判斷是什麼手勢)。
Android sdk給我們提供了GestureDetector(Gesture:手勢Detector:識別)類,通過這個類我們可以識別很多的手勢,主要是通過他的onTouchEvent(event)方法完成了不同手勢的識別。雖然他能識別手勢,但是不同的手勢要怎麼處理,應該是提供給程序員實現的。

GestureDetector這個類對外提供了兩個接口和一個外部類
接口:OnGestureListener,OnDoubleTapListener
內部類:SimpleOnGestureListener

這個外部類,其實是兩個接口中所有函數的集成,它包含了這兩個接口裏所有必須要實現的函數而且都已經重寫,但所有方法體都是空的;不同點在於:該類是static class,程序員可以在外部繼承這個類,重寫裏面的手勢處理方法。

下面我們先看OnGestureListener接口;

二、GestureDetector.OnGestureListener---接口

1、基本講解

如果我們寫一個類並implements OnGestureListener,會提示有幾個必須重寫的函數,加上之後是這個樣子的:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class gesturelistener implements GestureDetector.OnGestureListener{  
  2.   
  3.     public boolean onDown(MotionEvent e) {  
  4.         // TODO Auto-generated method stub  
  5.         return false;  
  6.     }  
  7.   
  8.     public void onShowPress(MotionEvent e) {  
  9.         // TODO Auto-generated method stub  
  10.           
  11.     }  
  12.   
  13.     public boolean onSingleTapUp(MotionEvent e) {  
  14.         // TODO Auto-generated method stub  
  15.         return false;  
  16.     }  
  17.   
  18.     public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  19.             float distanceX, float distanceY) {  
  20.         // TODO Auto-generated method stub  
  21.         return false;  
  22.     }  
  23.   
  24.     public void onLongPress(MotionEvent e) {  
  25.         // TODO Auto-generated method stub  
  26.           
  27.     }  
  28.   
  29.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  30.             float velocityY) {  
  31.         // TODO Auto-generated method stub  
  32.         return false;  
  33.     }  
  34.       
  35. }  
可見,這裏總共重寫了六個函數,這些函數都在什麼情況下才會觸發呢,下面講一下:

OnDown(MotionEvent e):用戶按下屏幕就會觸發;
onShowPress(MotionEvent e):如果是按下的時間超過瞬間,而且在按下的時候沒有鬆開或者是拖動的,那麼onShowPress就會執行,具體這個瞬間是多久,我也不清楚呃……
onLongPress(MotionEvent e):長按觸摸屏,超過一定時長,就會觸發這個事件
    觸發順序:
    onDown->onShowPress->onLongPress

onSingleTapUp(MotionEvent e):從名子也可以看出,一次單獨的輕擊擡起操作,也就是輕擊一下屏幕,立刻擡起來,纔會有這個觸發,當然,如果除了Down以外還有其它操作,那就不再算是Single操作了,所以也就不會觸發這個事件
    觸發順序:
    點擊一下非常快的(不滑動)Touchup:
    onDown->onSingleTapUp->onSingleTapConfirmed 
    點擊一下稍微慢點的(不滑動)Touchup:
    onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用戶按下觸摸屏、快速移動後鬆開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發   
     參數解釋:
    e1:第1個ACTION_DOWN MotionEvent
    e2:最後一個ACTION_MOVE MotionEvent
    velocityX:X軸上的移動速度,像素/秒
    velocityY:Y軸上的移動速度,像素/秒   

onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖動事件。無論是用手拖動view,或者是以拋的動作滾動,都會多次觸發,這個方法       在ACTION_MOVE動作發生時就會觸發
    滑屏:手指觸動屏幕後,稍微滑動後立即鬆開
    onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
    拖動
    onDown------》onScroll----》onScroll------》onFiling

    可見,無論是滑屏,還是拖動,影響的只是中間OnScroll觸發的數量多少而已,最終都會觸發onFling事件!

2、實例

要使用GestureDetector,有三步要走:

1、創建OnGestureListener監聽函數:
可以使用構造實例:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener(){  
  2.           
  3.     };  
也可以構造類:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class gestureListener implements GestureDetector.OnGestureListener{  
  2.   
  3. }  
2、創建GestureDetector實例mGestureDetector:

構造函數有下面三個,根據需要選擇:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);  
  2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);  
  3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);  
3、onTouch(View v, MotionEvent event)中攔截:
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public boolean onTouch(View v, MotionEvent event) {  
  2.     return mGestureDetector.onTouchEvent(event);     
  3. }  

4、控件綁定

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. TextView tv = (TextView)findViewById(R.id.tv);  
  2. tv.setOnTouchListener(this);  

現在進入實例階段:

首先,在主佈局頁面添加一個textView,並將其放大到整屏,方便在其上的手勢識別,代碼爲:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.gesturedetectorinterface.MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tv"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_margin="50dip"  
  12.         android:background="#ff00ff"  
  13.         android:text="@string/hello_world" />  
  14.   
  15. </RelativeLayout>  
然後在JAVA代碼中,依據上面的三步走原則,寫出代碼,並在所有的手勢下添加上Toast提示並寫上Log

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener{  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.           
  11.       mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  
  12.           
  13.       TextView tv = (TextView)findViewById(R.id.tv);  
  14.       tv.setOnTouchListener(this);  
  15.       tv.setFocusable(true);     
  16.       tv.setClickable(true);     
  17.       tv.setLongClickable(true);   
  18.     }  
  19.       
  20.       
  21.     /*  
  22.      * 在onTouch()方法中,我們調用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector  
  23.      * 來分析是否有合適的callback函數來處理用戶的手勢  
  24.      */    
  25.     public boolean onTouch(View v, MotionEvent event) {  
  26.         return mGestureDetector.onTouchEvent(event);     
  27.     }  
  28.       
  29.     private class gestureListener implements GestureDetector.OnGestureListener{  
  30.   
  31.         // 用戶輕觸觸摸屏,由1個MotionEvent ACTION_DOWN觸發     
  32.         public boolean onDown(MotionEvent e) {  
  33.             Log.i("MyGesture""onDown");     
  34.             Toast.makeText(MainActivity.this"onDown", Toast.LENGTH_SHORT).show();     
  35.             return false;  
  36.         }  
  37.   
  38.         /*   
  39.          * 用戶輕觸觸摸屏,尚未鬆開或拖動,由一個1個MotionEvent ACTION_DOWN觸發   
  40.          * 注意和onDown()的區別,強調的是沒有鬆開或者拖動的狀態   
  41.          *  
  42.          * 而onDown也是由一個MotionEventACTION_DOWN觸發的,但是他沒有任何限制, 
  43.          * 也就是說當用戶點擊的時候,首先MotionEventACTION_DOWN,onDown就會執行, 
  44.          * 如果在按下的瞬間沒有鬆開或者是拖動的時候onShowPress就會執行,如果是按下的時間超過瞬間 
  45.          * (這塊我也不太清楚瞬間的時間差是多少,一般情況下都會執行onShowPress),拖動了,就不執行onShowPress。 
  46.          */  
  47.         public void onShowPress(MotionEvent e) {  
  48.             Log.i("MyGesture""onShowPress");     
  49.             Toast.makeText(MainActivity.this"onShowPress", Toast.LENGTH_SHORT).show();     
  50.         }  
  51.   
  52.         // 用戶(輕觸觸摸屏後)鬆開,由一個1個MotionEvent ACTION_UP觸發     
  53.         ///輕擊一下屏幕,立刻擡起來,纔會有這個觸發  
  54.         //從名子也可以看出,一次單獨的輕擊擡起操作,當然,如果除了Down以外還有其它操作,那就不再算是Single操作了,所以這個事件 就不再響應  
  55.         public boolean onSingleTapUp(MotionEvent e) {  
  56.             Log.i("MyGesture""onSingleTapUp");     
  57.             Toast.makeText(MainActivity.this"onSingleTapUp", Toast.LENGTH_SHORT).show();     
  58.             return true;     
  59.         }  
  60.   
  61.         // 用戶按下觸摸屏,並拖動,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE觸發     
  62.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  63.                 float distanceX, float distanceY) {  
  64.             Log.i("MyGesture22""onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);     
  65.             Toast.makeText(MainActivity.this"onScroll", Toast.LENGTH_LONG).show();     
  66.               
  67.             return true;     
  68.         }  
  69.   
  70.         // 用戶長按觸摸屏,由多個MotionEvent ACTION_DOWN觸發     
  71.         public void onLongPress(MotionEvent e) {  
  72.              Log.i("MyGesture""onLongPress");     
  73.              Toast.makeText(MainActivity.this"onLongPress", Toast.LENGTH_LONG).show();     
  74.         }  
  75.   
  76.         // 用戶按下觸摸屏、快速移動後鬆開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發     
  77.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  78.                 float velocityY) {  
  79.             Log.i("MyGesture""onFling");     
  80.             Toast.makeText(MainActivity.this"onFling", Toast.LENGTH_LONG).show();     
  81.             return true;  
  82.         }  
  83.     };  
  84.       
  85.   
  86. }  
源碼在博客底部給出。

三、GestureDetector.OnDoubleTapListener---接口

1、構建

有兩種方式設置雙擊監聽:

方法一:新建一個類同時派生自OnGestureListener和OnDoubleTapListener:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{  
  2.     }  
方法二:使用GestureDetector::setOnDoubleTapListener();函數設置監聽:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //構建GestureDetector實例     
  2. mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  
  3. private class gestureListener implements GestureDetector.OnGestureListener{  
  4.       
  5. }  
  6.   
  7. //設置雙擊監聽器  
  8. mGestureDetector.setOnDoubleTapListener(new doubleTapListener());  
  9. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  10.       
  11. }  
注意:大家可以看到無論在方法一還是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我們說過GestureDetector 的構造函數,如下:
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);  
  2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);  
  3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);  
可以看到,在構造函數中,除了後面要講的SimpleOnGestureListener 以外的其它兩個構造函數都必須是OnGestureListener的實例。所以要想使用OnDoubleTapListener的幾個函數,就必須先實現OnGestureListener。

2、函數講解:

首先看一下OnDoubleTapListener接口必須重寫的三個函數:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  2.   
  3.     public boolean onSingleTapConfirmed(MotionEvent e) {  
  4.         // TODO Auto-generated method stub  
  5.         return false;  
  6.     }  
  7.   
  8.     public boolean onDoubleTap(MotionEvent e) {  
  9.         // TODO Auto-generated method stub  
  10.         return false;  
  11.     }  
  12.   
  13.     public boolean onDoubleTapEvent(MotionEvent e) {  
  14.         // TODO Auto-generated method stub  
  15.         return false;  
  16.     }  
  17. }  
onSingleTapConfirmed(MotionEvent e):單擊事件。用來判定該次點擊是SingleTap而不是DoubleTap,如果連續點擊兩次就是DoubleTap手勢,如果只點擊一次,系統等待一段時間後沒有收到第二次點擊則判定該次點擊爲SingleTap而不是DoubleTap,然後觸發SingleTapConfirmed事件。觸發順序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
關於onSingleTapConfirmed和onSingleTapUp的一點區別: OnGestureListener有這樣的一個方法onSingleTapUp,和onSingleTapConfirmed容易混淆。二者的區別是:onSingleTapUp,只要手擡起就會執行,而對於onSingleTapConfirmed來說,如果雙擊的話,則onSingleTapConfirmed不會執行。

onDoubleTap(MotionEvent e):雙擊事件

onDoubleTapEvent(MotionEvent e):雙擊間隔中發生的動作。指觸發onDoubleTap以後,在雙擊之間發生的其它動作,包含down、up和move事件;下圖是雙擊一下的Log輸出:

兩點總結:

1、從上圖可以看出,在第二下點擊時,先觸發OnDoubleTap,然後再觸發OnDown(第二次點擊)

2、其次在觸發OnDoubleTap以後,就開始觸發onDoubleTapEvent了,onDoubleTapEvent後面的數字代表了當前的事件,0指ACTION_DOWN,1指ACTION_UP,2 指ACTION_MOVE
在上一個例子的基礎上,我們再添加一個雙擊監聽類,實現如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener{  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.           
  11.   
  12.       mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  
  13.       mGestureDetector.setOnDoubleTapListener(new doubleTapListener());  
  14.           
  15.       TextView tv = (TextView)findViewById(R.id.tv);  
  16.       tv.setOnTouchListener(this);  
  17.       tv.setFocusable(true);     
  18.       tv.setClickable(true);     
  19.       tv.setLongClickable(true);   
  20.     }  
  21.       
  22.       
  23.     /*  
  24.      * 在onTouch()方法中,我們調用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector  
  25.      * 來分析是否有合適的callback函數來處理用戶的手勢  
  26.      */    
  27.     public boolean onTouch(View v, MotionEvent event) {  
  28.         return mGestureDetector.onTouchEvent(event);     
  29.     }  
  30.       
  31.     //OnGestureListener監聽  
  32.     private class gestureListener implements GestureDetector.OnGestureListener{  
  33.   
  34.         public boolean onDown(MotionEvent e) {  
  35.             Log.i("MyGesture""onDown");     
  36.             Toast.makeText(MainActivity.this"onDown", Toast.LENGTH_SHORT).show();     
  37.             return false;  
  38.         }  
  39.   
  40.         public void onShowPress(MotionEvent e) {  
  41.             Log.i("MyGesture""onShowPress");     
  42.             Toast.makeText(MainActivity.this"onShowPress", Toast.LENGTH_SHORT).show();     
  43.         }  
  44.   
  45.         public boolean onSingleTapUp(MotionEvent e) {  
  46.             Log.i("MyGesture""onSingleTapUp");     
  47.             Toast.makeText(MainActivity.this"onSingleTapUp", Toast.LENGTH_SHORT).show();     
  48.             return true;     
  49.         }  
  50.   
  51.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  52.                 float distanceX, float distanceY) {  
  53.             Log.i("MyGesture22""onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);     
  54.             Toast.makeText(MainActivity.this"onScroll", Toast.LENGTH_LONG).show();     
  55.               
  56.             return true;     
  57.         }  
  58.   
  59.         public void onLongPress(MotionEvent e) {  
  60.              Log.i("MyGesture""onLongPress");     
  61.              Toast.makeText(MainActivity.this"onLongPress", Toast.LENGTH_LONG).show();     
  62.         }  
  63.   
  64.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  65.                 float velocityY) {  
  66.             Log.i("MyGesture""onFling");     
  67.             Toast.makeText(MainActivity.this"onFling", Toast.LENGTH_LONG).show();     
  68.             return true;  
  69.         }  
  70.     };  
  71.       
  72.     //OnDoubleTapListener監聽  
  73.     private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  74.   
  75.         public boolean onSingleTapConfirmed(MotionEvent e) {  
  76.             Log.i("MyGesture""onSingleTapConfirmed");     
  77.             Toast.makeText(MainActivity.this"onSingleTapConfirmed", Toast.LENGTH_LONG).show();    
  78.             return true;  
  79.         }  
  80.   
  81.         public boolean onDoubleTap(MotionEvent e) {  
  82.             Log.i("MyGesture""onDoubleTap");     
  83.             Toast.makeText(MainActivity.this"onDoubleTap", Toast.LENGTH_LONG).show();    
  84.             return true;  
  85.         }  
  86.   
  87.         public boolean onDoubleTapEvent(MotionEvent e) {  
  88.             Log.i("MyGesture""onDoubleTapEvent");     
  89.             Toast.makeText(MainActivity.this"onDoubleTapEvent", Toast.LENGTH_LONG).show();    
  90.             return true;  
  91.         }  
  92.     };  
  93. }  
雙擊一下,部分截圖如下:


雙擊所對應的觸發事件順序:


輕輕單擊一下,對應的事件觸發順序爲:


源碼在博客底部給出。

四、GestureDetector.SimpleOnGestureListener---類

它與前兩個不同的是:
1、這是一個類,在它基礎上新建類的話,要用extends派生而不是用implements繼承!
2、OnGestureListener和OnDoubleTapListener接口裏的函數都是強制必須重寫的,即使用不到也要重寫出來一個空函數但在SimpleOnGestureListener類的實例或派生類中不必如此,可以根據情況,用到哪個函數就重寫哪個函數,因爲SimpleOnGestureListener類本身已經實現了這兩個接口的所有函數,只是裏面全是空的而已。

下面利用SimpleOnGestureListener類來重新實現上面的幾個效果,代碼如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener {  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         mGestureDetector = new GestureDetector(new simpleGestureListener());  
  11.           
  12.         TextView tv = (TextView)findViewById(R.id.tv);  
  13.         tv.setOnTouchListener(this);  
  14.         tv.setFocusable(true);     
  15.         tv.setClickable(true);     
  16.         tv.setLongClickable(true);   
  17.     }  
  18.       
  19.     public boolean onTouch(View v, MotionEvent event) {  
  20.         // TODO Auto-generated method stub  
  21.         return mGestureDetector.onTouchEvent(event);     
  22.     }  
  23.   
  24.     private class simpleGestureListener extends  
  25.             GestureDetector.SimpleOnGestureListener {  
  26.           
  27.         /*****OnGestureListener的函數*****/  
  28.         public boolean onDown(MotionEvent e) {  
  29.             Log.i("MyGesture""onDown");  
  30.             Toast.makeText(MainActivity.this"onDown", Toast.LENGTH_SHORT)  
  31.                     .show();  
  32.             return false;  
  33.         }  
  34.   
  35.         public void onShowPress(MotionEvent e) {  
  36.             Log.i("MyGesture""onShowPress");  
  37.             Toast.makeText(MainActivity.this"onShowPress", Toast.LENGTH_SHORT)  
  38.                     .show();  
  39.         }  
  40.   
  41.         public boolean onSingleTapUp(MotionEvent e) {  
  42.             Log.i("MyGesture""onSingleTapUp");  
  43.             Toast.makeText(MainActivity.this"onSingleTapUp",  
  44.                     Toast.LENGTH_SHORT).show();  
  45.             return true;  
  46.         }  
  47.   
  48.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  49.                 float distanceX, float distanceY) {  
  50.             Log.i("MyGesture""onScroll:" + (e2.getX() - e1.getX()) + "   "  
  51.                     + distanceX);  
  52.             Toast.makeText(MainActivity.this"onScroll", Toast.LENGTH_LONG)  
  53.                     .show();  
  54.   
  55.             return true;  
  56.         }  
  57.   
  58.         public void onLongPress(MotionEvent e) {  
  59.             Log.i("MyGesture""onLongPress");  
  60.             Toast.makeText(MainActivity.this"onLongPress", Toast.LENGTH_LONG)  
  61.                     .show();  
  62.         }  
  63.   
  64.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  65.                 float velocityY) {  
  66.             Log.i("MyGesture""onFling");  
  67.             Toast.makeText(MainActivity.this"onFling", Toast.LENGTH_LONG)  
  68.                     .show();  
  69.             return true;  
  70.         }  
  71.           
  72.         /*****OnDoubleTapListener的函數*****/  
  73.         public boolean onSingleTapConfirmed(MotionEvent e) {  
  74.             Log.i("MyGesture""onSingleTapConfirmed");  
  75.             Toast.makeText(MainActivity.this"onSingleTapConfirmed",  
  76.                     Toast.LENGTH_LONG).show();  
  77.             return true;  
  78.         }  
  79.   
  80.         public boolean onDoubleTap(MotionEvent e) {  
  81.             Log.i("MyGesture""onDoubleTap");  
  82.             Toast.makeText(MainActivity.this"onDoubleTap", Toast.LENGTH_LONG)  
  83.                     .show();  
  84.             return true;  
  85.         }  
  86.   
  87.         public boolean onDoubleTapEvent(MotionEvent e) {  
  88.             Log.i("MyGesture""onDoubleTapEvent");  
  89.             Toast.makeText(MainActivity.this"onDoubleTapEvent",  
  90.                     Toast.LENGTH_LONG).show();  
  91.             return true;  
  92.         }  
  93.   
  94.     }  
  95. }  

到此,有關GestureDetector的所有基礎知識都講解完了,下面給出一個小應用——識別用戶是向左滑還是向右滑!

源碼在博客底部給出。

五、OnFling應用——識別向左滑還是向右滑

這部分就有點意思了,可以說是上面知識的一個小應用,我們利用OnFling函數來識別當前用戶是在向左滑還是向右滑,從而打出日誌。先看下OnFling的參數:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. 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軸上的移動速度,像素/秒     
首先,先說一下實現的功能:當用戶向左滑動距離超過100px,且滑動速度超過100 px/s時,即判斷爲向左滑動;向右同理.代碼如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener {  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         mGestureDetector = new GestureDetector(new simpleGestureListener());  
  11.           
  12.         TextView tv = (TextView)findViewById(R.id.tv);  
  13.         tv.setOnTouchListener(this);  
  14.         tv.setFocusable(true);     
  15.         tv.setClickable(true);     
  16.         tv.setLongClickable(true);   
  17.     }  
  18.       
  19.     public boolean onTouch(View v, MotionEvent event) {  
  20.         // TODO Auto-generated method stub  
  21.         return mGestureDetector.onTouchEvent(event);     
  22.     }  
  23.   
  24.     private class simpleGestureListener extends  
  25.             GestureDetector.SimpleOnGestureListener {  
  26.           
  27.         /*****OnGestureListener的函數*****/  
  28.   
  29.         final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;    
  30.           
  31.         // 觸發條件 :     
  32.         // X軸的座標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒     
  33.          
  34.         // 參數解釋:     
  35.         // e1:第1個ACTION_DOWN MotionEvent     
  36.         // e2:最後一個ACTION_MOVE MotionEvent     
  37.         // velocityX:X軸上的移動速度,像素/秒     
  38.         // velocityY:Y軸上的移動速度,像素/秒     
  39.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  40.                 float velocityY) {  
  41.               
  42.               
  43.             if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE    
  44.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  45.                 // Fling left     
  46.                 Log.i("MyGesture""Fling left");    
  47.                 Toast.makeText(MainActivity.this"Fling Left", Toast.LENGTH_SHORT).show();    
  48.             } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE    
  49.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  50.                 // Fling right     
  51.                 Log.i("MyGesture""Fling right");    
  52.                 Toast.makeText(MainActivity.this"Fling Right", Toast.LENGTH_SHORT).show();    
  53.             }    
  54.             return true;  
  55.         }  
  56.   
  57.     }  
  58. }  
這段代碼難度不大,就不再細講,看下效果:

源碼在博客底部給出。

源碼地址:http://download.csdn.net/detail/harvic880925/7978943

本文轉載自:http://blog.csdn.net/harvic880925/article/details/39520901   謝謝!!


一、概述

當用戶觸摸屏幕的時候,會產生許多手勢,例如down,up,scroll,filing等等。
一般情況下,我們知道View類有個View.OnTouchListener內部接口,通過重寫他的onTouch(View v, MotionEvent event)方法,我們可以處理一些touch事件,但是這個方法太過簡單,如果需要處理一些複雜的手勢,用這個接口就會很麻煩(因爲我們要自己根據用戶觸摸的軌跡去判斷是什麼手勢)。
Android sdk給我們提供了GestureDetector(Gesture:手勢Detector:識別)類,通過這個類我們可以識別很多的手勢,主要是通過他的onTouchEvent(event)方法完成了不同手勢的識別。雖然他能識別手勢,但是不同的手勢要怎麼處理,應該是提供給程序員實現的。

GestureDetector這個類對外提供了兩個接口和一個外部類
接口:OnGestureListener,OnDoubleTapListener
內部類:SimpleOnGestureListener

這個外部類,其實是兩個接口中所有函數的集成,它包含了這兩個接口裏所有必須要實現的函數而且都已經重寫,但所有方法體都是空的;不同點在於:該類是static class,程序員可以在外部繼承這個類,重寫裏面的手勢處理方法。

下面我們先看OnGestureListener接口;

二、GestureDetector.OnGestureListener---接口

1、基本講解

如果我們寫一個類並implements OnGestureListener,會提示有幾個必須重寫的函數,加上之後是這個樣子的:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class gesturelistener implements GestureDetector.OnGestureListener{  
  2.   
  3.     public boolean onDown(MotionEvent e) {  
  4.         // TODO Auto-generated method stub  
  5.         return false;  
  6.     }  
  7.   
  8.     public void onShowPress(MotionEvent e) {  
  9.         // TODO Auto-generated method stub  
  10.           
  11.     }  
  12.   
  13.     public boolean onSingleTapUp(MotionEvent e) {  
  14.         // TODO Auto-generated method stub  
  15.         return false;  
  16.     }  
  17.   
  18.     public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  19.             float distanceX, float distanceY) {  
  20.         // TODO Auto-generated method stub  
  21.         return false;  
  22.     }  
  23.   
  24.     public void onLongPress(MotionEvent e) {  
  25.         // TODO Auto-generated method stub  
  26.           
  27.     }  
  28.   
  29.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  30.             float velocityY) {  
  31.         // TODO Auto-generated method stub  
  32.         return false;  
  33.     }  
  34.       
  35. }  
可見,這裏總共重寫了六個函數,這些函數都在什麼情況下才會觸發呢,下面講一下:

OnDown(MotionEvent e):用戶按下屏幕就會觸發;
onShowPress(MotionEvent e):如果是按下的時間超過瞬間,而且在按下的時候沒有鬆開或者是拖動的,那麼onShowPress就會執行,具體這個瞬間是多久,我也不清楚呃……
onLongPress(MotionEvent e):長按觸摸屏,超過一定時長,就會觸發這個事件
    觸發順序:
    onDown->onShowPress->onLongPress

onSingleTapUp(MotionEvent e):從名子也可以看出,一次單獨的輕擊擡起操作,也就是輕擊一下屏幕,立刻擡起來,纔會有這個觸發,當然,如果除了Down以外還有其它操作,那就不再算是Single操作了,所以也就不會觸發這個事件
    觸發順序:
    點擊一下非常快的(不滑動)Touchup:
    onDown->onSingleTapUp->onSingleTapConfirmed 
    點擊一下稍微慢點的(不滑動)Touchup:
    onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用戶按下觸摸屏、快速移動後鬆開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發   
     參數解釋:
    e1:第1個ACTION_DOWN MotionEvent
    e2:最後一個ACTION_MOVE MotionEvent
    velocityX:X軸上的移動速度,像素/秒
    velocityY:Y軸上的移動速度,像素/秒   

onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖動事件。無論是用手拖動view,或者是以拋的動作滾動,都會多次觸發,這個方法       在ACTION_MOVE動作發生時就會觸發
    滑屏:手指觸動屏幕後,稍微滑動後立即鬆開
    onDown-----》onScroll----》onScroll----》onScroll----》………----->onFling
    拖動
    onDown------》onScroll----》onScroll------》onFiling

    可見,無論是滑屏,還是拖動,影響的只是中間OnScroll觸發的數量多少而已,最終都會觸發onFling事件!

2、實例

要使用GestureDetector,有三步要走:

1、創建OnGestureListener監聽函數:
可以使用構造實例:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. GestureDetector.OnGestureListener listener = new GestureDetector.OnGestureListener(){  
  2.           
  3.     };  
也可以構造類:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class gestureListener implements GestureDetector.OnGestureListener{  
  2.   
  3. }  
2、創建GestureDetector實例mGestureDetector:

構造函數有下面三個,根據需要選擇:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);  
  2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);  
  3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);  
3、onTouch(View v, MotionEvent event)中攔截:
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public boolean onTouch(View v, MotionEvent event) {  
  2.     return mGestureDetector.onTouchEvent(event);     
  3. }  

4、控件綁定

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. TextView tv = (TextView)findViewById(R.id.tv);  
  2. tv.setOnTouchListener(this);  

現在進入實例階段:

首先,在主佈局頁面添加一個textView,並將其放大到整屏,方便在其上的手勢識別,代碼爲:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.gesturedetectorinterface.MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tv"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent"  
  11.         android:layout_margin="50dip"  
  12.         android:background="#ff00ff"  
  13.         android:text="@string/hello_world" />  
  14.   
  15. </RelativeLayout>  
然後在JAVA代碼中,依據上面的三步走原則,寫出代碼,並在所有的手勢下添加上Toast提示並寫上Log

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener{  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.           
  11.       mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  
  12.           
  13.       TextView tv = (TextView)findViewById(R.id.tv);  
  14.       tv.setOnTouchListener(this);  
  15.       tv.setFocusable(true);     
  16.       tv.setClickable(true);     
  17.       tv.setLongClickable(true);   
  18.     }  
  19.       
  20.       
  21.     /*  
  22.      * 在onTouch()方法中,我們調用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector  
  23.      * 來分析是否有合適的callback函數來處理用戶的手勢  
  24.      */    
  25.     public boolean onTouch(View v, MotionEvent event) {  
  26.         return mGestureDetector.onTouchEvent(event);     
  27.     }  
  28.       
  29.     private class gestureListener implements GestureDetector.OnGestureListener{  
  30.   
  31.         // 用戶輕觸觸摸屏,由1個MotionEvent ACTION_DOWN觸發     
  32.         public boolean onDown(MotionEvent e) {  
  33.             Log.i("MyGesture""onDown");     
  34.             Toast.makeText(MainActivity.this"onDown", Toast.LENGTH_SHORT).show();     
  35.             return false;  
  36.         }  
  37.   
  38.         /*   
  39.          * 用戶輕觸觸摸屏,尚未鬆開或拖動,由一個1個MotionEvent ACTION_DOWN觸發   
  40.          * 注意和onDown()的區別,強調的是沒有鬆開或者拖動的狀態   
  41.          *  
  42.          * 而onDown也是由一個MotionEventACTION_DOWN觸發的,但是他沒有任何限制, 
  43.          * 也就是說當用戶點擊的時候,首先MotionEventACTION_DOWN,onDown就會執行, 
  44.          * 如果在按下的瞬間沒有鬆開或者是拖動的時候onShowPress就會執行,如果是按下的時間超過瞬間 
  45.          * (這塊我也不太清楚瞬間的時間差是多少,一般情況下都會執行onShowPress),拖動了,就不執行onShowPress。 
  46.          */  
  47.         public void onShowPress(MotionEvent e) {  
  48.             Log.i("MyGesture""onShowPress");     
  49.             Toast.makeText(MainActivity.this"onShowPress", Toast.LENGTH_SHORT).show();     
  50.         }  
  51.   
  52.         // 用戶(輕觸觸摸屏後)鬆開,由一個1個MotionEvent ACTION_UP觸發     
  53.         ///輕擊一下屏幕,立刻擡起來,纔會有這個觸發  
  54.         //從名子也可以看出,一次單獨的輕擊擡起操作,當然,如果除了Down以外還有其它操作,那就不再算是Single操作了,所以這個事件 就不再響應  
  55.         public boolean onSingleTapUp(MotionEvent e) {  
  56.             Log.i("MyGesture""onSingleTapUp");     
  57.             Toast.makeText(MainActivity.this"onSingleTapUp", Toast.LENGTH_SHORT).show();     
  58.             return true;     
  59.         }  
  60.   
  61.         // 用戶按下觸摸屏,並拖動,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE觸發     
  62.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  63.                 float distanceX, float distanceY) {  
  64.             Log.i("MyGesture22""onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);     
  65.             Toast.makeText(MainActivity.this"onScroll", Toast.LENGTH_LONG).show();     
  66.               
  67.             return true;     
  68.         }  
  69.   
  70.         // 用戶長按觸摸屏,由多個MotionEvent ACTION_DOWN觸發     
  71.         public void onLongPress(MotionEvent e) {  
  72.              Log.i("MyGesture""onLongPress");     
  73.              Toast.makeText(MainActivity.this"onLongPress", Toast.LENGTH_LONG).show();     
  74.         }  
  75.   
  76.         // 用戶按下觸摸屏、快速移動後鬆開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發     
  77.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  78.                 float velocityY) {  
  79.             Log.i("MyGesture""onFling");     
  80.             Toast.makeText(MainActivity.this"onFling", Toast.LENGTH_LONG).show();     
  81.             return true;  
  82.         }  
  83.     };  
  84.       
  85.   
  86. }  
源碼在博客底部給出。

三、GestureDetector.OnDoubleTapListener---接口

1、構建

有兩種方式設置雙擊監聽:

方法一:新建一個類同時派生自OnGestureListener和OnDoubleTapListener:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class gestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{  
  2.     }  
方法二:使用GestureDetector::setOnDoubleTapListener();函數設置監聽:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. //構建GestureDetector實例     
  2. mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  
  3. private class gestureListener implements GestureDetector.OnGestureListener{  
  4.       
  5. }  
  6.   
  7. //設置雙擊監聽器  
  8. mGestureDetector.setOnDoubleTapListener(new doubleTapListener());  
  9. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  10.       
  11. }  
注意:大家可以看到無論在方法一還是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我們說過GestureDetector 的構造函數,如下:
[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);  
  2. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);  
  3. GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);  
可以看到,在構造函數中,除了後面要講的SimpleOnGestureListener 以外的其它兩個構造函數都必須是OnGestureListener的實例。所以要想使用OnDoubleTapListener的幾個函數,就必須先實現OnGestureListener。

2、函數講解:

首先看一下OnDoubleTapListener接口必須重寫的三個函數:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  2.   
  3.     public boolean onSingleTapConfirmed(MotionEvent e) {  
  4.         // TODO Auto-generated method stub  
  5.         return false;  
  6.     }  
  7.   
  8.     public boolean onDoubleTap(MotionEvent e) {  
  9.         // TODO Auto-generated method stub  
  10.         return false;  
  11.     }  
  12.   
  13.     public boolean onDoubleTapEvent(MotionEvent e) {  
  14.         // TODO Auto-generated method stub  
  15.         return false;  
  16.     }  
  17. }  
onSingleTapConfirmed(MotionEvent e):單擊事件。用來判定該次點擊是SingleTap而不是DoubleTap,如果連續點擊兩次就是DoubleTap手勢,如果只點擊一次,系統等待一段時間後沒有收到第二次點擊則判定該次點擊爲SingleTap而不是DoubleTap,然後觸發SingleTapConfirmed事件。觸發順序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
關於onSingleTapConfirmed和onSingleTapUp的一點區別: OnGestureListener有這樣的一個方法onSingleTapUp,和onSingleTapConfirmed容易混淆。二者的區別是:onSingleTapUp,只要手擡起就會執行,而對於onSingleTapConfirmed來說,如果雙擊的話,則onSingleTapConfirmed不會執行。

onDoubleTap(MotionEvent e):雙擊事件

onDoubleTapEvent(MotionEvent e):雙擊間隔中發生的動作。指觸發onDoubleTap以後,在雙擊之間發生的其它動作,包含down、up和move事件;下圖是雙擊一下的Log輸出:

兩點總結:

1、從上圖可以看出,在第二下點擊時,先觸發OnDoubleTap,然後再觸發OnDown(第二次點擊)

2、其次在觸發OnDoubleTap以後,就開始觸發onDoubleTapEvent了,onDoubleTapEvent後面的數字代表了當前的事件,0指ACTION_DOWN,1指ACTION_UP,2 指ACTION_MOVE
在上一個例子的基礎上,我們再添加一個雙擊監聽類,實現如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener{  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.           
  11.   
  12.       mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  
  13.       mGestureDetector.setOnDoubleTapListener(new doubleTapListener());  
  14.           
  15.       TextView tv = (TextView)findViewById(R.id.tv);  
  16.       tv.setOnTouchListener(this);  
  17.       tv.setFocusable(true);     
  18.       tv.setClickable(true);     
  19.       tv.setLongClickable(true);   
  20.     }  
  21.       
  22.       
  23.     /*  
  24.      * 在onTouch()方法中,我們調用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector  
  25.      * 來分析是否有合適的callback函數來處理用戶的手勢  
  26.      */    
  27.     public boolean onTouch(View v, MotionEvent event) {  
  28.         return mGestureDetector.onTouchEvent(event);     
  29.     }  
  30.       
  31.     //OnGestureListener監聽  
  32.     private class gestureListener implements GestureDetector.OnGestureListener{  
  33.   
  34.         public boolean onDown(MotionEvent e) {  
  35.             Log.i("MyGesture""onDown");     
  36.             Toast.makeText(MainActivity.this"onDown", Toast.LENGTH_SHORT).show();     
  37.             return false;  
  38.         }  
  39.   
  40.         public void onShowPress(MotionEvent e) {  
  41.             Log.i("MyGesture""onShowPress");     
  42.             Toast.makeText(MainActivity.this"onShowPress", Toast.LENGTH_SHORT).show();     
  43.         }  
  44.   
  45.         public boolean onSingleTapUp(MotionEvent e) {  
  46.             Log.i("MyGesture""onSingleTapUp");     
  47.             Toast.makeText(MainActivity.this"onSingleTapUp", Toast.LENGTH_SHORT).show();     
  48.             return true;     
  49.         }  
  50.   
  51.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  52.                 float distanceX, float distanceY) {  
  53.             Log.i("MyGesture22""onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);     
  54.             Toast.makeText(MainActivity.this"onScroll", Toast.LENGTH_LONG).show();     
  55.               
  56.             return true;     
  57.         }  
  58.   
  59.         public void onLongPress(MotionEvent e) {  
  60.              Log.i("MyGesture""onLongPress");     
  61.              Toast.makeText(MainActivity.this"onLongPress", Toast.LENGTH_LONG).show();     
  62.         }  
  63.   
  64.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  65.                 float velocityY) {  
  66.             Log.i("MyGesture""onFling");     
  67.             Toast.makeText(MainActivity.this"onFling", Toast.LENGTH_LONG).show();     
  68.             return true;  
  69.         }  
  70.     };  
  71.       
  72.     //OnDoubleTapListener監聽  
  73.     private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  
  74.   
  75.         public boolean onSingleTapConfirmed(MotionEvent e) {  
  76.             Log.i("MyGesture""onSingleTapConfirmed");     
  77.             Toast.makeText(MainActivity.this"onSingleTapConfirmed", Toast.LENGTH_LONG).show();    
  78.             return true;  
  79.         }  
  80.   
  81.         public boolean onDoubleTap(MotionEvent e) {  
  82.             Log.i("MyGesture""onDoubleTap");     
  83.             Toast.makeText(MainActivity.this"onDoubleTap", Toast.LENGTH_LONG).show();    
  84.             return true;  
  85.         }  
  86.   
  87.         public boolean onDoubleTapEvent(MotionEvent e) {  
  88.             Log.i("MyGesture""onDoubleTapEvent");     
  89.             Toast.makeText(MainActivity.this"onDoubleTapEvent", Toast.LENGTH_LONG).show();    
  90.             return true;  
  91.         }  
  92.     };  
  93. }  
雙擊一下,部分截圖如下:


雙擊所對應的觸發事件順序:


輕輕單擊一下,對應的事件觸發順序爲:


源碼在博客底部給出。

四、GestureDetector.SimpleOnGestureListener---類

它與前兩個不同的是:
1、這是一個類,在它基礎上新建類的話,要用extends派生而不是用implements繼承!
2、OnGestureListener和OnDoubleTapListener接口裏的函數都是強制必須重寫的,即使用不到也要重寫出來一個空函數但在SimpleOnGestureListener類的實例或派生類中不必如此,可以根據情況,用到哪個函數就重寫哪個函數,因爲SimpleOnGestureListener類本身已經實現了這兩個接口的所有函數,只是裏面全是空的而已。

下面利用SimpleOnGestureListener類來重新實現上面的幾個效果,代碼如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener {  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         mGestureDetector = new GestureDetector(new simpleGestureListener());  
  11.           
  12.         TextView tv = (TextView)findViewById(R.id.tv);  
  13.         tv.setOnTouchListener(this);  
  14.         tv.setFocusable(true);     
  15.         tv.setClickable(true);     
  16.         tv.setLongClickable(true);   
  17.     }  
  18.       
  19.     public boolean onTouch(View v, MotionEvent event) {  
  20.         // TODO Auto-generated method stub  
  21.         return mGestureDetector.onTouchEvent(event);     
  22.     }  
  23.   
  24.     private class simpleGestureListener extends  
  25.             GestureDetector.SimpleOnGestureListener {  
  26.           
  27.         /*****OnGestureListener的函數*****/  
  28.         public boolean onDown(MotionEvent e) {  
  29.             Log.i("MyGesture""onDown");  
  30.             Toast.makeText(MainActivity.this"onDown", Toast.LENGTH_SHORT)  
  31.                     .show();  
  32.             return false;  
  33.         }  
  34.   
  35.         public void onShowPress(MotionEvent e) {  
  36.             Log.i("MyGesture""onShowPress");  
  37.             Toast.makeText(MainActivity.this"onShowPress", Toast.LENGTH_SHORT)  
  38.                     .show();  
  39.         }  
  40.   
  41.         public boolean onSingleTapUp(MotionEvent e) {  
  42.             Log.i("MyGesture""onSingleTapUp");  
  43.             Toast.makeText(MainActivity.this"onSingleTapUp",  
  44.                     Toast.LENGTH_SHORT).show();  
  45.             return true;  
  46.         }  
  47.   
  48.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  49.                 float distanceX, float distanceY) {  
  50.             Log.i("MyGesture""onScroll:" + (e2.getX() - e1.getX()) + "   "  
  51.                     + distanceX);  
  52.             Toast.makeText(MainActivity.this"onScroll", Toast.LENGTH_LONG)  
  53.                     .show();  
  54.   
  55.             return true;  
  56.         }  
  57.   
  58.         public void onLongPress(MotionEvent e) {  
  59.             Log.i("MyGesture""onLongPress");  
  60.             Toast.makeText(MainActivity.this"onLongPress", Toast.LENGTH_LONG)  
  61.                     .show();  
  62.         }  
  63.   
  64.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  65.                 float velocityY) {  
  66.             Log.i("MyGesture""onFling");  
  67.             Toast.makeText(MainActivity.this"onFling", Toast.LENGTH_LONG)  
  68.                     .show();  
  69.             return true;  
  70.         }  
  71.           
  72.         /*****OnDoubleTapListener的函數*****/  
  73.         public boolean onSingleTapConfirmed(MotionEvent e) {  
  74.             Log.i("MyGesture""onSingleTapConfirmed");  
  75.             Toast.makeText(MainActivity.this"onSingleTapConfirmed",  
  76.                     Toast.LENGTH_LONG).show();  
  77.             return true;  
  78.         }  
  79.   
  80.         public boolean onDoubleTap(MotionEvent e) {  
  81.             Log.i("MyGesture""onDoubleTap");  
  82.             Toast.makeText(MainActivity.this"onDoubleTap", Toast.LENGTH_LONG)  
  83.                     .show();  
  84.             return true;  
  85.         }  
  86.   
  87.         public boolean onDoubleTapEvent(MotionEvent e) {  
  88.             Log.i("MyGesture""onDoubleTapEvent");  
  89.             Toast.makeText(MainActivity.this"onDoubleTapEvent",  
  90.                     Toast.LENGTH_LONG).show();  
  91.             return true;  
  92.         }  
  93.   
  94.     }  
  95. }  

到此,有關GestureDetector的所有基礎知識都講解完了,下面給出一個小應用——識別用戶是向左滑還是向右滑!

源碼在博客底部給出。

五、OnFling應用——識別向左滑還是向右滑

這部分就有點意思了,可以說是上面知識的一個小應用,我們利用OnFling函數來識別當前用戶是在向左滑還是向右滑,從而打出日誌。先看下OnFling的參數:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. 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軸上的移動速度,像素/秒     
首先,先說一下實現的功能:當用戶向左滑動距離超過100px,且滑動速度超過100 px/s時,即判斷爲向左滑動;向右同理.代碼如下:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class MainActivity extends Activity implements OnTouchListener {  
  2.   
  3.     private GestureDetector mGestureDetector;     
  4.       
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.           
  10.         mGestureDetector = new GestureDetector(new simpleGestureListener());  
  11.           
  12.         TextView tv = (TextView)findViewById(R.id.tv);  
  13.         tv.setOnTouchListener(this);  
  14.         tv.setFocusable(true);     
  15.         tv.setClickable(true);     
  16.         tv.setLongClickable(true);   
  17.     }  
  18.       
  19.     public boolean onTouch(View v, MotionEvent event) {  
  20.         // TODO Auto-generated method stub  
  21.         return mGestureDetector.onTouchEvent(event);     
  22.     }  
  23.   
  24.     private class simpleGestureListener extends  
  25.             GestureDetector.SimpleOnGestureListener {  
  26.           
  27.         /*****OnGestureListener的函數*****/  
  28.   
  29.         final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;    
  30.           
  31.         // 觸發條件 :     
  32.         // X軸的座標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒     
  33.          
  34.         // 參數解釋:     
  35.         // e1:第1個ACTION_DOWN MotionEvent     
  36.         // e2:最後一個ACTION_MOVE MotionEvent     
  37.         // velocityX:X軸上的移動速度,像素/秒     
  38.         // velocityY:Y軸上的移動速度,像素/秒     
  39.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  40.                 float velocityY) {  
  41.               
  42.               
  43.             if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE    
  44.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  45.                 // Fling left     
  46.                 Log.i("MyGesture""Fling left");    
  47.                 Toast.makeText(MainActivity.this"Fling Left", Toast.LENGTH_SHORT).show();    
  48.             } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE    
  49.                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {    
  50.                 // Fling right     
  51.                 Log.i("MyGesture""Fling right");    
  52.                 Toast.makeText(MainActivity.this"Fling Right", Toast.LENGTH_SHORT).show();    
  53.             }    
  54.             return true;  
  55.         }  
  56.   
  57.     }  
  58. }  
這段代碼難度不大,就不再細講,看下效果:

源碼在博客底部給出。


源碼地址:http://download.csdn.net/detail/harvic880925/7978943


請大家尊重原創者版權,轉載請標明出處:http://blog.csdn.net/harvic880925/article/details/39520901   謝謝!!


發佈了32 篇原創文章 · 獲贊 45 · 訪問量 13萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章