以前只知道控件的onTouchEvent()事件,它的動(dòng)作有MotionEvent.ACTION_DOWN、MotionEvent.ACTION_MOVE、MotionEvent.ACTION_UP;今天有個(gè)需求,要監(jiān)聽(tīng)控件的雙擊、拖動(dòng)、滑動(dòng)等事件,這時(shí)onTouchEvent()很明顯不能滿(mǎn)足我們的需求,經(jīng)多方打聽(tīng),找到了今天的主角GestureDetector,下面就對(duì)它進(jìn)行簡(jiǎn)單的學(xué)習(xí)。
構(gòu)造方法:
已過(guò)時(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主要用來(lái)執(zhí)行延時(shí)操作時(shí)使用,參數(shù)unused暫時(shí)沒(méi)有使用。
從構(gòu)成函數(shù)可以看出,當(dāng)我們需要?jiǎng)?chuàng)建一個(gè)GestureDetector對(duì)象時(shí),必須給它傳一個(gè)GestureDetector.onGestureListener對(duì)象,查看API之后,發(fā)現(xiàn)它是個(gè)接口(interface),創(chuàng)建GestureDetector.onGestureListener的對(duì)象時(shí),必須實(shí)現(xiàn)一下幾個(gè)方法:
1、onDown(MotionEvent e);
當(dāng)用戶(hù)按下時(shí)的回調(diào)。
2、onFling(MotionEvent e1,MontionEvent e2,float velocityX,float velocityY);
當(dāng)用戶(hù)快速拖動(dòng),并離開(kāi)屏幕時(shí),控件還在滑動(dòng)的回調(diào)。
3、onLongPress(MotionEvent e);
當(dāng)用戶(hù)長(zhǎng)按控件時(shí)的回調(diào)。
4、onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY);
當(dāng)用戶(hù)拖著控件(控件本身并沒(méi)有動(dòng))滑動(dòng)時(shí)的回調(diào)。
5、onShowPress(MotionEvent e);
當(dāng)用戶(hù)按下,還沒(méi)有執(zhí)行移動(dòng)或者抬起的動(dòng)作的回調(diào)。
6、onSingleTapUp(MotionEvent e);
用戶(hù)按下,立即抬起,沒(méi)有做其它動(dòng)作時(shí)的回調(diào)。
有了這個(gè)監(jiān)聽(tīng)之后,我們還可以給GestureDetector設(shè)置雙擊監(jiān)聽(tīng),使用的方法是:
mGestureDetector.setOnDoubleTapListener(GestureDetector.OnDoubleTapListener onDoubleListener);
參數(shù)是雙擊監(jiān)聽(tīng)的對(duì)象,GestureDetector.OnDoubleTapListener它也是一個(gè)接口(interface),創(chuàng)建它的對(duì)象時(shí),也必須實(shí)現(xiàn)以下幾個(gè)方法。
1、onDoubleTap(MotionEvent e);
當(dāng)用戶(hù)雙擊時(shí)回調(diào)。
2、onDoubleTapEvent(MotionEvent e);
雙擊間隔事件的回調(diào)。
3、onSingleTapConfirmed(MotionEvent e);
當(dāng)用戶(hù)單擊時(shí)回調(diào)。
上面的兩個(gè)監(jiān)聽(tīng),回調(diào)函數(shù)都是必須實(shí)現(xiàn),有時(shí)候我們不需要監(jiān)聽(tīng)所有的事件,只對(duì)自己感興趣的事件進(jìn)行監(jiān)聽(tīng),GestureDetector有個(gè)內(nèi)部類(lèi)幫我們實(shí)現(xiàn)此功能GestureDetector.SimpleOnGestureListener,該類(lèi)實(shí)現(xiàn)了GestureDetector.onGestureListener、GestureDetector.onDoubleTapListener、GestureDetector.onContextClickListener這三個(gè)接口,并實(shí)現(xiàn)了它們的方法,只不過(guò)是空實(shí)現(xiàn),在我們需要這三個(gè)接口的時(shí)候,我們可以創(chuàng)建GestureDetector.SimpleOnGestureListener對(duì)象,然后需要監(jiān)聽(tīng)哪個(gè)事件,我們就重寫(xiě)它的哪個(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); } }; 只有理論沒(méi)有實(shí)踐,怎么行呢?這里我也寫(xiě)了一個(gè)Demo,這個(gè)Demo是我從另一篇博客中抄的,文章的內(nèi)容也是參考他的博客寫(xiě)的,下面會(huì)把大神的那篇博客地址貼出來(lái)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注