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

「アップ」および「ダウン」イベントを検出するには、次のような単純なものをお勧めします。

@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 に答える