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

Discussion in 'Code Snippets' started by Erel, Jun 24, 2018.

  1. Erel

    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:

    Code:
    'Example
    Sub Page1_Click
           
       DrawPolygon(cvs, 
    Array (Array As Int(100dip100dip), Array As Int(200dip0), _
           
    Array As Int (300dip100dip), Array As Int(200dip200dip), Array As Int(100dip100dip)), _
            xui.Color_Red, 
    True2dip)
       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
     
  2. Star-Dust

    Star-Dust Expert Licensed User

    Why not edit jXUI library and have this method already integrated into jB4XCanvas class?
     
    ivan.tellez likes this.
  3. OliverA

    OliverA Well-Known Member Licensed User

    Erel likes this.
  4. Star-Dust

    Star-Dust Expert Licensed User

    Is it already included in XUI library 1.72?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    You can see the available methods in the IDE. It is not included.
     
    Star-Dust likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice