我正在使用以下代码为android创建一个多边形列表供以后重绘。 问题是由于某种原因,当一个手指被按住而另一个手指被按到屏幕时,返回的指针ID是0。但是,当一个或多个手指同时按下屏幕时,会生成正确的Id。
public boolean onTouchEvent(MotionEvent e) {
//Cycle through all pointer indexes
for(int i=0; i < e.getPointerCount(); i++){
int PointerId=e.getPointerId(i); //Get the pointer Id for that index.
int x= (int)e.getX(e.findPointerIndex(PointerId)); //Get the x coordinate for the current pointer id.
int y= (int)e.getY(e.findPointerIndex(PointerId)); //Get the y coordinate for the current pointer id.
//Handle Touch events.
switch(e.getActionMasked()){
case MotionEvent.ACTION_POINTER_DOWN: //Check for subsequent Pointer down events.
case MotionEvent.ACTION_DOWN:{ //Check for first finger down.
pntsU.put(PointerId,new Polygon(x,y,Color));//Create a new polygon finger down event, and associate with the Pointer Id.
Log.i("Pointer Id: ","" + PointerId); //Log the pointer id for testing.
break;
}
case MotionEvent.ACTION_MOVE:{ //A finger move event has occured.
pntsU.get(PointerId).Points.add(new Point(x,y));//Add a point to the Associated polygon.
break;
}
case MotionEvent.ACTION_POINTER_UP: //A finger has been lifted.
case MotionEvent.ACTION_UP:{
pntsCommited.add(pntsU.get(PointerId)); //Store the associated polygon in a permanent list.
break;
}
}
}
invalidate();
return true;
}
编辑:对于任何关心或有此问题的人,我已经解决了。 这是我更新的代码。
public boolean onTouchEvent(MotionEvent e) {
int x,y;
int PointerId,PointerIndex;
//Handle Touch events.
switch(e.getActionMasked()){
case MotionEvent.ACTION_POINTER_DOWN: //Check for subsequent Pointer down events.
case MotionEvent.ACTION_DOWN:{ //Check for first finger down.
PointerIndex = (e.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
PointerId= e.getPointerId(PointerIndex);
x= (int) e.getX(PointerIndex);
y= (int) e.getY(PointerIndex);
pntsU.put(PointerId,new Polygon(x,y,Color)); //Create a new polygon finger down event, and associate with the Pointer Id.
break;
}
case MotionEvent.ACTION_MOVE:{ //A finger move event has occurred.
//Cycle through all pointer indexes
for(int i=0; i < e.getPointerCount(); i++){
PointerId=e.getPointerId(i); //Get the pointer Id for that index.
x= (int)e.getX(e.findPointerIndex(PointerId)); //Get the x coordinate for the current pointer id.
y= (int)e.getY(e.findPointerIndex(PointerId)); //Get the y coordinate for the current pointer id.
x=Math.max(x,0); //Enforce x and y boundaries.
x=Math.min(x,getWidth());
y=Math.max(y,0);
y=Math.min(y,getHeight());
pntsU.get(PointerId).Points.add(new Point(x,y)); //Add a point to the Associated polygon.
}
break;
}
case MotionEvent.ACTION_POINTER_UP: //A finger has been lifted.
case MotionEvent.ACTION_UP:{
PointerIndex = (e.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
PointerId= e.getPointerId(PointerIndex);
pntsCommited.add(pntsU.get(PointerId)); //Store the associated polygon in a permanent list.
break;
}
}
invalidate();
return true;
}
请注意,我已将for循环代码移动到修复问题的Action_Move部分。我不确定为什么会这样,但我的猜测是它可能与Action_Move事件总是返回指针Id为0的事实有关,因此在某些情况下导致第二次点击返回0。强制在Action_Move中循环指针索引将解决此问题,并确保您获得正确的指针Id。