1

我有一个使用 GestureDetector 的应用程序,我正在替换一些图像 onDown 和 onFling(又名 onUp)。但是在某些情况下,不会调用 onFling 并且结果并不漂亮:)

您是否遇到过此类问题,是否找到了修复/解决方法(除了使用超时)?
这是一个小代码:

    final GestureDetector gdt1 = new GestureDetector(getApplicationContext(), new MyGestureDetector(mGestDetector, R.id.weatherFrame1));
    FrameLayout weatherFrame1 = (FrameLayout)findViewById(R.id.weatherFrame1);
    if (weatherFrame1 != null)
    {
        weatherFrame1.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(final View view, final MotionEvent event)
            {
                gdt1.onTouchEvent(event);
                return true;
            }
        });
    }

这是 MyGestureDetector.java 的一部分

    public class MyGestureDetector implements GestureDetector.OnGestureListener{
    {
    ...
        @Override
        public boolean onDown(MotionEvent e)
        {
            int index = e.getActionIndex();
            int pointerId = e.getPointerId(index);

            if (startingPointerId == -1)
            {
                Log.i("MyGestureDetector", "Pointer is " + pointerId);

                if (pointerId == 0)
                {
                    startingPosX = e.getX(pointerId);
                    startingPosY = e.getY(pointerId);
                    startingPointerId = pointerId;
                    if (null != mGestureListener)
                    {
                        mGestureListener.onDown(mGestureOrigin);
                    }
                }
            }
            return true;
        }
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
            startingPointerId = -1;
            if (null != mGestureListener)
            {
                mGestureListener.onUp(mGestureOrigin);
            }
            return true;
        }
    }
4

1 回答 1

3

为了检测“up”和“down”事件,我会推荐一些简单的东西:

@Override
public boolean onTouch(final View view, final MotionEvent event) {

   if (event.getAction() == MotionEvent.ACTION_UP) {
      // handle up event
   } else if (event.getAction() == MotionEvent.ACTION_DOWN) {
      // handle down event
   }

}

编辑:最可能的原因onFling不是每次都被调用是因为它不仅仅是一种处理 UP 事件的方法。查看Android源代码,onFling仅在速度达到被视为“甩动”所需的最小速度时才调用。一探究竟:

 case MotionEvent.ACTION_UP:
        mStillDown = false;
        MotionEvent currentUpEvent = MotionEvent.obtain(ev);
        if (mIsDoubleTapping) {
            // Finally, give the up event of the double-tap
            handled |= mDoubleTapListener.onDoubleTapEvent(ev);
        } else if (mInLongPress) {
            mHandler.removeMessages(TAP);
            mInLongPress = false;
        } else if (mAlwaysInTapRegion) {
            handled = mListener.onSingleTapUp(ev);
        } else {

            // A fling must travel the minimum tap distance
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumFlingVelocity);
            final float velocityY = velocityTracker.getYVelocity();
            final float velocityX = velocityTracker.getXVelocity();

            if ((Math.abs(velocityY) > mMinimumFlingVelocity)
                    || (Math.abs(velocityX) > mMinimumFlingVelocity)){
                handled = mListener.onFling(mCurrentDownEvent, ev, velocityX, velocityY);
            }
        }

最为显着地:

 if ((Math.abs(velocityY) > mMinimumFlingVelocity)
          || (Math.abs(velocityX) > mMinimumFlingVelocity)){
      handled = mListener.onFling(mCurrentDownEvent, ev, velocityX, velocityY);
 }

(来源: http: //grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/view/GestureDetector.java

于 2014-06-24T02:49:21.053 回答