2011年5月3日

Android 手勢判斷處理




Android 手勢判斷

簡介 

我們常常在瀏覽器、手機開發用看到一些手勢的應用,今天我學了一點簡單的例子,在這裡與大家分享。


android要使用手勢時,首先必須瞭解Gesture類,這個類是android提供給我們使用來判斷手劃圖案,文檔是這解釋的

A gesture is a hand-drawn shape on a touch screen. It can have one or multiple strokes. Each stroke is a sequence of timed points. A user-defined gesture can be recognized by a GestureLibrary and a built-in alphabet gesture can be recognized by a LetterRecognizer.

也就是說此類可以判斷我們在手機螢幕上移動路徑的行為判斷,好! 廢話不多說….

要怎麼開始使用這個呢,很簡單,以下幾個步驟解決它:


1.  定一個類MyGesture繼承Gesture
2.  實現onFling()方法
3.  改寫onFling()方法
4.  讓我們的Activity實現OnTouchListener
5.  在Activity宣告MyGesture
6.  讓MyGesture來處理onTouch()事件
7.  結束!


以下透過程式碼來學習
public class MyGesture extends Activity implements OnTouchListener { private GestureDetector mGestureDetector; public MyGesture() { mGestureDetector = new GestureDetector(new MySimpleGesture()); } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.tv); tv.setOnTouchListener(this); tv.setFocusable(true); tv.setClickable(true); tv.setLongClickable(true); } public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { Log.i("MyGesture", "MotionEvent.ACTION_UP"); } return mGestureDetector.onTouchEvent(event); } // SimpleOnGestureListener implements GestureDetector.OnDoubleTapListener, GestureDetector.OnGestureListener private class MySimpleGesture extends SimpleOnGestureListener { // 按兩下的第二下Touch down時觸發 public boolean onDoubleTap(MotionEvent e) { Log.i("MyGesture", "onDoubleTap"); return super.onDoubleTap(e); } // 按兩下的第二下Touch down和up都會觸發,可用e.getAction()區分 public boolean onDoubleTapEvent(MotionEvent e) { Log.i("MyGesture", "onDoubleTapEvent"); return super.onDoubleTapEvent(e); } // Touch down時觸發 public boolean onDown(MotionEvent e) { Log.i("MyGesture", "onDown"); return super.onDown(e); } // Touch了滑動一點距離後,up時觸發 // 參數解釋: // e1:第1個ACTION_DOWN MotionEvent // e2:最後一個ACTION_MOVE MotionEvent // velocityX:X軸上的移動速度,圖元/秒 // velocityY:Y軸上的移動速度,圖元/秒 // 觸發條件 : // X軸的座標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個圖元/秒 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200; if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { // Fling left Log.i("MyGesture", "Fling left"); Toast.makeText(getApplicationContext(), "Fling Left", Toast.LENGTH_SHORT).show(); } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) { // Fling right Log.i("MyGesture", "Fling right"); Toast.makeText(getApplicationContext(), "Fling Right", Toast.LENGTH_SHORT).show(); } return false; } // Touch了不移動一直Touch down時觸發 public void onLongPress(MotionEvent e) { Log.i("MyGesture", "onLongPress"); super.onLongPress(e); } // Touch了滑動時觸發 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.i("MyGesture", "onScroll"); return super.onScroll(e1, e2, distanceX, distanceY); } /* * Touch了還沒有滑動時觸發 * (1)onDown只要Touch Down一定立刻觸發 * (2)Touch Down後過一會沒有滑動先觸發onShowPress再觸發onLongPress * So: Touch Down後一直不滑動,onDown -> onShowPress -> onLongPress這個順序觸發。 */ public void onShowPress(MotionEvent e) { Log.i("MyGesture", "onShowPress"); super.onShowPress(e); } /* * 兩個函數都是在Touch Down後又沒有滑動(onScroll),又沒有長按(onLongPress),然後Touch Up時觸發 * 點擊一下非常快的(不滑動)Touch Up: onDown->onSingleTapUp->onSingleTapConfirmed * 點擊一下稍微慢點的(不滑動)Touch Up: onDown->onShowPress->onSingleTapUp->onSingleTapConfirmed */ public boolean onSingleTapConfirmed(MotionEvent e) { Log.i("MyGesture", "onSingleTapConfirmed"); return super.onSingleTapConfirmed(e); } public boolean onSingleTapUp(MotionEvent e) { Log.i("MyGesture", "onSingleTapUp"); return super.onSingleTapUp(e); } } }



沒有留言:

ShareThis