我正在关注检测常见手势指南。我已链接到android-support-v4.jar
库以获取GestureDetectorCompat
,并且我的代码似乎与指南中的完全相同,除了我在自定义视图中检测手势而不是在活动中:
public class MyGlView extends GLSurfaceView {
private GestureDetectorCompat m_gestureDetector = null;
public MyGlView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyGlView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
if (m_gestureDetector == null)
m_gestureDetector = new GestureDetectorCompat(context, new MyGestureListener());
setEGLContextClientVersion(2);
setRenderer(new DrawSurfRenderer());
setRenderMode(RENDERMODE_CONTINUOUSLY);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
m_gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
Log.e("", "OnScroll: deltaX=" + String.valueOf(e2.getX() - e1.getX()) + ", deltaY=" + String.valueOf(e2.getY() - e1.getY()));
return true;
}
@Override
public boolean onSingleTapUp(MotionEvent e)
{
Log.e("", "onSingleTapUp: X=" + String.valueOf(e.getX()) + ", Y=" + String.valueOf(e.getY()));
return true;
}
@Override
public void onLongPress(MotionEvent e)
{
Log.e("", "onLongPress: X=" + String.valueOf(e.getX()) + ", Y=" + String.valueOf(e.getY()));
}
}
无论我用触摸屏做什么,我只会得到onLongPress
. 事实上,当我快速点击(快速触摸并释放屏幕)时onLongPress
,我的手指从屏幕上移开后仍然会有轻微的感觉(怀疑这是一个长的点击检测延迟)。
有什么问题?