【发布时间】: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.我的情况不需要拖动,只有上下事件.