GestureDetector(手勢識別器)
### (1)手勢交互過程原理:
A.觸屏一剎那,觸發 MotionEvent事件;
B.上述事件被 OnTouchListenter 監聽,在 nTouch() 中獲得 MotionEvent對象;
C.GestureDetector 轉發MotionEvent對象至 OnGestureListenter;
D.OnGestureListenter 獲得該對象,根據該對象封裝的信息作出合適的反饋;
### (2)MotionEvent:
1)、用于封裝手勢等動作事件;
2)、內部封裝用于記錄橫軸和縱軸坐標的屬性X和Y;
GestureDetector:
1)、識別各種手勢;
OnGestureListenter:
1)、手勢交互的監聽接口,其提供多個抽象方法;
2)、根據GestureDetector的手勢識別結果調用相對應的方法;
### (3)GestureDetector工作原理:
1)、GestureDetector.OnGestureListener接口
onDown(MotionEvent e):——單擊
Notified when a tap occurs with the down MotionEvent that triggered it.
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):——滑動
Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent.
onLongPress(MotionEvent e):——長按
Notified when a long press occurs with the initial on down MotionEvent that trigged it.
onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):——滾動
Notified when a scroll occurs with the initial on down MotionEvent and the current move MotionEvent.
onShowPress(MotionEvent e):——短按
The user has performed a down MotionEvent and not performed a move or up yet.
onSingleTapUp(MotionEvent e):——抬起
Notified when a tap occurs with the up MotionEvent that triggered it.?
2)、GestureDetector.OnDoubleTapListener接口
onDoubleTap(MotionEvent e):——雙擊**
Notified when a double-tap occurs.
onDoubleTapEvent(MotionEvent e):——雙擊按下和抬起各觸發一次
Notified when an event within a double-tap gesture occurs, including the down, move, and up events.
onSingleTapConfirmed(MotionEvent e):——單擊確認(即很快的按下并抬起,但并不連續點擊第二下)
Notified when a single-tap occurs.
3)、GestureDetector.SimpleOnGestureListener類
其實我們并不需要處理上面所有手勢。問了方便起見,Android提供了另外一個類SimpleOnGestureListener。它實現了如上接口,我們只需要繼承SimpleOnGestureListener類,然后重載感興趣的手勢。?implements?[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)?[GestureDetector. OnDoubleTapListener](http://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener.html)[GestureDetector. OnContextClickListener](http://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener.html)。A convenience class to extend when you only want to listen for a subset of all the gestures. This implements all methods in ?the ?`[GestureDetector.OnGestureListener](http://developer.android.com/reference/android/view/GestureDetector.OnGestureListener.html)`, ?`[GestureDetector.OnDoubleTapListener](http://developer.android.com/reference/android/view/GestureDetector.OnDoubleTapListener.html)`, and`[GestureDetector.OnContextClickListener](http://developer.android.com/reference/android/view/GestureDetector.OnContextClickListener.html)`?but does nothing and return`false`?for all applicable methods.
~~~
public class MainActivity extends Activity {
private ImageView imageView;
private GestureDetector mygestureDetector;
// 繼承SimpleOnGestureListener類,然后重載感興趣的手勢。
class MyGestureListenter extends SimpleOnGestureListener {
@Override
// 重載滑動手勢
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if (e1.getX() - e2.getX() > 50) {
Toast.makeText(MainActivity.this, "從右往左滑動", Toast.LENGTH_SHORT)
.show();
} else if (e2.getX() - e1.getX() > 50) {
Toast.makeText(MainActivity.this, "從左往右滑動", Toast.LENGTH_SHORT)
.show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* GestureDetector工作原理
* 1、當接收到用戶觸摸消息時,將消息交給GestureDetector加工;
* 2、通過設置監聽器獲得GestureDetector處理后的手勢;
* 3、GestureDetector提供兩個監聽器:
* A.OnGestureListenter:處理單擊類消息;
* B.OnDoubleTapListenter:處理雙擊類消息;
*/
imageView = (ImageView) findViewById(R.id.img);
mygestureDetector = new GestureDetector(new MyGestureListenter());
// MotionEvent——》setOnTouchListener捕獲
// ——》onTouch中GestureDetector對象將監聽事件轉發給MyGestureListenter(extends SimpleOnGestureListener)
// ——》MyGestureListenter類中實現了要重載的手勢
imageView.setOnTouchListener(new OnTouchListener() {
@Override
// 可以捕獲觸摸屏幕發生的Event
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
mygestureDetector.onTouchEvent(event); // 轉發event事件,轉發給MyGestureListenter(extends SimpleOnGestureListener)
return false;
}
});
}
//消除沖突
/*@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if(mygestureDetector!=null){
if(mygestureDetector.onTouchEvent(ev)){
return true;
}
}
return super.dispatchTouchEvent(ev);
}*/
//必須重寫onTouchEvent方法,onFling才生效
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return mygestureDetector.onTouchEvent(event);
}
}
~~~
為了讓onFling才生效,必須重寫onTouchEvent方法!
- 前言
- Java內部類
- 從一個View向一個Activity跳轉
- Android 與 SQLite
- Android工程A依賴B,B依賴C
- Android重要控件概覽(上)
- Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED
- Android布局概覽
- 動態引用APK文件
- Android重要控件概覽(中)
- Android重要控件概覽(下)
- Gallery和ImageSwitcher
- Android之Toast
- Android之Dialog
- Android之Notification
- Android之Menu
- Android Menu中android:showAsAction屬性
- Android SharedPreferences存儲數據的使用方法
- Android手勢識別之GestureDetector
- 不同APP通過SharedPreferences傳遞數據(共享數據)
- 一個自定義的Topbar模板
- 關于Activity回收造成View選中不對應的問題
- Android之Fragment靜態加載