Android的手势操作(Gesture)
上一篇介紹的onTouch提供的事件還是相對(duì)較簡(jiǎn)單,如果需要處理一些復(fù)雜的手勢(shì),用這個(gè)接口就會(huì)很麻煩,因?yàn)槲覀円鶕?jù)用戶(hù)觸摸的軌跡去判斷是什么手勢(shì)。幸好Android SDK給我們提供了GestureDetector類(lèi),通過(guò)這個(gè)類(lèi)我們可以識(shí)別很多的手勢(shì),主要是通過(guò)他的onTouchEvent(event)方法完成了不同手勢(shì)的識(shí)別。
介紹
GestureDetector這個(gè)類(lèi)對(duì)外提供了兩個(gè)接口和一個(gè)外部類(lèi):
- 接口:OnGestureListener,OnDoubleTapListener
- 外部類(lèi):SimpleOnGestureListener
這個(gè)外部類(lèi),其實(shí)是兩個(gè)接口中所有函數(shù)的集成,它包含了這兩個(gè)接口里所有必須要實(shí)現(xiàn)的函數(shù)而且都已經(jīng)重寫(xiě),但所有方法體都是空的;不同點(diǎn)在于:該類(lèi)是static class,程序員可以在外部繼承這個(gè)類(lèi),重寫(xiě)里面的手勢(shì)處理方法。
OnGestureListener有下面的幾個(gè)動(dòng)作:
- onDown(MotionEvent e):用戶(hù)按下屏幕就會(huì)觸發(fā)。
- onShowPress(MotionEvent e):如果按下的時(shí)間超過(guò)瞬間,而且在按下的時(shí)候沒(méi)有松開(kāi)或者是拖動(dòng)的,那么onShowPress就會(huì)執(zhí)行,具體這個(gè)瞬間是多久,我也不清楚…
- onLongPress(MotionEvent e):長(zhǎng)按觸摸屏,超過(guò)一定時(shí)長(zhǎng),就會(huì)觸發(fā)這個(gè)事件。
觸發(fā)順序:onDown->onShowPress->onLongPress - onSingleTapUp(MotionEvent e):一次單獨(dú)的輕擊抬起操作,也就是輕擊一下屏幕,立刻抬起來(lái),才會(huì)有這個(gè)觸發(fā),當(dāng)然,如果除了Down以外還有其它操作,那就不再是Single操作了,所以也就不會(huì)觸發(fā)這個(gè)事件。
觸發(fā)順序:
點(diǎn)擊一下非??斓?#xff08;不滑動(dòng))Touchup:
onDown->onSingleTapUp->onSingleTapConfirmed
點(diǎn)擊一下稍微慢點(diǎn)的(不滑動(dòng))Touchup:
onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed - onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) :滑屏,用戶(hù)按下觸摸屏、快速移動(dòng)后松開(kāi),由1個(gè)MotionEvent ACTION_DOWN, 多個(gè)ACTION_MOVE, 1個(gè)ACTION_UP觸發(fā)。
參數(shù)解釋:
e1:第1個(gè)ACTION_DOWN MotionEvent
e2:最后一個(gè)ACTION_MOVE MotionEvent
velocityX:X軸上的移動(dòng)速度,像素/秒
velocityY:Y軸上的移動(dòng)速度,像素/秒 - onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY):在屏幕上拖動(dòng)事件。無(wú)論是用手拖動(dòng)view,或者是以?huà)伒膭?dòng)作滾動(dòng),都會(huì)多次觸發(fā),這個(gè)方法在ACTION_MOVE動(dòng)作發(fā)生時(shí)就會(huì)觸發(fā)。
OnDoubleTapListener有下面的幾個(gè)動(dòng)作:
- onSingleTapConfirmed(MotionEvent e):單擊事件。用來(lái)判定該次點(diǎn)擊是SingleTap而不是DoubleTap,如果連續(xù)點(diǎn)擊兩次就是DoubleTap手勢(shì),如果只點(diǎn)擊一次,系統(tǒng)等待一段時(shí)間后沒(méi)有收到第二次點(diǎn)擊則判定該次點(diǎn)擊為SingleTap而不是DoubleTap,然后觸發(fā)SingleTapConfirmed事件。
觸發(fā)順序是:OnDown->OnsingleTapUp->OnsingleTapConfirmed - onDoubleTap(MotionEvent e):雙擊事件。
- onDoubleTapEvent(MotionEvent e):雙擊間隔中發(fā)生的動(dòng)作。指觸發(fā)onDoubleTap以后,在雙擊之間發(fā)生的其它動(dòng)作,包含down、up和move事件。
關(guān)于onSingleTapConfirmed和onSingleTapUp的區(qū)別:onSingleTapUp,只要手抬起就會(huì)執(zhí)行,而對(duì)于onSingleTapConfirmed來(lái)說(shuō),如果雙擊的話(huà),則onSingleTapConfirmed不會(huì)執(zhí)行。
使用Gesture
使用GestureDetector.OnGestureListener接口
要使用OnGestureListener接口,大致有幾步要走:
1、創(chuàng)建OnGestureListener監(jiān)聽(tīng)函數(shù):
2、創(chuàng)建GestureDetector實(shí)例:
構(gòu)造函數(shù)有下面三個(gè),根據(jù)需要選擇:
注: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來(lái)分析是否有合適的callback函數(shù)來(lái)處理用戶(hù)的手勢(shì)
public boolean onTouch(View v, MotionEvent event) { return gestureDetectorCompat.onTouchEvent(event); }4、控件綁定
TextView tv = (TextView)findViewById(R.id.tv); tv.setOnTouchListener(this);實(shí)現(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接口
實(shí)現(xiàn)OnDoubleTapListener接口的前提是先實(shí)現(xiàn)OnGestureListener接口,其實(shí)除了第1步,2、3、4步和上面完全一樣,不再贅述,下面看下第一步:
同時(shí)創(chuàng)建OnGestureListener和OnDoubleTapListener監(jiān)聽(tīng)函數(shù):
方法一:新建一個(gè)類(lèi)同時(shí)派生自O(shè)nGestureListener和OnDoubleTapListener:
方法二:使用GestureDetector.setOnDoubleTapListener();函數(shù)設(shè)置監(jiān)聽(tīng):
/構(gòu)建GestureDetector實(shí)例 mGestureDetector = new GestureDetector(new gestureListener()); private class gestureListener implements GestureDetector.OnGestureListener{ } //設(shè)置雙擊監(jiān)聽(tīng)器 mGestureDetector.setOnDoubleTapListener(new doubleTapListener()); private class doubleTapListener implements GestureDetector.OnDoubleTapListener{ }注:大家可以看到無(wú)論在方法一還是在方法二中,都需要派生自GestureDetector.OnGestureListener,前面我們說(shuō)過(guò)GestureDetectorCompat 的構(gòu)造函數(shù),如下:
GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener); GestureDetectorCompat gestureDetectorCompat=new GestureDetectorCompat(Context context,GestureDetector.OnGestureListener listener, Handler handler);可以看到,它的兩個(gè)構(gòu)造函數(shù)參數(shù)都必須是OnGestureListener的實(shí)例。所以要想使用OnDoubleTapListener的幾個(gè)函數(shù),就必須先實(shí)現(xiàn)OnGestureListener。
實(shí)現(xiàn)代碼:
使用GestureDetector.SimpleOnGestureListener類(lèi)
使用OnGestureListener和OnDoubleTapListener接口,這樣需要重載接口所有的方法,適合監(jiān)聽(tīng)所有的手勢(shì),如果我們只想監(jiān)聽(tīng)某個(gè)手勢(shì)或某幾個(gè)手勢(shì)呢,這時(shí)候就可以使用SimpleOnGestureListener類(lèi)了。
它與前兩個(gè)不同的是:
1、這是一個(gè)類(lèi),在它基礎(chǔ)上新建類(lèi)的話(huà),要用extends派生而不是用implements繼承!
2、OnGestureListener和OnDoubleTapListener接口里的函數(shù)都是強(qiáng)制必須重寫(xiě)的,即使用不到也要重寫(xiě)出來(lái)一個(gè)空函數(shù)但在SimpleOnGestureListener類(lèi)的實(shí)例或派生類(lèi)中不必如此,可以根據(jù)情況,用到哪個(gè)函數(shù)就重寫(xiě)哪個(gè)函數(shù),因?yàn)镾impleOnGestureListener類(lèi)本身已經(jīng)實(shí)現(xiàn)了這兩個(gè)接口的所有函數(shù),只是里面全是空的而已。
Demo下載地址
總結(jié)
以上是生活随笔為你收集整理的Android的手势操作(Gesture)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 《行为经济学》北京大学 孟涓涓 第四章
- 下一篇: Android 指南针