这是由于Android辅助服务的限制,只能处理单点触摸而非多点触摸引起的问题。为了解决这个问题,应该使用Android触摸事件处理程序来处理多点触摸事件。下面是一个包含示例代码的
1.创建一个自定义的View类,用于处理多点触摸事件:
public class MultiTouchView extends View {
private Paint paint;
private int[] colors = {Color.RED, Color.GREEN, Color.BLUE};
private List points = new ArrayList<>();
public MultiTouchView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
int index = event.getActionIndex();
PointF point = new PointF(event.getX(index), event.getY(index));
points.add(point);
break;
case MotionEvent.ACTION_MOVE:
for (int i = 0; i < event.getPointerCount(); i++) {
index = i;
point = points.get(i);
point.x = event.getX(i);
point.y = event.getY(i);
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_CANCEL:
index = event.getActionIndex();
points.remove(index);
break;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
for (int i = 0; i < points.size(); i++) {
paint.setColor(colors[i % colors.length]);
canvas.drawCircle(points.get(i).x, points.get(i).y, 50, paint);
}
}
}
2.在布局文件中引用自定义的View类,并使用属性“android:onClick”将其与一个按钮关联起来: