少年击剑更吹箫,剑气箫心一例消。这篇文章主要讲述android 判断左右滑动,上下滑动的GestureDetector简单手势检测相关的知识,希望能为你提供帮助。
直接加入监听GestureDetector放在需要判断滑动手势的地方:
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.util.Log; 4 import android.view.GestureDetector; 5 import android.view.GestureDetector.OnGestureListener; 6 import android.view.MotionEvent; 7 8 public class MainActivity extends Activity { 9protected static final float FLIP_DISTANCE = 50; 10GestureDetector mDetector; 11 12@Override 13protected void onCreate(Bundle savedInstanceState) { 14super.onCreate(savedInstanceState); 15setContentView(R.layout.activity_main); 16 17mDetector = new GestureDetector(this, new OnGestureListener() { 18 19@Override 20public boolean onSingleTapUp(MotionEvent e) { 21// TODO Auto-generated method stub 22return false; 23} 24 25@Override 26public void onShowPress(MotionEvent e) { 27// TODO Auto-generated method stub 28 29} 30 31@Override 32public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 33// TODO Auto-generated method stub 34return false; 35} 36 37@Override 38public void onLongPress(MotionEvent e) { 39// TODO Auto-generated method stub 40 41} 42 43/** 44* 45* e1 The first down motion event that started the fling. e2 The 46* move motion event that triggered the current onFling. 47*/ 48@Override 49public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 50if (e1.getX() - e2.getX() > FLIP_DISTANCE) { 51Log.i("TAG", "< --- left, left, go go go"); 52return true; 53} 54if (e2.getX() - e1.getX() > FLIP_DISTANCE) { 55Log.i("TAG", "right, right, go go go ---> "); //忽然觉得这个log好智障... 56return true; 57} 58if (e1.getY() - e2.getY() > FLIP_DISTANCE) { 59Log.i("TAG", "向上滑..."); 60return true; 61} 62if (e2.getY() - e1.getY() > FLIP_DISTANCE) { 63Log.i("TAG", "向下滑..."); 64return true; 65} 66 67Log.d("TAG", e2.getX() + " " + e2.getY()); 68 69return false; 70} 71 72@Override 73public boolean onDown(MotionEvent e) { 74// TODO Auto-generated method stub 75return false; 76} 77}); 78} 79 80@Override 81public boolean onTouchEvent(MotionEvent event) { 82return mDetector.onTouchEvent(event); 83} 84 }
【android 判断左右滑动,上下滑动的GestureDetector简单手势检测】
推荐阅读
- com/android/dx/command/dexer/Main : Unsupported major.minor version 52.0
- C .Thunder——爱阅app(测评人(方铭))
- Apple Tree POJ - 2486
- B.欢迎来怼——博客园安卓app(测评人(阚博文))
- Mapper.xml中对象的关联
- 安卓常见英文缩写的全拼
- SpringMVC系列: 注解@RequestMapping@PathVariable
- Android Studio 3.0 |Kotlin 测试
- Linux/Android——usb触摸屏驱动 - usbtouchscreen