Question

I am making a noughts and crosses game and i want to draw a line over the winning combination. The playing grid consists of a grid of buttons and i want the line to be over the top of them. At the moment the line will draw but it will have a black background hiding the grid of buttons even though it is set to transparent.

How would i make the transparency work then clear when i want to start a new game? I hope that makes sense.

This is the draw class:

public class DrawView extends View  {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public DrawView(Context context) {
        super(context);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
    }

    @Override
      public void onDraw(Canvas canvas) {
           canvas.drawLine(0, 0, 1000, 1000, paint);
    }

}

This is the part of the method in the game:

public void checkWin() {
        if (squares[1] == 1 & squares[2] == 1 & squares[3] == 1) {
            for (int i = 1; i <= 9; i++) {
                buttons[i].setEnabled(false);
            }

            drawView = new DrawView(this);
            drawView.setBackgroundColor(Color.TRANSPARENT);
            setContentView(drawView);

            Intent d = new Intent(this, Win.class);
            startActivity(d);
Was it helpful?

Solution

If you want to draw a line with alpha, use :

paint.setAlpha(125);
paint.setColor(Color.RED);
paint.setStrokeWidth(10);

Just remember to set it back after you use it, if you use paint else where.

Note:

  • 0 = completely transparent
  • 255 = completely opaque

Edit: Okay, I think you might be trying to clear the canvas, so it starts off blank.

Draw clear onto it, with PorterDuff to clear it.

@Override
  public void onDraw(Canvas canvas) {
       canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
       canvas.drawLine(0, 0, 1000, 1000, paint);
}

I dont think your setBackgroundColor works because you have overriden the draw method. But I could be wrong, the above code should fix the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top