Code to create a rectangle image with rounded corners:
Compatible with B4A, B4i and B4J. Depends on XUI library v1.47+.
Usage example:
Compatible with B4A, B4i and B4J. Depends on XUI library v1.47+.
B4X:
Sub CreateRoundRectBitmap (Input As B4XBitmap, Radius As Float) As B4XBitmap
Dim BorderColor As Int = xui.Color_Black
Dim BorderWidth As Int = 4dip
Dim c As B4XCanvas
Dim xview As B4XView = xui.CreatePanel("")
xview.SetLayoutAnimated(0, 0, 0, Input.Width, Input.Height)
c.Initialize(xview)
Dim path As B4XPath
path.InitializeRoundedRect(c.TargetRect, Radius)
c.ClipPath(path)
c.DrawRect(c.TargetRect, BorderColor, True, BorderWidth) 'border
c.RemoveClip
Dim r As B4XRect
r.Initialize(BorderWidth, BorderWidth, c.TargetRect.Width - BorderWidth, c.TargetRect.Height - BorderWidth)
path.InitializeRoundedRect(r, Radius - 0.7 * BorderWidth)
c.ClipPath(path)
c.DrawBitmap(Input, r)
c.RemoveClip
c.Invalidate
Dim res As B4XBitmap = c.CreateBitmap
c.Release
Return res
End Sub
Usage example:
B4X:
Dim img As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, "image.png", ImageView1.Width, ImageView1.Height, True)
ImageView1.SetBitmap(CreateRoundRectBitmap(img, 20dip)) 'ImageView1 type is set to B4XView