【问题标题】:Sometimes GestureDetector is skipping onFling有时 GestureDetector 会跳过 onFling
【发布时间】:2014-05-27 14:44:51
【问题描述】:

我有一个使用 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;
        }
    }

【问题讨论】:

  • 贴出你目前工作过的代码。
  • 带有手势监听器的视图和拖动的视图是同一个视图吗?如果是,只需在可拖动视图上创建视图并在新覆盖视图上设置手势监听器
  • @ErvinMartirosyan 实际上我正在使用 onFling() 来检测 onUp() 事件,因为根据 onFling() 官方文档Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. 我的情况不需要拖动,只有上下事件.

标签: android gesture onfling


【解决方案1】:

为了检测“向上”和“向下”事件,我建议使用如下简单的方法:

@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

【讨论】:

  • 我想这将是最好的方法,但这仍然不能解决我没有调用 onFling 的问题。
  • 查看我添加到答案中的附加信息。不一定会为每个 UP 事件调用 onFling。
  • 尽管我不能 100% 确定由于速度太低而不会调用 onFling(我需要查看(调试)它才能相信它;)),但这是最合乎逻辑的原因对于我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多