Android Code Snippet [B4X] [XUI] Faster* DrawPolygon Method

Erel

Administrator
Staff member
Licensed User
You can draw polygons with B4XCanvas.DrawPath. The following code calls B4XCanvas.DrawPath on B4A and B4i and calls a different method in B4J. DrawPath in B4J is slower than the other drawing methods as it requires clipping the canvas (it is only required in B4J).

To summarize, only use this code if you encountered a performance issue with B4XCanvas.DrawPath in B4J:

B4X:
'Example
Sub Page1_Click
       
   DrawPolygon(cvs, Array (Array As Int(100dip, 100dip), Array As Int(200dip, 0), _
       Array As Int (300dip, 100dip), Array As Int(200dip, 200dip), Array As Int(100dip, 100dip)), _
        xui.Color_Red, True, 2dip)
   cvs.Invalidate
End Sub

Sub DrawPolygon (cvs1 As B4XCanvas, Points As List, Color As Int, Filled As Boolean, StrokeWidth As Double)
   If Points.Size < 1 Then Return
   #if B4A or B4i
   Dim FirstPoint() As Int = Points.Get(0)
   Dim p As B4XPath
   p.Initialize(FirstPoint(0), FirstPoint(1))
   For i = 1 To Points.Size - 1
       Dim point() As Int = Points.Get(i)
       p.LineTo(point(0), point(1))
   Next
   cvs1.DrawPath(p, Color, Filled, StrokeWidth)
   #Else
   Dim jcvs As JavaObject = cvs1
   jcvs = jcvs.GetFieldJO("cvs").RunMethodJO("getObject", Null).RunMethod("getGraphicsContext2D", Null)
   jcvs.RunMethod("save", Null)
   Dim xpoints(Points.Size), ypoints(Points.Size) As Double
   For i = 0 To Points.Size - 1
       Dim point() As Int = Points.Get(i)
       xpoints(i) = point(0)
       ypoints(i) = point(1)
   Next
   Dim paint As Object = fx.Colors.From32Bit(Color)
   If Filled Then
       jcvs.RunMethod("setFill", Array(paint))
       jcvs.RunMethod("fillPolygon", Array(xpoints, ypoints, Points.Size))
   Else
       jcvs.RunMethod("setStroke", Array(paint))
       jcvs.RunMethod("setLineWidth", Array(StrokeWidth))
       jcvs.RunMethod("strokePolygon", Array(xpoints, ypoints, Points.Size))
   End If
   jcvs.RunMethod("restore", Null)
   #End If
End Sub
 

Star-Dust

Expert
Licensed User
Why not edit jXUI library and have this method already integrated into jB4XCanvas class?
 

OliverA

Expert
Licensed User

Star-Dust

Expert
Licensed User
You can draw polygons with B4XCanvas.DrawPath. The following code calls B4XCanvas.DrawPath on B4A and B4i and calls a different method in B4J. DrawPath in B4J is slower than the other drawing methods as it requires clipping the canvas (it is only required in B4J).

To summarize, only use this code if you encountered a performance issue with B4XCanvas.DrawPath in B4J:

B4X:
'Example
Sub Page1_Click
      
   DrawPolygon(cvs, Array (Array As Int(100dip, 100dip), Array As Int(200dip, 0), _
       Array As Int (300dip, 100dip), Array As Int(200dip, 200dip), Array As Int(100dip, 100dip)), _
        xui.Color_Red, True, 2dip)
   cvs.Invalidate
End Sub

Sub DrawPolygon (cvs1 As B4XCanvas, Points As List, Color As Int, Filled As Boolean, StrokeWidth As Double)
   If Points.Size < 1 Then Return
   #if B4A or B4i
   Dim FirstPoint() As Int = Points.Get(0)
   Dim p As B4XPath
   p.Initialize(FirstPoint(0), FirstPoint(1))
   For i = 1 To Points.Size - 1
       Dim point() As Int = Points.Get(i)
       p.LineTo(point(0), point(1))
   Next
   cvs1.DrawPath(p, Color, Filled, StrokeWidth)
   #Else
   Dim jcvs As JavaObject = cvs1
   jcvs = jcvs.GetFieldJO("cvs").RunMethodJO("getObject", Null).RunMethod("getGraphicsContext2D", Null)
   jcvs.RunMethod("save", Null)
   Dim xpoints(Points.Size), ypoints(Points.Size) As Double
   For i = 0 To Points.Size - 1
       Dim point() As Int = Points.Get(i)
       xpoints(i) = point(0)
       ypoints(i) = point(1)
   Next
   Dim paint As Object = fx.Colors.From32Bit(Color)
   If Filled Then
       jcvs.RunMethod("setFill", Array(paint))
       jcvs.RunMethod("fillPolygon", Array(xpoints, ypoints, Points.Size))
   Else
       jcvs.RunMethod("setStroke", Array(paint))
       jcvs.RunMethod("setLineWidth", Array(StrokeWidth))
       jcvs.RunMethod("strokePolygon", Array(xpoints, ypoints, Points.Size))
   End If
   jcvs.RunMethod("restore", Null)
   #End If
End Sub
Is it already included in XUI library 1.72?
 
Top