gpt4 book ai didi

android - 有时 GestureDetector 会跳过 onFling

转载 作者:行者123 更新时间:2023-11-29 17:49:39 38 4
gpt4 key购买 nike

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

}

编辑:onFling 不是每次都被调用的最可能原因是因为它不仅仅是一种处理 UP 事件的方法。查看 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);
}

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

关于android - 有时 GestureDetector 会跳过 onFling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23892451/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com