我正在使用OnGestureListener界面和GestureDetector来处理安卓中的触摸手势。
我制作了一个应用程序来测试检测两个手指是否有效,在onFlp(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY)中,我打印不同MotionEvents的id,但是这些id是相同的(显然只检测到一个手指)。
GestureDetector支持多点触摸事件吗?
发布于 2020-07-18 08:16:40
The Issue
使用OnGestureListener检测多点触摸手势在默认情况下似乎没有实现。
第一件您可能尝试过的事情是阅读event.pointerCount来获取屏幕上的手指数。然而,这将等于1。这是因为你(很可能)永远无法用两根手指在完全相同的毫秒内触摸屏幕。
修复它
您必须缓冲pointerCount (屏幕上手指的数量)。首先,在要跟踪手势的上下文中的某个位置添加这些变量:
// track how many fingers are used
var bufferedPointerCount = 1
var bufferTolerance = 500 // in ms
var pointerBufferTimer = Timer()然后,在onTouchEvent(event: MotionEvent)函数中添加以下内容:
// Buffer / Debounce the pointer count
if (event.pointerCount > bufferedPointerCount) {
bufferedPointerCount = event.pointerCount
pointerBufferTimer = fixedRateTimer("pointerBufferTimer", true, bufferTolerance, 1000) {
bufferedPointerCount = 1
this.cancel() // a non-recurring timer
}
}本质上,这跟踪显示上手指的最大数量,并使其在bufferTolerance毫秒内有效(此处为: 500)。
目前,我正在我创建的一个定制的Android启动程序中实现它(https://github.com/finnmglas/Launcher \\请参见https://github.com/finnmglas/Launcher/issues/52)
https://stackoverflow.com/questions/19707454
复制相似问题