国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android手勢(shì)識(shí)別器GestureDetector使用詳解

2019-10-23 18:35:34
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

以前只知道控件的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)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凉城县| 镇沅| 广河县| 原平市| 龙里县| 成都市| 雅江县| 宕昌县| 岳西县| 富顺县| 固原市| 绍兴市| 儋州市| 长海县| 马龙县| 井陉县| 博兴县| 潼南县| 洛隆县| 丽江市| 福建省| 长春市| 唐山市| 陇川县| 永年县| 西乌| 钟祥市| 兴隆县| 济宁市| 崇礼县| 石阡县| 津南区| 四川省| 景德镇市| 阜新| 郴州市| 翁牛特旗| 韩城市| 萨迦县| 廊坊市| 工布江达县|