I have a custom view which detects user touch and draws a circle when screen is touched. Different circles for different fingers.And circle is cleared when user removes the finger. Now i have to move the circle while moving the fingers. i can only move 1 circle while moving fingers. IF i have more than 1 finger on screen, how can i move all the circle in the finger direction?
public boolean onTouchEvent(MotionEvent event) {
int pointerIndex = event.getActionIndex();
int pointerId = event.getPointerId(pointerIndex);
int maskedAction = event.getActionMasked();
switch (maskedAction){
case MotionEvent.ACTION_DOWN: {
Pointers pointer = new Pointers();
pointer.setX(event.getX(pointerIndex));
pointer.setY(event.getY(pointerIndex));
pointer.setPaint(getPaint());
pointersList.put(pointerId,pointer);
break;
}
case MotionEvent.ACTION_POINTER_DOWN: {
Pointers pointer = new Pointers();
pointer.setX(event.getX(pointerIndex));
pointer.setY(event.getY(pointerIndex));
pointer.setPaint(getPaint());
pointersList.put(pointerId,pointer);
break;
}
case MotionEvent.ACTION_MOVE:{
int size = event.getPointerCount();
Pointers point = pointersList.get(pointerId);
if (point != null) {
point.setX(event.getX(pointerIndex));
point.setY(event.getY(pointerIndex));
}
}
break;
case MotionEvent.ACTION_UP:{
pointersList.remove(pointerId);
}
case MotionEvent.ACTION_POINTER_UP: {
pointersList.remove(pointerId);
break;
}
case MotionEvent.ACTION_CANCEL:{
pointersList.remove(pointerId);
break;
}
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int size = pointersList.size();
for(int i=0; i<size;i++){
Pointers point = pointersList.get(i);
if(point != null){
canvas.drawCircle(point.getX(),point.getY(),SIZE,point.getPaint());
}
}
}
Pointer Class
public class Pointers {
private float x,y;
private Paint paint;
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public Paint getPaint() {
return paint;
}
public void setPaint(Paint paint) {
this.paint = paint;
}
}