1
votes

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;
}

}

1
you can use getPointerCount method to get pointer counts and work on circles by using index numbers.Mohammed Junaid

1 Answers

0
votes

I think the problem is in the draw method. From what I can tell pointerList is a Map that uses the pointer ID as the key. In the draw method you're trying to get all the pointer that have the key in the range 0 to total pointers - 1 which is incorrect. You're trying to get the pointers by index but they were stored by ID.

Try this:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    for(Map.Entry<Integer, Pointers> entry : pointersList.entrySet()){
        Pointers point = entry.getValue();
        canvas.drawCircle(point.getX(),point.getY(),SIZE,point.getPaint());
    }
}