android VelocityTracker和 GestureDetector滑动监听工具类
2017-04-02 19:57
465 查看
这次将两个滑动监听工具类,是方便我们判断当前滑动状态的。
VelocityTracker:速度监听,能够监听x轴和y轴上的分量速度。
GestureDetector:能够检测用户的单击,滑动,双击等行为。
使用例子如下:
使用方法都很简单,mGestureDetector.setIsLongpressEnabled(false)这个命令慎用,因为它会导致GestureDetector无法判断长按状态。
还有就是 velocityTracker.clear();
velocityTracker.recycle();,记得用完VelocityTracker,要进行回收资源。
再见
VelocityTracker:速度监听,能够监听x轴和y轴上的分量速度。
GestureDetector:能够检测用户的单击,滑动,双击等行为。
使用例子如下:
public class MainActivity extends AppCompatActivity { private MyView view; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); view = new MyView(this); setContentView(view); } } public class MyView extends View implements GestureDetector.OnGestureListener { private int x,y,left,right,top,bottom,rawX,rawY,touchslop; private int startx=0,starty=0,endx=0,endy=0; private String state=""; private GestureDetector mGestureDetector ; public MyView(Context context) { super(context); setFocusable(true); touchslop= ViewConfiguration.get(context).getScaledTouchSlop(); mGestureDetector = new GestureDetector(this); //解决长按后屏幕无法拖动,但是却无法判断长按的情况 mGestureDetector.setIsLongpressEnabled(false); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.BLACK); paint.setTextSize(40); canvas.drawText(" x:" + x + " y:" + y, 50, 100, paint); super.onDraw(canvas); } @Override public boolean onKeyDown(int keyCode,KeyEvent event) { return super.onKeyDown(keyCode, event); } @Override public boolean onKeyUp(int keyCode,KeyEvent event) { return super.onKeyUp(keyCode, event); } @Override public boolean onTouchEvent(MotionEvent event) { VelocityTracker velocityTracker = VelocityTracker.obtain(); velocityTracker.addMovement(event); velocityTracker.computeCurrentVelocity(1000); x=(int)velocityTracker.getXVelocity(); y=(int)velocityTracker.getYVelocity(); invalidate(); velocityTracker.clear(); velocityTracker.recycle(); mGestureDetector.onTouchEvent(event); return true; } @Override public boolean onDown(MotionEvent e) { Log.v("zzw","onDown"); return false; } @Override public void onShowPress(MotionEvent e) { Log.v("zzw","onShowPress"); } @Override public boolean onSingleTapUp(MotionEvent e) { Log.v("zzw","onSingleTapUp"); return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { Log.v("zzw","onScroll"); return false; } @Override public void onLongPress(MotionEvent e) { Log.v("zzw","onLongPress"); } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.v("zzw","onFling"); return false; } }
使用方法都很简单,mGestureDetector.setIsLongpressEnabled(false)这个命令慎用,因为它会导致GestureDetector无法判断长按状态。
还有就是 velocityTracker.clear();
velocityTracker.recycle();,记得用完VelocityTracker,要进行回收资源。
再见
相关文章推荐
- android velocityTracker滑动速度检测器简单使用
- Android VelocityTracker获取滑动速度
- android 手势滑动(ViewGroup, Scroller, VelocityTracker, ViewConfiguration)
- Android VelocityTracker与GestureDetector使用简介
- Android View深入解析(一)基础知识VelocityTracker,GestureDetector,Scroller
- Android VelocityTracker简介
- Android 内存分配跟踪工具(Allocation tracker)
- Android-短信监听工具
- Android-电话监听工具
- android短信监听工具
- 【Android】滑动屏幕效果GestureDetector、OnGestureListener、ViewFlipper
- android velocityTracker 简介
- Android滑动屏幕效果GestureDetector、OnGestureListener、ViewFlipper
- Android手势滑动GestureDetector和OnGestureListener(二)
- android短信监听工具
- Android VelocityTracker简介
- 【Android】滑动屏幕效果GestureDetector、OnGestureListener、ViewFlipper
- 如何通过GestureDetector实现屏幕事件监听(滑动切换Layout) (转)
- android ScrollLayout VelocityTracker
- 【Android】滑动屏幕效果GestureDetector、OnGestureListener、ViewFlipper