, . , , , , . . startes ( , , - /). , .
: getPointerCount() . getX (i) getY (i) . : 0, 1, 2 , 1 ( ), 0, 1. , getPointerId (1) 2, - 2, , 1 2. , ACTION_POINTER_UP. ACTION_POINTER_DOWN getActionIndex() , . 0 .
, , ( getActionIndex() 0 ). , 5 . , multitouch, ; -)
. coordsX coordsY , 2 , , , - , , . , , .
public class MultitouchView extends LinearLayout {
private int [] coordsX = new int [5];
private int [] coordsY = new int [5];
public MultitouchView(Context context, AttributeSet attrs) {
super(context, attrs);
Arrays.fill(coordsX, Integer.MIN_VALUE);
Arrays.fill(coordsY, Integer.MIN_VALUE);
}
@Override
public boolean onTouchEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_CANCEL) {
Arrays.fill(coordsX, Integer.MIN_VALUE);
Arrays.fill(coordsY, Integer.MIN_VALUE);
} else {
for (int i = 0; i < e.getPointerCount(); i++) {
int id = e.getPointerId(i);
if (e.getActionIndex() == i
&& (e.getActionMasked() == MotionEvent.ACTION_POINTER_UP
|| e.getActionMasked() == MotionEvent.ACTION_UP)) {
coordsX[id] = coordsY[id] = Integer.MIN_VALUE;
} else {
coordsX[id] = (int) e.getX(i);
coordsY[id] = (int) e.getY(i);
}
}
}
int hw = getWidth() / 2;
int hh = getHeight() / 2;
boolean topLeft = false, topRight = false, bottomRight = false, bottomLeft = false;
for (int i = 0; i < coordsX.length; i++) {
if (coordsX[i] == Integer.MIN_VALUE || coordsY[i] == Integer.MIN_VALUE) {
continue;
}
topLeft = topLeft || coordsX[i] < hw && coordsY[i] < hh;
topRight = topRight || coordsX[i] > hw && coordsY[i] < hh;
bottomRight = bottomRight || coordsX[i] > hw && coordsY[i] > hh;
bottomLeft = bottomLeft || coordsX[i] < hw && coordsY[i] > hh;
}
((ViewGroup) getChildAt(0)).getChildAt(0).setBackgroundColor(topLeft ? 0xffffff00 : 0x0);
((ViewGroup) getChildAt(0)).getChildAt(1).setBackgroundColor(topRight ? 0xffff0000 : 0x0);
((ViewGroup) getChildAt(1)).getChildAt(0).setBackgroundColor(bottomLeft ? 0xff00ff00 : 0x0);
((ViewGroup) getChildAt(1)).getChildAt(1).setBackgroundColor(bottomRight ? 0xff0000ff : 0x0);
return true;
}
}