Android Code Snippet DrawRoundRect on any Canvas Native Code

This is really usefull, should be on the functions of B4A Canvas.

B4X:
Sub DrawRoundRect (TheCanvas As Canvas, fLeft As Float, fTop As Float, fRight As Float, fBottom As Float, Rx As Float, Color As Int, Filled As Boolean, StrokeWidth As Float)

Dim CnvJO As JavaObject = TheCanvas
Dim RealCanvas As JavaObject = CnvJO.GetField("canvas")

Dim RectF As JavaObject
    RectF.InitializeNewInstance("android.graphics.RectF", Null)
    RectF.RunMethodJO("set", Array(fLeft, fTop, fRight, fBottom))

Dim Paint As JavaObject
    Paint.InitializeNewInstance("android.graphics.Paint", Array(1))
    Paint.RunMethodJO("setStrokeWidth", Array(StrokeWidth))
    Paint.RunMethodJO("setColor", Array(Color))
    If Filled = True Then
        Paint.RunMethodJO("setStyle", Array("FILL"))
    Else
        Paint.RunMethodJO("setStyle", Array("STROKE"))
    End If
   
RealCanvas.RunMethod("drawRoundRect", Array(RectF, Rx, Rx, Paint))

End Sub


Likes apreciated
 

sorex

Expert
Licensed User
Longtime User
Ivan : since which OS is this supported? Doesn't seem to do much on my 2.2 phone.

Edit: it works afterall BUT I need to make it a lot smaller than my canvas size.

if not then I only see the top left corner that's rounden, the rest goes beyond the canvas boundaries.

If RX is the radius then 10 would be 40 pixels margin but it's 50.

B4X:
Dim canvas1 As Canvas
canvas1.Initialize(ImageView1)  'imageview is also 200x200
Dim r1 As Rect
r1.Initialize(0,0,200,200)
canvas1.DrawRect(r1,0xffff0000,True,0)
DrawRoundRect(canvas1,0,0,150,150,10,0xff00ff00,True,0)
 
Last edited:

Saverio

Member
Licensed User
Longtime User
Yes, but in my app I only needed DrawRoundRect. So, I take this as an excersice to use the java Object and avoid one library dependency
Hello Ivan,

in these days I'm studying the same matter

I'd like to submit a variant derived from your code. :D
It's just another point of view.
And it's also another reason of study.
B4X:
Sub Activity_Create(FirstTime As Boolean)

    Dim c As Canvas
    c.Initialize(activity)
    Activity.Tag = c

     Dim rc As Rect
    rc.Initialize(25%x,25%y,75%x,75%y)
    DrawRoundRect(Activity,rc,20,Colors.Green,"FILL",1)

End Sub
'** v Must have the initialized Canvas in its Tag **
' Possible fill Values are: "FILL"; "FILL_AND_STROKE; "STROKE"
Sub DrawRoundRect(v As View,rc As Rect,radius As Float,color As Int,fill As String,stroke As Float) As Boolean
Try 
    Dim rcf As JavaObject
    rcf=rcf.InitializeNewInstance("android.graphics.RectF",Null)
    rcf.RunMethod("set",Array(rc))
 
    Dim mPaint As JavaObject = v.Background
    mPaint = mPaint.RunMethod("getPaint",Null)
    mPaint.RunMethod("setStrokeWidth",Array(stroke))
    mPaint.RunMethod("setColor",Array(color))
    mPaint.RunMethod("setStyle",Array(fill))
     
    Dim mCanvas As JavaObject = v.Tag
    mCanvas = mCanvas.GetField("canvas")
    mCanvas.RunMethod("drawRoundRect",Array(rcf,radius,radius,mPaint))
 
    Return True
 
Catch
  Log(LastException.Message)
  Return False
End Try
End Sub

If needed, there are lot of usable variants.
For example, pass the relative coordinates instead of those absoute as they are or pass them
one by one instead of inside a Rect object.

In short there are lot of choices.

Regards
 

Attachments

  • DrawRoundRect1.zip
    6.4 KB · Views: 415

sorex

Expert
Licensed User
Longtime User
Indeed, options enough.

I even made it for someone without the use of any library beside the core.
 

Informatix

Expert
Licensed User
Longtime User
If someone wants performance (because the B4a canvas is very slow) and more features for all kinds of drawing, there's the Accelerated Surface library. To create a round rectangle filled with red color:
B4X:
Dim O As AS_Object
O.CreateRoundRect(140dip, 110dip, 20dip, 20dip, True).SetColor(Colors.Red)
AC.DrawObjectAt(O, 0, 0)
And to fill it with any image, you replace SetColor by SetTexture(your_image).
 

Similar Threads

Top