B4J Question How to write an Ellipse on Canvas?

amorosik

Expert
Licensed User
I see that among the methods available for a Canvas there is only the DrawCircle
The question is: how to draw an ellipse?
 

klaus

Expert
Licensed User
Longtime User
Using a B4XCanvas you have the B4XPath.InitializeOval(Rect As B4XRect) method.
And use B4XCanvas.DrawPath method.

Be aware that DrawPath works differently than DrawRectangle and DrawCircle when the Filled property is False.
With DrawRectangle and DrawCircle, the Stroke is half on both sides if the base rectangle or circle.
With DrawPath the Stroke in entirely insides the base path.
The small blue line on the thick yellow line shows the difference.
To make a DrawEllipse method working like DrawRectangle and DrawCircle you can use the code below.
Left ellipse pathOval, right ellipse with the code below.

Attached a small test program showing the behavior.

1723555886888.png


B4X:
Private Sub DrawEllipse(Canvas As B4XCanvas, Rect As B4XRect, Color As Int, Filled As Boolean, StrokeWidth As Float)
    Private RectExt As B4XRect
    Private Path, PathExt As B4XPath
    Private StrokeWidth_2 As Float
    
    StrokeWidth_2 = StrokeWidth / 2
    RectExt.Initialize(Rect.Left - StrokeWidth_2, Rect.Top - StrokeWidth_2, Rect.Right + StrokeWidth_2, Rect.Bottom + StrokeWidth_2)
    
    Path.InitializeOval(Rect)
    PathExt.InitializeOval(RectExt)
    If Filled = True Then
        Canvas.DrawPath(Path, Color, True, 1dip)
    Else
        Canvas.DrawPath(PathExt, Color, False, StrokeWidth)
    End If
End Sub
 

Attachments

  • DrawEllipse.zip
    2 KB · Views: 35
Upvote 0
Top