2011-07-06 155 views
1

我正在创建一个视图,需要消耗几乎任何手势。为此,我创建了一个ScaleGestureDetector和一个GestureDetector。我还创建了一个监听器类,并意识到可以实现我需要的每个接口;所以我做了。这使得总的意义OnGestureListener和OnDoubleTapListener,因为它们来自同一类,但:一个OnGestureListener对象可以处理两个GestureDetector对象吗?

  • 请问ScaleGestureDetector想到自己的监听器类?
  • 如果它对同一个班级感到满意,它会期待它自己的对象吗?
  • 相反,我是否需要在两个探测器上使用相同的监听器?

实验已经确认以下内容:

  • 你的确可以使用一个监听器类,但如果他们消耗相同的事件
  • ScaleGestureDetector和GestureDetector可惹恼对方。然而
  • 看来你可以总是先打电话规模探测器,然后运行常规检测前检查其isInProgress()方法阻止这种相互irking:

    public boolean onTouchEvent(MotionEvent event) { 
    //let the ScaleGestureDetector try first 
        mScaleDetector.onTouchEvent(event); 
    //if isInProgress() returns true then it's consuming the event 
        if(mScaleDetector.isInProgress()) return true; 
    //if isInProgress() returns false it isn't consuming the event 
    //it's therefore safe to pass it to the regular detector 
        mPrimaryDetector.onTouchEvent(event); 
        return true; 
    } 
    

回答

2

ScaleGestureDetector和GestureDetector能如果他们 消耗相同的事件互相惹恼。不过看来你可以总是先打电话规模探测器,然后运行定期检测

个人之前检查 其isInProgress()方法阻止这种相互 irking,我没有让他们两个手柄发现的所有问题相同的触摸事件。

该android GestureDetector有一个constructor这需要一个布尔ignoreMultiTouch。将ignoreMultiTouch设置为true将确保GestureDetector触摸事件处理忽略任何mutitouch事件。 (安卓实际上是设置ignoreMultiTouchtrue如果目标API等级> = Froyo的,所以你可能不会需要明确设置它。)

如果你只叫mPrimaryDetector.onTouchEvent(event),当mScaleDetector.isInProgress()返回false,你会不正确地得到长按活动。原因是GestureDetector在其onTouchEvent(MotionEvent ev)下面的代码,以确保它不会与多点触控手势冲突:

case MotionEvent.ACTION_POINTER_DOWN: 
    if (mIgnoreMultitouch) { 
    // Multitouch event - abort. 
    cancel(); 
    } 
    break; 

cancel()会做什么它说,并取消任何单点触摸手势。 (如果你真的好奇,你可以自己看看GestureDetector code;它实际上使用处理程序来发送/删除消息)。

希望这可以帮助任何遇到同样问题的人。

+0

这非常有用,谢谢!我根本不知道“ignoreMultiTouch”。 –

+0

顺便说一句,“烦恼”的表现形式是“MotionEvent”被一个监听器类改变,导致另一个监听器崩溃。 –

+1

@Noel看起来像ignoreMultiTouch参数已被重命名为未使用,并像它被命名不再使用。不知道为什么。 – Flynn81

0

这对我的伟大工程:

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    m_sGestureDetector.onTouchEvent(event); 
    m_GestureDetector.onTouchEvent(event); 
    return true; 
} 
+1

我假设'm_sGestureDetector'是'ScaleGestureDetector'?无论如何,这就像我在我的问题结束时包含的代码片段,除了我还有'if(mScaleDetector.isInProgress())返回true;'在比例检测器消耗事件的情况下在检测器之间。 –

3

要确定是否MotionEvent是一个多点触摸事件,只需使用MotionEvent.getPointerCount() > 1。所以我认为下面的代码会很好用:

public boolean onTouchEvent(MotionEvent event) { 
    if (event.getPointerCount() > 1) { 
     mScaleDetector.onTouchEvent(event); 
    } else { 
     mDetector.onTouchEvent(event); 
    } 
    return true; 
} 
+0

这并不包括如果指针数量发生变化会发生什么情况:至少必须通过操作进行过滤,多点触控手势通常会在稍微不同的时间点指向下。 –

相关问题