how to create a grid using canvas in android studio

1.6k views Asked by At

I am trying to create a 10x10 grid in android studio that will display in the center of the screen, i am trying to use a for loop to do this but cant get it right. Does anyone know how to do this, ive been looking on google and there are no examples or guides on how to do it. my code is below.

public class Draw extends View {
Paint red = new Paint();
Paint green = new Paint();
float startX;
float stopX;
float startY;
float stopY;
int rectSide = 1000;

public Draw(Context context) {
    super(context);
    red.setColor(Color.RED);
    green.setColor(Color.GREEN);
    red.setStrokeWidth(8);
    green.setStrokeWidth(8);
}

@Override
public void onDraw(Canvas canvas) {

    int width = canvas.getWidth();
    int height = canvas.getHeight();

    for (int i = 2; i < 10; i++) {

        startX = width / 2 - rectSide / 2;
        startY = i*100;
        stopX = width / 2 + rectSide / 2;
        stopY = i*100;

        canvas.drawLine(startX, startY, stopX, stopY, red);

    }

    for (int i = 2; i < 10; i++) {

        startX = i*100;
        startY = height / 2 - rectSide / 2;
        stopX = i*100;
        stopY = height / 2 + rectSide / 2;

        canvas.drawLine(startX, startY, stopX, stopY, red);

    }
}
}
0

There are 0 answers