我有一个使用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;
}
}发布于 2014-06-24 02:49:21
对于检测“向上”和“向下”事件,我推荐以下简单的方法:
@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最可能的原因是,它不仅仅是一种处理事件的方法。看一下安卓的源代码,只有当速度达到最低速度时才会调用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);
}(资料来源:r1/android/view/GestureDetector.java)
https://stackoverflow.com/questions/23892451
复制相似问题