Android Gesture 手勢識別,androidgesture
手勢識別
實現OnGestureListener, OnTouchListener介面
class MyView extend LinearLayout implements OnGestureListener, OnTouchListener { public MyView(Context context) { this.setOnTouchListener(this);// 將本類綁定觸屏監聽器 GestureDetector gd = new GestureDetector(this); } //先經過gd.onTouchEvent(event)事件判斷, //執行手勢則不執行父類onTouch(View v, MotionEvent event)事件 @Override public boolean onTouch(View v, MotionEvent event) { return gd.onTouchEvent(event); } // --------------以下是使用OnGestureListener手勢監聽的時候重寫的函數--------- /** * @以下方法中的參數解釋: * @e1:第1個是 ACTION_DOWN MotionEvent 按下的動作 * @e2:後一個是ACTION_UP MotionEvent 抬起的動作(這裡要看下備忘5的解釋) * @velocityX:X軸上的移動速度,像素/秒 * @velocityY:Y軸上的移動速度,像素/秒 */ @Override public boolean onDown(MotionEvent e) { // ACTION_DOWN return false; } @Override // ACTION_DOWN 、短按不移動 public void onShowPress(MotionEvent e) { } @Override // ACTION_DOWN 、長按不滑動 public void onLongPress(MotionEvent e) { } @Override // ACTION_DOWN 、慢滑動 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override // ACTION_DOWN 、快滑動、 ACTION_UP public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { v_str.add("onFling"); //-------e1是MotionEvent.ACTION_DOWN, e2是MotionEvent.ACTION_UP---------- // if(e1.getAction()==MotionEvent.ACTION_MOVE){ // v_str.add("onFling"); // }else if(e1.getAction()==MotionEvent.ACTION_DOWN){ // v_str.add("onFling"); // }else if(e1.getAction()==MotionEvent.ACTION_UP){ // v_str.add("onFling"); // } // if(e2.getAction()==MotionEvent.ACTION_MOVE){ // v_str.add("onFling"); // }else if(e2.getAction()==MotionEvent.ACTION_DOWN){ // v_str.add("onFling"); // }else if(e2.getAction()==MotionEvent.ACTION_UP){ // v_str.add("onFling"); // } return false; } @Override // 短按ACTION_DOWN、ACTION_UP public boolean onSingleTapUp(MotionEvent e) { return false; }}
這隻是一個簡單的例子,Android Simples中有個完整的例子:Gestures Builder。