只是一个确认文档的快速问题:
getY(int) function from MotionEvent在 Android 中,它的文档让我感到困惑:
Returns the Y coordinate of this event for the given pointer index (use getPointerId(int) to find the pointer identifier for this index). Whole numbers are pixels; the value may have a fraction for input devices that are sub-pixel precise.
有了这个,我认为代码应该这样写:
for(int i = 0; i < event.getPointerCount(); ++i) {
// Some code...
int currentPointer = event.getPointerId(i);
float currentY = event.getY(currentPointer);
Log.i("test", "Pointer " + currentPointer + " has Y-coord of " + currentY);
// Some more code ...
}
但是,在我的 Samsung Galaxy Tab 上进行测试时,我似乎不应该将 currentPointer 传递给 getY,而应该像这样传递 'i':
for(int i = 0; i < event.getPointerCount(); ++i) {
// Some code...
int currentPointer = event.getPointerId(i);
float currentY = event.getY(i);
Log.i("test", "Pointer " + currentPointer + " has Y-coord of " + currentY);
// Some more code ...
}
这是使用 getPointerId 和 getY 函数的正确方法吗?你给他们两个相同的'i'变量而不是他们的实际指针ID?
我问是因为我想确保这在我的设备上没有什么不同,这确实是它的预期工作方式。谢谢。
最佳答案
是的,这是正确的,您应该只指定“i”。
所有 getPointer*(int) 方法都采用相同的索引作为参数。
关于安卓 MotionEvent getY(int) : Which call is the correct way?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7913533/