生活随笔
收集整理的這篇文章主要介紹了
Android GestureDetector方法详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原帖地址:http://tonysun3544.iteye.com/blog/1787684
?為了加強點擊、拖動響應事件,Android提供了GestureDetector手勢識別類。通過GestureDetector.OnGestureListener來獲取當前被觸發的操作手勢(Single Tap Up、Show Press、Long Press、Scroll、Down、Fling),具體包括以下幾種:
?
Java代碼??
boolean ??onDoubleTap(MotionEvent?e) ??
? 解釋:雙擊的第二下down時觸發?
Java代碼??
boolean ??onDoubleTapEvent(MotionEvent?e)???
?解釋:雙擊的第二下down和up都會觸發,可用e.getAction()區分。?
Java代碼??
boolean ??onDown(MotionEvent?e)???
解釋:down時觸發?
Java代碼??
boolean ??onFling(MotionEvent?e1,?MotionEvent?e2,?float ?velocityX,?float ?velocityY)???
?解釋:Touch了滑動一點距離后,up時觸發。?
Java代碼??
void ??onLongPress(MotionEvent?e)???
?解釋:Touch了不移動一直down時觸發?
Java代碼??
boolean ??onScroll(MotionEvent?e1,?MotionEvent?e2,?float ?distanceX,?float ?distanceY)???
?解釋:Touch了滑動時觸發。?
Java代碼??
void ??onShowPress(MotionEvent?e)???
?解釋:Touch了還沒有滑動時觸發?
?
與onDown,onLongPress比較?onDown只要Touch down一定立刻觸發。? 而Touch down后過一會沒有滑動先觸發onShowPress再是onLongPress。 所以Touch down后一直不滑動,onDown->onShowPress->onLongPress這個順序觸發。
boolean? onSingleTapConfirmed(MotionEvent e)? boolean? onSingleTapUp(MotionEvent e)? 解釋:上面這兩個函數都是在touch down后又沒有滑動(onScroll),又沒有長按(onLongPress),然后Touchup時觸發。
點擊一下非常快的(不滑動)Touchup:? onDown->onSingleTapUp->onSingleTapConfirmed? 點擊一下稍微慢點的(不滑動)Touchup:? onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed
創建手勢監聽對象:
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()?{?? ?????????? ????}?? ?? ????? ? ? ? ?? ????@Override ?? ????public ?boolean ?onDoubleTap(MotionEvent?e)?{?? ????????Log.i(TAG,?"onDoubleTap?:?" ?+?e.getAction());?? ????????return ?super .onDoubleTap(e);?? ????}?? ?? ????? ? ? ? ?? ????@Override ?? ????public ?boolean ?onDoubleTapEvent(MotionEvent?e)?{?? ????????Log.i(TAG,?"onDoubleTapEvent?:?" ?+?e.getAction());?? ????????return ?super .onDoubleTapEvent(e);?? ????}?? ?? ????? ? ? ? ?? ????@Override ?? ????public ?boolean ?onDown(MotionEvent?e)?{?? ????????Log.i(TAG,?"onDown?:?" ?+?e.getAction());?? ????????return ?super .onDown(e);?? ????}?? ?? ????? ? ? ? ? ? ? ?? ????@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);?? ????}?? ?? ????? ? ? ?? ????@Override ?? ????public ?void ?onLongPress(MotionEvent?e)?{?? ????????Log.i(TAG,?"onLongPress?:?" ?+?e.getAction());?? ????????super .onLongPress(e);?? ????}?? ?? ????? ? ? ? ? ? ? ?? ????@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);?? ????}?? ?? ????? ? ? ?? ????@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)方法中設置手勢:
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中設置手勢:
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中設置手勢有兩點需要注意:
1:View必須設置longClickable為true,否則手勢識別無法正確工作,只會返回Down, Show, Long三種手勢。
2:必須在View的onTouchListener中調用手勢識別,而不能像Activity一樣重載onTouchEvent,否則同樣手勢識別無法正確工作。
?
與50位技術專家面對面 20年技術見證,附贈技術全景圖
總結
以上是生活随笔 為你收集整理的Android GestureDetector方法详解 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。