Canvas - Rectangle with rounded ends

gawie007

Member
Licensed User
Longtime User
I am reading in a Gerber file and drawing it on the screen.
So far it all works well until I need is a simple rectangle with rounded (semi-circle) ends.

I have tried the ABExtDrawing library which would do it but it would mean converting my whole program all for the sake of one small item.

Would it be possible to use this:
Canvas | Android Developers

with the reflection library, and if so, how would I do it?
Any guidance will be greatly appreciated!!!
 

Dave O

Well-Known Member
Licensed User
Longtime User
Hi @klaus , thanks for your help in this thread.

I'm trying to draw a transparent rounded rectangle, where it removes the canvas color entirely and makes that part of the canvas see-through (basically a "hole" in the canvas to see the views below it).

I tried your code above, but the transparency didn't work that way. (It left the existing color of the canvas unaffected.)

However, I figured there might be a transfer mode that would clear the underlying color completely, so I tried this, and it seems to do what I want.

B4X:
Sub drawRoundRect(cvs As Canvas, Rect1 As Rect, Radius As Float, StrokeColor As Int, FillColor As Int, StrokeWidth As Float)
   Dim drw As ABExtDrawing
   Dim r1 As ABRectF
   Dim pt1, pt2 As ABPaint
   r1.Initialize(Rect1.Left, Rect1.Top, Rect1.Right, Rect1.Bottom)

   pt1.Initialize
   pt1.SetStrokeWidth(1)
   pt1.SetColor(FillColor)
   pt1.SetStyle(pt1.Style_FILL)
   'added this line
   If FillColor = Colors.Transparent Then pt1.SetPorterDuffXfermode(drw.PorterDuffMode_CLEAR)
   drw.drawRoundRect(cvs, r1, Radius, Radius, pt1)

   pt2.Initialize
   pt2.SetStrokeWidth(StrokeWidth)
   pt2.SetColor(StrokeColor)
   pt2.SetStyle(pt1.Style_STROKE)
   'added this line
   If StrokeColor = Colors.Transparent Then pt2.SetPorterDuffXfermode(drw.PorterDuffMode_CLEAR)
   drw.drawRoundRect(cvs, r1, Radius, Radius, pt2)
End Sub

Just posting it here for others who want the same effect.

Thanks again!
 
Last edited:
Upvote 0

Dave O

Well-Known Member
Licensed User
Longtime User
Yes, I'm using the sub above to draw on a panel's background, so I can "see through" to the views below the panel.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…