How to delete in android studio something drawn on the canvas

0

I would like to know if you can delete something drawn on the canvas. In this example I have 2 circles, circle 1 can move while circle2 is static. I would like that when the circles collide the circle2 disappears. Is it possible to do that?

Thank you.

public class Juego extends View implements View.OnTouchListener {

int x = 100;
int y = 100;
int radio = 100;
Paint paint;
int x2, y2;

public Juego(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(this);
    setFocusable(true);
    paint = new Paint();
}

public void onDraw(Canvas canvas){
    Paint paint1 = new Paint();
    paint1.setColor(Color.GREEN);
    canvas.drawCircle(x, y, radio, paint);
    x2= canvas.getWidth()/2;
    y2=canvas.getHeight()/2;
    canvas.drawCircle(x2, y2, radio, paint1);

    double dist = Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));

    if (dist < radio + radio){

    }
}

public boolean onTouch(View view,MotionEvent event){

    x = (int)event.getX();
    y = (int)event.getY();

    invalidate();
    return true;
}

}

I have the 2 circles and using an equation calculate the distance the problem is that I do not know how to do to erase one of the circles.

    
asked by Agustin Val 16.07.2017 в 02:51
source

2 answers

1

Sorry for the previous comment. Nothing is understood. If you want it to disappear altogether, I would add a Boolean. public class Game extends View implements View.OnTouchListener {

int x = 100;
int y = 100;
int radio = 100;
Paint paint;
int x2, y2;
boolean muere = false;

public Juego(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.setOnTouchListener(this);
    setFocusable(true);
    paint = new Paint();
}

public void onDraw(Canvas canvas) {
    Paint paint1 = new Paint();
    paint1.setColor(Color.GREEN);
    canvas.drawCircle(x, y, radio, paint);

    // Si muere es false se dibuja el circulo verde
    if (!muere) {
        x2 = canvas.getWidth() / 2;
        y2 = canvas.getHeight() / 2;
        canvas.drawCircle(x2, y2, radio, paint1);
    }

    double dist = Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));

    if (dist < radio + radio) {
        // Con muere = true, deja de dibujarse el circulo verde
        muere = true;
    }
}

public boolean onTouch(View view, MotionEvent event) {

    x = (int) event.getX();
    y = (int) event.getY();

    invalidate();
    return true;
}

}

    
answered by 17.07.2017 / 09:35
source
0

With one condition. If (circle 1 does not touch circle 2) circle 2 is drawn. When circle 1 touches circle 2, circle 2 will not be drawn.

public void onDraw(Canvas canvas){
    Paint paint1 = new Paint();
    paint1.setColor(Color.GREEN);
    canvas.drawCircle(x, y, radio, paint);

    double dist = Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));

    if (la condicion que quieras){
     // Se dibuja el circulo 2
     x2= canvas.getWidth()/2;
     y2=canvas.getHeight()/2;
     canvas.drawCircle(x2, y2, radio, paint1);
    }
}
    
answered by 16.07.2017 в 11:04