0

I would like to draw small rectangles on a Canvas. The problem is that the rectangles don´t get any border, so the result is just a big rectangle (containing all the small rectangles (20x20 rectangles as stated below)).

In my custom View, I override onDraw:

protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);
    canvas.drawRect(getWidth(), getHeight(), 50, 50, paint);

    paint.setColor(Color.LTGRAY);
    paint.setStrokeWidth(3);
    for(int i = 0; i < 20; i++) {
        for(int j = 0; j < 20; j++) {
            canvas.drawRect(20*j, 20*i, 50, 50, paint);
        }
    }
} 

For Java Swing, you can just do

g.setColor(Color.red);
for(int x = 0; x < 20; x++) {
    for(int y = 0; y < 20; y++) {
       g.drawRect(20 * x, 20 * y, 20, 20);
    }
 }

to paint the rectangles and each rectangle will have red borders.

How can I achieve this on Android?

Rox
  • 2,647
  • 15
  • 50
  • 85

1 Answers1

0

I guess you need to change paint style to Stroke before drawing small rectangles. Use mPaint.setStyle(Paint.Style.STROKE) along with setting stroke width. In your code you haven't declared paint style anywhere. If you use Paint.Style.FILL it will fill the rectangle.

kaushal trivedi
  • 3,405
  • 3
  • 29
  • 47