pointerIndex out of range Android multitouch

安稳与你 提交于 2019-11-27 12:04:19

My problem was that it was actually calling event.getX(1) when there wasn't actually two ids. So I made sure that there were two ids with event.getPointerCount() >= 2 and it now works. Maybe you'll have the same luck!

Your missing a few things, you need to apply the mask to the pointer otherwise as you are not technically accessing the ID of the finger you think you are

int action = event.getAction() & MotionEvent.ACTION_MASK;     
int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
int pointerId = event.getPointerId(pointerIndex);

I'm also having this error but none of the solutions before the date of my post work. The only temporary pseudo-fix which works for me is to use a try-catch against IllegalArgumentException.

According to Android Issue 18990, it is related to ScaleGestureDetector and a fix has been committed few days ago.

blackjack102

Just change your getPointerId() to findPointerIndex(). Maybe change geAction() to getActionMasked().

Hope it helps!

The original posting is using the pointer id when the getX and getY use the pointer index.

It appears to work when you use the ID for a single touch because the id and the index are both 0. It will mess up if you use a multi-touch because the indexes can change.

Example:

Touch 1 Down.
Touch 1 State Index=0. ID=0

Touch 2 Down.
Touch 1 State Index=0. ID=0
Touch 2 State Index=1. ID=1

Touch 1 Release.
Touch 2 State Index=0. ID=1

Try the following code:

final int action = e.getAction();       
final int pointerIndex =(action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
                >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;      
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!