Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有时,手势检测器会跳过飞行_Android_Gesture_Onfling - Fatal编程技术网

Android 有时,手势检测器会跳过飞行

Android 有时,手势检测器会跳过飞行,android,gesture,onfling,Android,Gesture,Onfling,我有一个使用GestureDetector的应用程序,我正在替换onDown和onFling(又名onUp)上的一些图像。但是,在某些情况下,不会调用onFling,结果也不太理想:) 您是否遇到过此类问题,是否找到了修复/解决方法(除了使用超时)? 下面是一个小代码: final GestureDetector gdt1 = new GestureDetector(getApplicationContext(), new MyGestureDetector(mGestDetector,

我有一个使用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;
        }
    }
对于检测“向上”和“向下”事件,我建议使用如下简单方法:

@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
   }

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

 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);
 }

(来源:)

发布您迄今为止所使用的代码。使用手势侦听器查看和拖动的视图是同一视图吗?如果是,只需在可拖动视图上创建视图,并在新覆盖上设置手势侦听器view@ErvinMartirosyan实际上,我使用onFling()来检测onUp()事件,因为根据onFling()命令官方文档
在初始的on down MotionEvent和匹配的up MotionEvent发生投掷事件时会通知投掷事件。
我的案例不需要拖动,只需要向下和向上事件。我想这将是最好的方法,但这仍然不能回答我关于onFling没有被呼叫的问题。请查看我在回答中添加的其他信息。onFling并不一定会在每个UP事件中被调用。尽管我不能100%确定onFling不会被调用,因为速度太低(我需要查看(调试)它才能相信;),但这是我的问题最合理的原因。