Android 多点触控获取错误的指针和/或 XY 值
我在从多点触控事件获取 X 和 Y 值时遇到问题。下面的代码显示了当 POINTER_DOWN 和 POINTER_UP 事件被触发时我如何获取值,但是 X 和 Y 值似乎在 POINTER_UP 事件上混合/重复。
@Override
public void onTouchEvent(MotionEvent event) {
int id, pointerIndex;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT;
id = event.getPointerId(pointerIndex);
Log.e("Down", ""+id+" "+event.getX(id)+" "+event.getY(id));
break;
case MotionEvent.ACTION_POINTER_UP:
pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT;
id = event.getPointerId(pointerIndex);
Log.e("UP", ""+id+" "+event.getX(id)+" "+event.getY(id));
break;
}
super.onTouchEvent(event);
}
这通常会导致以下结果:
E/DOWN (25070): 0 279.60922 279.17447
E/DOWN (25070): 1 513.20044 520.3252
E/DOWN (25070): 2 422.6651 358.72418
E/UP (25070): 0 279.60922 279.17447
E/UP (25070): 1 422.6651 358.72418
E/UP (25070): 2 422.6651 358.72418
在这里,您可以看到 id 1 的 XY 位置是错误的,而是显示值 id 2。
请注意,没有调用任何 ACTION_CANCEL 事件。我想我对 MASKS/ANDing 的使用有问题。任何帮助将不胜感激!
I have an issue getting the X and Y values from mutlitouch events. Below is the code showing how I get the value when the POINTER_DOWN and POINTER_UP events are fired, however the X and Y values seem to get mixed up / duplicated on the POINTER_UP event.
@Override
public void onTouchEvent(MotionEvent event) {
int id, pointerIndex;
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_POINTER_DOWN:
pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT;
id = event.getPointerId(pointerIndex);
Log.e("Down", ""+id+" "+event.getX(id)+" "+event.getY(id));
break;
case MotionEvent.ACTION_POINTER_UP:
pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
>> MotionEvent.ACTION_POINTER_ID_SHIFT;
id = event.getPointerId(pointerIndex);
Log.e("UP", ""+id+" "+event.getX(id)+" "+event.getY(id));
break;
}
super.onTouchEvent(event);
}
This typically results in the following lolcat:
E/DOWN (25070): 0 279.60922 279.17447
E/DOWN (25070): 1 513.20044 520.3252
E/DOWN (25070): 2 422.6651 358.72418
E/UP (25070): 0 279.60922 279.17447
E/UP (25070): 1 422.6651 358.72418
E/UP (25070): 2 422.6651 358.72418
Here you can see that the XY location for id 1 is wrong, showing instead the values id 2.
Note that no ACTION_CANCEL events are called. I imagine it's something wrong with my use of the MASKS/ANDing. Any help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然 event.getX 和 event.getY 应该输入指针 index,而不是指针 id。
Apparently event.getX and event.getY should be fed the pointer index, not the pointer id.