0

I'm trying to draw a rounded rectangle on a Canvas object but nothing is showing up. I initialize the RoundRectShape like so:

test = new RoundRectShape(new float[]{r, r, r, r, r, r, r, r}, new RectF(100, 100, 100, 100), null);

I then call test.draw(canvas) but nothing shows up. Why is it not drawing, and did I create the RoundRectShape correctly? Because I was quite confused about how I'm supposed to do it.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Ryan
  • 727
  • 2
  • 14
  • 31

1 Answers1

1

From the documentation for RoundRectShape:

void draw (Canvas canvas, Paint paint)

Before calling this, you must call resize(float, float).

If you do not call resize(), your shape has no dimensions.

Community
  • 1
  • 1
Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • yes, this worked :). Is there any way that I can set the location of the shape? Because at the moment it is just at 0,0 in the corner. – Ryan Aug 02 '17 at 13:26
  • @Ryan Not 100% sure since I haven't tried it, but it looks like you can construct a new `ShapeDrawable` using your `RoundRectShape`, then call `setBounds()` on that new drawable, and then draw that drawable to the canvas. – Ben P. Aug 02 '17 at 13:29
  • This set the location but because the `ShapeDrawable` draw function doesn't have a `Paint` parameter, it removes the color of and such from the shape – Ryan Aug 02 '17 at 13:39
  • Nevermind. I managed to fix the problem by omitting the use of a `RoundRectShape` object at all and using a `Path` instead. Ultimately made things much easier – Ryan Aug 02 '17 at 13:51