Android手勢操作簡單實例講解
上一篇介紹的onTouch提供的事件還是相對較簡單,如果需要處理一些復雜的手勢,用這個接口就會很麻煩,因為我們要根據(jù)用戶觸摸的軌跡去判斷是什么手勢。幸好Android SDK給我們提供了GestureDetector類,通過這個類我們可以識別很多的手勢,主要是通過他的onTouchEvent(event)方法完成了不同手勢的識別。
GestureDetector這個類對外提供了兩個接口和一個外部類:
•接口:OnGestureListener,OnDoubleTapListener
•外部類:SimpleOnGestureListener
這個外部類,其實是兩個接口中所有函數(shù)的集成,它包含了這兩個接口里所有必須要實現(xiàn)的函數(shù)而且都已經(jīng)重寫,但所有方法體都是空的;不同點在于:該類是static class,程序員可以在外部繼承這個類,重寫里面的手勢處理方法。
OnGestureListener有下面的幾個動作:
•onDown(MotionEvent e):用戶按下屏幕就會觸發(fā)。
•onShowPress(MotionEvent e):如果按下的時間超過瞬間,而且在按下的時候沒有松開或者是拖動的,那么onShowPress就會執(zhí)行,具體這個瞬間是多久,我也不清楚…
•onLongPress(MotionEvent e):長按觸摸屏,超過一定時長,就會觸發(fā)這個事件。
觸發(fā)順序:onDown->onShowPress->onLongPress
•onSingleTapUp(MotionEvent e):一次單獨的輕擊抬起操作,也就是輕擊一下屏幕,立刻抬起來,才會有這個觸發(fā),當然,如果除了Down以外還有其它操作,那就不再是Single操作了,所以也就不會觸發(fā)這個事件。
觸發(fā)順序:
點擊一下非??斓模ú换瑒樱㏕ouchup:
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觸發(fā)。
參數(shù)解釋:
e1:第1個ACTION_DOWN MotionEvent
e2:最后一個ACTION_MOVE MotionEvent
velocityX:X軸上的移動速度,像素/秒
velocityY:Y軸上的移動速度,像素/秒
•onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖動事件。無論是用手拖動view,或者是以拋的動作滾動,都會多次觸發(fā),這個方法在ACTION_MOVE動作發(fā)生時就會觸發(fā)。
OnDoubleTapListener有下面的幾個動作:
•onSingleTapConfirmed(MotionEvent e):單擊事件。用來判定該次點擊是SingleTap而不是DoubleTap,如果連續(xù)點擊兩次就是DoubleTap手勢,如果只點擊一次,系統(tǒng)等待一段時間后沒有收到第二次點擊則判定該次點擊為SingleTap而不是DoubleTap,然后觸發(fā)SingleTapConfirmed事件。
觸發(fā)順序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed
•onDoubleTap(MotionEvent e):雙擊事件。
•onDoubleTapEvent(MotionEvent e):雙擊間隔中發(fā)生的動作。指觸發(fā)onDoubleTap以后,在雙擊之間發(fā)生的其它動作,包含down、up和move事件。
關于onSingleTapConfirmed和onSingleTapUp的區(qū)別:onSingleTapUp,只要手抬起就會執(zhí)行,而對于onSingleTapConfirmed來說,如果雙擊的話,則onSingleTapConfirmed不會執(zhí)行。
使用Gesture
•使用GestureDetector.OnGestureListener接口
要使用OnGestureListener接口,大致有幾步要走:
1、創(chuàng)建OnGestureListener監(jiān)聽函數(shù):
private class gestureListener implements GestureDetector.OnGestureListener{
}
2、創(chuàng)建GestureDetector實例:
構造函數(shù)有下面三個,根據(jù)需要選擇:
GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);
注:GestureDetector現(xiàn)在已經(jīng)是deprecation狀態(tài),現(xiàn)在推薦GestureDetectorCompat
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);
3、onTouch(View v, MotionEvent event)中攔截,在onTouch()方法中,我們調(diào)用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector來分析是否有合適的callback函數(shù)來處理用戶的手勢
public boolean onTouch(View v, MotionEvent event) { return gestureDetectorCompat.onTouchEvent(event); }
4、控件綁定
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
實現(xiàn)代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="50dip" android:background="#76EE00" android:text="Gesture Detector" /> </RelativeLayout>
public class MainActivity extends Activity implements OnTouchListener{ private GestureDetectorCompat mGestureDetectorCompat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener()); TextView tv = (TextView)findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } public boolean onTouch(View v, MotionEvent event) { return mGestureDetectorCompat.onTouchEvent(event); } private class gestureListener implements GestureDetector.OnGestureListener{ public boolean onDown(MotionEvent e) { showlog("onDown"); Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show(); return false; } public void onShowPress(MotionEvent e) { showlog("onShowPress"); Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show(); } public boolean onSingleTapUp(MotionEvent e) { showlog("onSingleTapUp"); Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show(); return true; } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { showlog("onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX); Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show(); return true; } public void onLongPress(MotionEvent e) { showlog("onLongPress"); Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show(); } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { showlog("onFling"); Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show(); return true; } }; public void showlog(String info) { System.out.print("GestureDetector "+info); } }
•使用GestureDetector.OnDoubleTapListener接口
實現(xiàn)OnDoubleTapListener接口的前提是先實現(xiàn)OnGestureListener接口,其實除了第1步,2、3、4步和上面完全一樣,不再贅述,下面看下第一步:
同時創(chuàng)建OnGestureListener和OnDoubleTapListener監(jiān)聽函數(shù):
方法一:新建一個類同時派生自OnGestureListener和OnDoubleTapListener:
}
方法二:使用GestureDetector.setOnDoubleTapListener();函數(shù)設置監(jiān)聽:
/構建GestureDetector實例 mGestureDetector = new GestureDetector(new gestureListener()); private class gestureListener implements GestureDetector.OnGestureListener{ } //設置雙擊監(jiān)聽器 mGestureDetector.setOnDoubleTapListener(new doubleTapListener()); private class doubleTapListener implements GestureDetector.OnDoubleTapListener{ }
注:大家可以看到無論在方法一還是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我們說過GestureDetectorCompat 的構造函數(shù),如下:
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener);
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);
可以看到,它的兩個構造函數(shù)參數(shù)都必須是OnGestureListener的實例。所以要想使用OnDoubleTapListener的幾個函數(shù),就必須先實現(xiàn)OnGestureListener。
實現(xiàn)代碼:
public class MainActivity extends Activity implements OnTouchListener{ private GestureDetectorCompat mGestureDetectorCompat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetectorCompat = new GestureDetectorCompat(this, new gestureListener()); TextView tv = (TextView)findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } public boolean onTouch(View v, MotionEvent event) { return mGestureDetectorCompat.onTouchEvent(event); } private class gestureListener implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{ public boolean onDown(MotionEvent e) { showlog("onDown"); Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show(); return false; } public void onShowPress(MotionEvent e) { showlog("onShowPress"); Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show(); } public boolean onSingleTapUp(MotionEvent e) { showlog("onSingleTapUp"); Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show(); return true; } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { showlog("onScroll:"+(e2.getX()-e1.getX()) +" "+distanceX); Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_LONG).show(); return true; } public void onLongPress(MotionEvent e) { showlog("onLongPress"); Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_LONG).show(); } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { showlog("onFling"); Toast.makeText(MainActivity.this, "onFling", Toast.LENGTH_LONG).show(); return true; } public boolean onSingleTapConfirmed(MotionEvent e) { showlog("onSingleTapConfirmed"); Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show(); return true; } public boolean onDoubleTap(MotionEvent e) { showlog("onDoubleTap"); Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show(); return true; } public boolean onDoubleTapEvent(MotionEvent e) { showlog("onDoubleTapEvent"); Toast.makeText(MainActivity.this, "onDoubleTapEvent", Toast.LENGTH_LONG).show(); return true; } public void showlog(String info) { System.out.print("GestureDetector "+info); } }
•使用GestureDetector.SimpleOnGestureListener類
使用OnGestureListener和OnDoubleTapListener接口,這樣需要重載接口所有的方法,適合監(jiān)聽所有的手勢,如果我們只想監(jiān)聽某個手勢或某幾個手勢呢,這時候就可以使用SimpleOnGestureListener類了。
它與前兩個不同的是:
1、這是一個類,在它基礎上新建類的話,要用extends派生而不是用implements繼承!
2、OnGestureListener和OnDoubleTapListener接口里的函數(shù)都是強制必須重寫的,即使用不到也要重寫出來一個空函數(shù)但在SimpleOnGestureListener類的實例或派生類中不必如此,可以根據(jù)情況,用到哪個函數(shù)就重寫哪個函數(shù),因為SimpleOnGestureListener類本身已經(jīng)實現(xiàn)了這兩個接口的所有函數(shù),只是里面全是空的而已。
public class MainActivity extends Activity implements OnTouchListener { private GestureDetector mGestureDetector; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mGestureDetector = new GestureDetector(new simpleGestureListener()); TextView tv = (TextView)findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } public boolean onTouch(View v, MotionEvent event) { return mGestureDetector.onTouchEvent(event); } private class simpleGestureListener extends GestureDetector.SimpleOnGestureListener { /*****OnGestureListener的函數(shù)*****/ public boolean onDown(MotionEvent e) { Log.i("MyGesture", "onDown"); Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show(); return false; } public void onShowPress(MotionEvent e) { Log.i("MyGesture", "onShowPress"); Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show(); } public boolean onSingleTapUp(MotionEvent e) { Log.i("MyGesture", "onSingleTapUp"); Toast.makeText(MainActivity.this, "onSingleTapUp", Toast.LENGTH_SHORT).show(); return true; } /*****OnDoubleTapListener的函數(shù)*****/ public boolean onSingleTapConfirmed(MotionEvent e) { Log.i("MyGesture", "onSingleTapConfirmed"); Toast.makeText(MainActivity.this, "onSingleTapConfirmed",Toast.LENGTH_LONG).show(); return true; } public boolean onDoubleTap(MotionEvent e) { Log.i("MyGesture", "onDoubleTap"); Toast.makeText(MainActivity.this, "onDoubleTap", Toast.LENGTH_LONG).show(); return true; } } }
源碼下載:http://xiazai.jb51.net/201609/yuanma/GestureDetector(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android 自動補全提示輸入AutoCompleteTextView、 MultiAutoCompleteTextV
本文主要介紹了Android自動補全提示輸入AutoCompleteTextView、 MultiAutoCompleteTextView,具有一定的參考作用,下面跟著小編一起來看下吧2017-01-01android scrollview 滑動到頂端或者指定位置的實現(xiàn)方法
下面小編就為大家?guī)硪黄猘ndroid scrollview 滑動到頂端或者指定位置的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android NDK開發(fā)簡單程序分享(Hello Word!)
本文主要對Android NDK開發(fā)簡單程序(Hello Word!)的實現(xiàn)步驟及方法進行詳細介紹。具有很好的參考價值,需要的朋友一起來看下吧2016-12-1236個Android開發(fā)常用經(jīng)典代碼大全
本篇文章主要介紹了36個Android開發(fā)常用經(jīng)典代碼片段,都是實用的代碼段,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11android開發(fā)教程之a(chǎn)ndroid的handler使用方法
這篇文章主要介紹了android的handler使用方法,大家參考使用吧2014-01-01Android開發(fā)之OpenGL繪制2D圖形的方法分析
這篇文章主要介紹了Android開發(fā)之OpenGL繪制2D圖形的方法,結合實例形式分析了Android使用OpenGL ES的圖形繪制組件實現(xiàn)2D圖形繪制的原理、步驟及相關代碼注意事項,需要的朋友可以參考下2017-09-09