Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drawRoundRect in Android not working

I want to draw a rounded rectangle with drawRoundRect method in Android.

void drawRoundRect (RectF rect, float rx,  float ry, Paint paint)

I'm using Android Studio, and my testing device use Android 6.0.1, API 23

This is part of my code. It works find when I put zeros in rx and ry.

public void draw(Canvas canvas){
        canvas.drawColor(Color.WHITE);
        canvas.drawRoundRect(rect, 0, 0, pnt);
}

This draws a black rectangle in my screen.

But when I try to make it rounded,

public void draw(Canvas canvas){
        canvas.drawColor(Color.WHITE);
        canvas.drawRoundRect(rect, 10, 10, pnt);
}

It draws nothing. This only draws white background....

I tried different numbers like 100, 3, 5, 0.03f etc in rx and ry,

but any numbers bigger than 0 make drawRoundRect() not working.

Is there anything wrong with my code...?

like image 804
repun Avatar asked Nov 22 '25 08:11

repun


1 Answers

Make sure that coordinates in RectF that is used to draw rounded rectangle are correct. That means: rect.left < rect.right and rect.top < rect.bottom.

It seems that Android 7 corrects wrong coordinates itself and draws desired rounded rectangle, but Android 6 is drawing nothing if there is problem with coordinates.

like image 172
Kacper Dziubek Avatar answered Nov 24 '25 21:11

Kacper Dziubek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!