android – 单眼触摸检测在Ontouch方法的视图

前端之家收集整理的这篇文章主要介绍了android – 单眼触摸检测在Ontouch方法的视图前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在自定义视图的onuch方法中进行单边触摸检测.我尝试在ACTION-DOWN和ACTION-UP中获取x和y值,而在ACTION-UP中我给出的条件是,如果ACTIONDOWN和ACTION-UP中的X和Y的值相等,则将其作为单次点击.

我的代码如下

  1. @Override
  2. public boolean onTouchEvent(MotionEvent ev) {
  3. if (!mSupportsZoom && !mSupportsPan) return false;
  4.  
  5. mScaleDetector.onTouchEvent(ev);
  6.  
  7. final int action = ev.getAction();
  8. switch (action & MotionEvent.ACTION_MASK) {
  9. case MotionEvent.ACTION_DOWN: {
  10. final float x = ev.getX();
  11. final float y = ev.getY();
  12.  
  13. mLastTouchX = x; //here i get x and y values in action down
  14. mLastTouchY = y;
  15. mActivePointerId = ev.getPointerId(0);
  16.  
  17. break;
  18. }
  19.  
  20. case MotionEvent.ACTION_MOVE: {
  21. final int pointerIndex = ev.findPointerIndex(mActivePointerId);
  22. final float x = ev.getX(pointerIndex);
  23. final float y = ev.getY(pointerIndex);
  24.  
  25. if (mSupportsPan && !mScaleDetector.isInProgress()) {
  26. final float dx = x - mLastTouchX;
  27. final float dy = y - mLastTouchY;
  28.  
  29. mPosX += dx;
  30. mPosY += dy;
  31. //mFocusX = mPosX;
  32. //mFocusY = mPosY;
  33.  
  34. invalidate();
  35. }
  36.  
  37. mLastTouchX = x;
  38. mLastTouchY = y;
  39.  
  40. break;
  41. }
  42.  
  43. case MotionEvent.ACTION_UP: {
  44.  
  45. final float x = ev.getX();
  46. final float y = ev.getY();
  47.  
  48. touchupX=x; //here is get x and y values at action up
  49. touchupY=y;
  50.  
  51. if(mLastTouchX == touchupX && mLastTouchY == touchupY){ //my condition if both the x and y values are same .
  52.  
  53. PinchZoomPanActivity2.tapped1(this.getContext(),100); //my method if the singletap is detected
  54.  
  55. }
  56. else{
  57.  
  58. }
  59.  
  60. mActivePointerId = INVALID_POINTER_ID;
  61.  
  62. break;
  63. }
  64.  
  65. case MotionEvent.ACTION_CANCEL: {
  66. mActivePointerId = INVALID_POINTER_ID;
  67. break;
  68. }
  69.  
  70. case MotionEvent.ACTION_POINTER_UP: {
  71. final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
  72. >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  73. final int pointerId = ev.getPointerId(pointerIndex);
  74. if (pointerId == mActivePointerId) {
  75.  
  76. final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  77. mLastTouchX = ev.getX(newPointerIndex);
  78. mLastTouchY = ev.getY(newPointerIndex);
  79. mActivePointerId = ev.getPointerId(newPointerIndex);
  80. }
  81. break;
  82. }
  83. }
  84.  
  85. return true;
  86. }

但我不能完成.我的意思是在每一个动作,我的方法被称为.即使动作和动作的x和y值都不相同.我想我也需要把一些范围放在单边,我们用手指触摸屏幕.有人可以给我一些建议吗

解决方法

最近我也遇到了同样的问题,最终不得不实施去抖动来使其工作.这不是理想的,但它是非常可靠的,直到我能找到更好的东西.

View.onClickListener对我来说更可靠,但不幸的是,我需要OnTouchListener的MotionEvent.

编辑:删除导致其在此失败的多余代码

  1. class CustomView extends View {
  2.  
  3. private static long mDeBounce = 0;
  4.  
  5. static OnTouchListener listenerMotionEvent = new OnTouchListener() {
  6. @Override
  7. public boolean onTouch(View view,MotionEvent motionEvent) {
  8. if ( Math.abs(mDeBounce - motionEvent.getEventTime()) < 250) {
  9. //Ignore if it's been less then 250ms since
  10. //the item was last clicked
  11. return true;
  12. }
  13.  
  14. int intCurrentY = Math.round(motionEvent.getY());
  15. int intCurrentX = Math.round(motionEvent.getX());
  16. int intStartY = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalY(0)) : intCurrentY;
  17. int intStartX = motionEvent.getHistorySize() > 0 ? Math.round(motionEvent.getHistoricalX(0)) : intCurrentX;
  18.  
  19. if ( (motionEvent.getAction() == MotionEvent.ACTION_UP) && (Math.abs(intCurrentX - intStartX) < 3) && (Math.abs(intCurrentY - intStartY) < 3) ) {
  20. if ( mDeBounce > motionEvent.getDownTime() ) {
  21. //Still got occasional duplicates without this
  22. return true;
  23. }
  24.  
  25. //Handle the click
  26.  
  27. mDeBounce = motionEvent.getEventTime();
  28. return true;
  29. }
  30. return false;
  31. }
  32. };
  33. }

猜你在找的Android相关文章