日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android GestureDetector方法详解

發(fā)布時(shí)間:2024/4/17 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android GestureDetector方法详解 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原帖地址:http://tonysun3544.iteye.com/blog/1787684


?為了加強(qiáng)點(diǎn)擊、拖動響應(yīng)事件,Android提供了GestureDetector手勢識別類。通過GestureDetector.OnGestureListener來獲取當(dāng)前被觸發(fā)的操作手勢(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具體包括以下幾種:

?

Java代碼??
  • boolean??onDoubleTap(MotionEvent?e) ??
  • ?解釋:雙擊的第二下down時(shí)觸發(fā)?

    Java代碼??
  • boolean??onDoubleTapEvent(MotionEvent?e)???
  • ?解釋:雙擊的第二下down和up都會觸發(fā),可用e.getAction()區(qū)分。?

    Java代碼??
  • boolean??onDown(MotionEvent?e)???
  • 解釋:down時(shí)觸發(fā)?

    Java代碼??
  • boolean??onFling(MotionEvent?e1,?MotionEvent?e2,?float?velocityX,?float?velocityY)???
  • ?解釋:Touch了滑動一點(diǎn)距離后,up時(shí)觸發(fā)。?

    Java代碼??
  • void??onLongPress(MotionEvent?e)???
  • ?解釋:Touch了不移動一直down時(shí)觸發(fā)?

    Java代碼??
  • boolean??onScroll(MotionEvent?e1,?MotionEvent?e2,?float?distanceX,?float?distanceY)???
  • ?解釋:Touch了滑動時(shí)觸發(fā)。?

    Java代碼??
  • void??onShowPress(MotionEvent?e)???
  • ?解釋:Touch了還沒有滑動時(shí)觸發(fā)?

    ?

    與onDown,onLongPress比較?onDown只要Touch down一定立刻觸發(fā)。?
    而Touch down后過一會沒有滑動先觸發(fā)onShowPress再是onLongPress。
    所以Touch down后一直不滑動,onDown->onShowPress->onLongPress這個(gè)順序觸發(fā)。


    boolean? onSingleTapConfirmed(MotionEvent e)?
    boolean? onSingleTapUp(MotionEvent e)?
    解釋:上面這兩個(gè)函數(shù)都是在touch down后又沒有滑動(onScroll),又沒有長按(onLongPress),然后Touchup時(shí)觸發(fā)。

    點(diǎn)擊一下非常快的(不滑動)Touchup:?
    onDown->onSingleTapUp->onSingleTapConfirmed?
    點(diǎn)擊一下稍微慢點(diǎn)的(不滑動)Touchup:?
    onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed


    創(chuàng)建手勢監(jiān)聽對象:

    Java代碼??
  • package?com.sun.gesturetest;??
  • ??
  • import?android.util.Log;??
  • import?android.view.GestureDetector.SimpleOnGestureListener;??
  • import?android.view.MotionEvent;??
  • ??
  • public?class?MyGestureListener?extends?SimpleOnGestureListener?{??
  • ??????
  • ????private?static?final?String?TAG?=?"MyGestureListener";??
  • ??
  • ????public?MyGestureListener()?{??
  • ??????????
  • ????}??
  • ??
  • ????/**?
  • ?????*?雙擊的第二下Touch?down時(shí)觸發(fā)??
  • ?????*?@param?e?
  • ?????*?@return?
  • ?????*/??
  • ????@Override??
  • ????public?boolean?onDoubleTap(MotionEvent?e)?{??
  • ????????Log.i(TAG,?"onDoubleTap?:?"?+?e.getAction());??
  • ????????return?super.onDoubleTap(e);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?雙擊的第二下?down和up都會觸發(fā),可用e.getAction()區(qū)分。?
  • ?????*?@param?e?
  • ?????*?@return?
  • ?????*/??
  • ????@Override??
  • ????public?boolean?onDoubleTapEvent(MotionEvent?e)?{??
  • ????????Log.i(TAG,?"onDoubleTapEvent?:?"?+?e.getAction());??
  • ????????return?super.onDoubleTapEvent(e);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?down時(shí)觸發(fā)??
  • ?????*?@param?e?
  • ?????*?@return?
  • ?????*/??
  • ????@Override??
  • ????public?boolean?onDown(MotionEvent?e)?{??
  • ????????Log.i(TAG,?"onDown?:?"?+?e.getAction());??
  • ????????return?super.onDown(e);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Touch了滑動一點(diǎn)距離后,up時(shí)觸發(fā)。?
  • ?????*?@param?e1?
  • ?????*?@param?e2?
  • ?????*?@param?velocityX?
  • ?????*?@param?velocityY?
  • ?????*?@return?
  • ?????*/??
  • ????@Override??
  • ????public?boolean?onFling(MotionEvent?e1,?MotionEvent?e2,?float?velocityX,??
  • ????????????float?velocityY)?{??
  • ????????Log.i(TAG,?"onFling?e1?:?"?+?e1.getAction()?+?",?e2?:?"?+?e2.getAction()?+?",?distanceX?:?"?+?velocityX?+?",?distanceY?:?"?+?velocityY);??
  • ????????return?super.onFling(e1,?e2,?velocityX,?velocityY);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Touch了不移動一直?down時(shí)觸發(fā)??
  • ?????*?@param?e?
  • ?????*/??
  • ????@Override??
  • ????public?void?onLongPress(MotionEvent?e)?{??
  • ????????Log.i(TAG,?"onLongPress?:?"?+?e.getAction());??
  • ????????super.onLongPress(e);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Touch了滑動時(shí)觸發(fā)。??
  • ?????*?@param?e1?
  • ?????*?@param?e2?
  • ?????*?@param?distanceX?
  • ?????*?@param?distanceY?
  • ?????*?@return?
  • ?????*/??
  • ????@Override??
  • ????public?boolean?onScroll(MotionEvent?e1,?MotionEvent?e2,?float?distanceX,??
  • ????????????float?distanceY)?{??
  • ????????Log.i(TAG,?"onScroll?e1?:?"?+?e1.getAction()?+?",?e2?:?"?+?e2.getAction()?+?",?distanceX?:?"?+?distanceX?+?",?distanceY?:?"?+?distanceY);??
  • ????????return?super.onScroll(e1,?e2,?distanceX,?distanceY);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?Touch了還沒有滑動時(shí)觸發(fā)??
  • ?????*?@param?e?
  • ?????*/??
  • ????@Override??
  • ????public?void?onShowPress(MotionEvent?e)?{??
  • ????????Log.i(TAG,?"onShowPress?:?"?+?e.getAction());??
  • ????????super.onShowPress(e);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onSingleTapConfirmed(MotionEvent?e)?{??
  • ????????Log.i(TAG,?"onSingleTapConfirmed?:?"?+?e.getAction());??
  • ????????return?super.onSingleTapConfirmed(e);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onSingleTapUp(MotionEvent?e)?{??
  • ????????Log.i(TAG,?"onSingleTapUp?:?"?+?e.getAction());??
  • ????????return?super.onSingleTapUp(e);??
  • ????}??
  • }??
  • ?

    在Activity中onTouchEvent(MotionEvent event)方法中設(shè)置手勢:

    Java代碼??
  • package?com.sun.gesturetest;??
  • ??
  • import?android.os.Bundle;??
  • import?android.app.Activity;??
  • import?android.view.GestureDetector;??
  • import?android.view.Menu;??
  • import?android.view.MotionEvent;??
  • ??
  • public?class?MainActivity?extends?Activity?{??
  • ??????
  • ????private?GestureDetector?mDetector;??
  • ??
  • ????@Override??
  • ????public?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.activity_main);??
  • ????????mDetector?=?new?GestureDetector(this,?new?MyGestureListener());??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onTouchEvent(MotionEvent?event)?{??
  • ????????return?mDetector.onTouchEvent(event);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?boolean?onCreateOptionsMenu(Menu?menu)?{??
  • ????????getMenuInflater().inflate(R.menu.activity_main,?menu);??
  • ????????return?true;??
  • ????}??
  • }??
  • ?或者在自定義View中設(shè)置手勢:

    Java代碼??
  • package?com.sun.gesturetest;??
  • ??
  • import?android.content.Context;??
  • import?android.util.AttributeSet;??
  • import?android.view.GestureDetector;??
  • import?android.view.MotionEvent;??
  • import?android.view.View;??
  • ??
  • public?class?GestureView?extends?View?{??
  • ??????
  • ????private?GestureDetector?mDetector;??
  • ??
  • ????public?GestureView(Context?context,?AttributeSet?set)?{??
  • ????????super(context,?set);??
  • ??????????
  • ????????mDetector?=?new?GestureDetector(context,?new?MyGestureListener());??
  • ????????setLongClickable(true);??
  • ????????this.setOnTouchListener(new?OnTouchListener()?{??
  • ??????????????
  • ????????????@Override??
  • ????????????public?boolean?onTouch(View?v,?MotionEvent?event)?{??
  • ????????????????return?mDetector.onTouchEvent(event);??
  • ????????????}??
  • ????????});??
  • ????}??
  • }??
  • ?在View中設(shè)置手勢有兩點(diǎn)需要注意:

    1:View必須設(shè)置longClickable為true,否則手勢識別無法正確工作,只會返回Down, Show, Long三種手勢。

    2:必須在View的onTouchListener中調(diào)用手勢識別,而不能像Activity一樣重載onTouchEvent,否則同樣手勢識別無法正確工作。

    ?


    與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

    總結(jié)

    以上是生活随笔為你收集整理的Android GestureDetector方法详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。