Android程序如何實現觸屏移動
Java代碼 public class DayCell extends View implements GestureDetector.OnGestureListener { GestureDetector mGestureDetector; .... p* s0 O! f) p+ t k2 Q9 U public DayCell(Context context) { super(context); - I' ^4 A$ `$ y setFocusable(true); mGestureDetector = new GestureDetector(DayCell.this); //setLongClickable(true); } ...... 6 {% C: n4 g: y+ M' G @Override 7 k/ J( }# e. [5 `( _ public boolean onTouchEvent(MotionEvent event) { return mGestureDetector.onTouchEvent(event); ( Y% U) |' `9 O - Y } @Override 8 O2 m: |6 E5 f - l public boolean onDown(MotionEvent e) { return true;//必定的返回true,若是不是好像沒有反應。 . \ 5 p$ G7 C7 r/ Q/ t& ? } @Override # B! i) r" E1 b+ W2 m+ ~! q public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 左 m4 M9 R9 p( Z- B, A if (e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 200) {//移動的距 離和速度 5 _( b. t6 `2 ^+ }5 H- w! o ... } // 右 else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 200) { ... ( m. Q7 Q# _. } D; z- H$ q }//上 " b! a5 ?9 f# r+ ?% R else if (e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 200) { & L, W5 E' p2 [ ... # Y1 i! x" e8 r X" K; z } // 下 else if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 200) { ... } 5 P9 \ ! _5 C. O return true; % {& ]: V- W; o* H7 C; ?* e } 浪曦網-國內頂級IT 視頻網絡編程教育平臺 Www.LangSin.Com 浪曦網-國內頂級IT 視頻網絡編程教育平臺 Www.LangSin.Com @Override public void onLongPress(MotionEvent e) { 8 f4 R. A' E: I - F4 Z } @Override 5 h7 U9 \ " m6 S( I L7 I/ A) R" [ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 6 A ?0 c9 B) b& I8 k // TODO Auto-generated method stub return false; } @Override - N% c1 c- V7 |% h public void onShowPress(MotionEvent e) { ( g/ j* |) m! ]6 ], {6 r* l } @Override 9 k% d0 X* ~, J4 q public boolean onSingleTapUp(MotionEvent e) { - @6 t/ ~3 M( c .. return true; } }