Android手勢識別器GestureDetector使用詳解
以前只知道控件的onTouchEvent()事件,它的動(dòng)作有MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP;今天有個(gè)需求,要監(jiān)聽控件的雙擊、拖動(dòng)、滑動(dòng)等事件,這時(shí)onTouchEvent()很明顯不能滿足我們的需求,經(jīng)多方打聽,找到了今天的主角GestureDetector,下面就對它進(jìn)行簡單的學(xué)習(xí)。
構(gòu)造方法:
已過時(shí)的有2個(gè),不推薦使用。
GestureDetector(GestureDetector.onGestureListener listener);
GestureDetector(GestureDetector.onGestureListener listener,Handler handler);
推薦使用。
GestureDeterctor(Context context,GestureDetector.onGestureListener listener);
GestureDeterctor(Context context,GestureDetector.onGestureListener listener,Handler handler);
GestureDeterctor(Context context,GestureDetector.onGestureListener listener,Handler handler,boolean unused);
參數(shù)handler主要用來執(zhí)行延時(shí)操作時(shí)使用,參數(shù)unused暫時(shí)沒有使用。
從構(gòu)成函數(shù)可以看出,當(dāng)我們需要?jiǎng)?chuàng)建一個(gè)GestureDetector對象時(shí),必須給它傳一個(gè)GestureDetector.onGestureListener對象,查看API之后,發(fā)現(xiàn)它是個(gè)接口(interface),創(chuàng)建GestureDetector.onGestureListener的對象時(shí),必須實(shí)現(xiàn)一下幾個(gè)方法:
1、onDown(MotionEvent e);
當(dāng)用戶按下時(shí)的回調(diào)。
2、onFling(MotionEvent e1,MontionEvent e2,float velocityX,float velocityY);
當(dāng)用戶快速拖動(dòng),并離開屏幕時(shí),控件還在滑動(dòng)的回調(diào)。
3、onLongPress(MotionEvent e);
當(dāng)用戶長按控件時(shí)的回調(diào)。
4、onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY);
當(dāng)用戶拖著控件(控件本身并沒有動(dòng))滑動(dòng)時(shí)的回調(diào)。
5、onShowPress(MotionEvent e);
當(dāng)用戶按下,還沒有執(zhí)行移動(dòng)或者抬起的動(dòng)作的回調(diào)。
6、onSingleTapUp(MotionEvent e);
用戶按下,立即抬起,沒有做其它動(dòng)作時(shí)的回調(diào)。
有了這個(gè)監(jiān)聽之后,我們還可以給GestureDetector設(shè)置雙擊監(jiān)聽,使用的方法是:
mGestureDetector.setOnDoubleTapListener(GestureDetector.OnDoubleTapListener onDoubleListener);
參數(shù)是雙擊監(jiān)聽的對象,GestureDetector.OnDoubleTapListener它也是一個(gè)接口(interface),創(chuàng)建它的對象時(shí),也必須實(shí)現(xiàn)以下幾個(gè)方法。
1、onDoubleTap(MotionEvent e);
當(dāng)用戶雙擊時(shí)回調(diào)。
2、onDoubleTapEvent(MotionEvent e);
雙擊間隔事件的回調(diào)。
3、onSingleTapConfirmed(MotionEvent e);
當(dāng)用戶單擊時(shí)回調(diào)。
上面的兩個(gè)監(jiān)聽,回調(diào)函數(shù)都是必須實(shí)現(xiàn),有時(shí)候我們不需要監(jiān)聽所有的事件,只對自己感興趣的事件進(jìn)行監(jiān)聽,GestureDetector有個(gè)內(nèi)部類幫我們實(shí)現(xiàn)此功能GestureDetector.SimpleOnGestureListener,該類實(shí)現(xiàn)了GestureDetector.onGestureListener、GestureDetector.onDoubleTapListener、GestureDetector.onContextClickListener這三個(gè)接口,并實(shí)現(xiàn)了它們的方法,只不過是空實(shí)現(xiàn),在我們需要這三個(gè)接口的時(shí)候,我們可以創(chuàng)建GestureDetector.SimpleOnGestureListener對象,然后需要監(jiān)聽哪個(gè)事件,我們就重寫它的哪個(gè)方法,下面我把它的所有方法都實(shí)現(xiàn)了,其實(shí)和上邊兩個(gè)接口實(shí)現(xiàn)的方法是一樣。
GestureDetector.SimpleOnGestureListener mGestureDetector = new SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return super.onSingleTapUp(e);
}
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onShowPress(MotionEvent e) {
super.onShowPress(e);
}
@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return super.onDoubleTap(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return super.onDoubleTapEvent(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
};
只有理論沒有實(shí)踐,怎么行呢?這里我也寫了一個(gè)Demo,這個(gè)Demo是我從另一篇博客中抄的,文章的內(nèi)容也是參考他的博客寫的,下面會(huì)把大神的那篇博客地址貼出來。
參考資料:Android GestureDetector用戶手勢檢測實(shí)例講解
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android GestureDetector用戶手勢檢測實(shí)例講解
- android使用gesturedetector手勢識別示例分享
- Android GestureDetector手勢滑動(dòng)使用實(shí)例講解
- Android自定義viewgroup可滾動(dòng)布局 GestureDetector手勢監(jiān)聽(5)
- Android自定義GestureDetector實(shí)現(xiàn)手勢ImageView
- Android GestureDetector實(shí)現(xiàn)手勢滑動(dòng)效果
- Android編程使用GestureDetector實(shí)現(xiàn)簡單手勢監(jiān)聽與處理的方法
- Android觸摸及手勢操作GestureDetector
- Android使用手勢監(jiān)聽器GestureDetector遇到的不響應(yīng)問題
- Android如何使用GestureDetector進(jìn)行手勢檢測詳解
相關(guān)文章
Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫效果實(shí)例
我想看到今日頭條的點(diǎn)贊效果,應(yīng)該都覺得很絢麗吧,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫效果的相關(guān)資料,文中通過示例代價(jià)介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2
這篇文章主要介紹了Android實(shí)現(xiàn)自動(dòng)變換大小的組件ViewPager2,ViewPager2最顯著的特點(diǎn)是基于RecyclerView實(shí)現(xiàn),RecyclerView是目前Android端最成熟的AdapterView解決方案2023-03-03
Android中關(guān)于定時(shí)任務(wù)實(shí)現(xiàn)關(guān)閉訂單問題
在電商、支付等領(lǐng)域,往往會(huì)有這樣的場景,用戶下單后放棄支付了,那這筆訂單會(huì)在指定的時(shí)間段后進(jìn)行關(guān)閉操作,細(xì)心的你一定發(fā)現(xiàn)了像某寶、某東都有這樣的邏輯,而且時(shí)間很準(zhǔn)確,誤差在1s內(nèi);那他們是怎么實(shí)現(xiàn)的呢?今天通過本文學(xué)習(xí)定時(shí)任務(wù)實(shí)現(xiàn)關(guān)閉訂單問題2022-05-05
Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的方法匯總
這篇文章主要介紹了Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的方法匯總,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06
Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
今天給大家?guī)淼氖莾H僅使用一個(gè)TextView實(shí)現(xiàn)一個(gè)高仿京東、淘寶、唯品會(huì)等各種電商APP的活動(dòng)倒計(jì)時(shí)。今天小編把實(shí)現(xiàn)代碼分享到腳本之家平臺(tái),對android textclock 倒計(jì)時(shí)效果感興趣的朋友參考下吧2016-10-10
基于Android XML解析與保存的實(shí)現(xiàn)
本篇文章小編為大家介紹,基于Android XML解析與保存的實(shí)現(xiàn)。需要的朋友參考下2013-04-04
Android定時(shí)器實(shí)現(xiàn)的幾種方式整理及removeCallbacks失效問題解決
本文為大家詳細(xì)介紹下Android 定時(shí)器實(shí)現(xiàn)的幾種方式:Handler + Runnable、Timer的方式、Handle與線程的sleep(long )方法和removeCallbacks失效問題如何解決2013-06-06
Android修改DatePicker字體顏色及分割線顏色詳細(xì)介紹
這篇文章主要介紹了Android修改DatePicker字體顏色及分割線顏色詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下2017-05-05

