Android Question Can't use Canvas.DrawText in a class

NeoTechni

Well-Known Member
Licensed User
Longtime User
However Canvas.DrawBitmap works fine.

I've even tried rewriting the DrawText line without using any of the variables, and I get the same result.

B4X:
public Sub DrawHTML(BG As Canvas, X As Int, Y As Int, Width As Int, Height As Int)
   Dim tempX As Int = X, temp As Int, Info As TextInfo, NeedsClip As Boolean = Width > 0 And Height > 0, Image As ImageInfo, tempX2 As Int, tempY2 As Int, Alignment As String
   If Not(WasSplit) Then
       If Width > 0 Then WordWrapHTML(BG, Width)
   else If LineHeight = 0 Then
       CalculateHTMLSize(BG, False)
   End If
   If NeedsClip Then MakeClipPath(BG,X,Y,Width,Height)   
   Y = Y - StartY
   For temp = 0 To ParsedHTML.Size-1
       Info = ParsedHTML.Get(temp)
       If Info.Align > 0 Then
           Select Case Info.Align
               Case 7,4,1
                   Alignment = "LEFT"
                   tempX2 = X
               Case 8,5,2   
                   Alignment = "CENTER"
                   tempX2 = X + Width * 0.5 - Info.Width * 0.5
               Case 9,6,3
                   Alignment = "RIGHT"
                   tempX2 = X + Width - Info.Width
           End Select
           Select Case Info.Align
               Case 7,8,9'top
                   tempY2 = Y + Info.LineHeight
               Case 4,5,6'middle
                   tempY2 = Y + LineHeight * 0.5 - Info.LineHeight * 0.5
               Case 1,2,3'bottom
                   tempY2 = Y + LineHeight - (Info.Height - Info.LineHeight)
           End Select           
           BG.DrawText(Info.Text, tempX2, tempY2, Info.Font, Info.Size, Info.Color, Alignment)
       Else
           Select Case Info.Align
               Case TAG_CRLF, TAG_CRLF_Natural
                   Y = Y + LineHeight
                   X = tempX
               Case TAG_IMG
                   Image = Images.Get(Info.Size)
                   BG.DrawBitmap(Image.BMP, Null, SetRect(X, Y, Info.Width, Info.Height))
                   X = X + Info.Width
           End Select
       End If
   Next
   If NeedsClip Then BG.RemoveClip
End Sub
 

Attachments

  • Untitled.png
    Untitled.png
    53.2 KB · Views: 193

NeoTechni

Well-Known Member
Licensed User
Longtime User
The instructions make no sense since I'm passing a canvas into the sub when I call it. So it shouldn't need the canvas to be anywhere else. And I've seen examples use the canvas in a class (ie: the Draw sub)
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
I emptied the class for the sake of file size. And put it back once I saved as a zip.

If I made a smaller example it would just be the code I pasted above
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private cv As Canvas
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(canvas As Canvas)
    cv = canvas
End Sub
public Sub Draw(text As String)
    cv.DrawText(text, 1,1, Typeface.DEFAULT, 10, Colors.Red, "LEFT")
End Sub

main
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private testtest As test
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Dim c As Canvas
    testtest.Initialize(c)
    testtest.Draw("Hallo")
End Sub
 
Upvote 0

NeoTechni

Well-Known Member
Licensed User
Longtime User
Thank you, I know it can be done that way. But it shouldn't need that since the other Draw methods work.
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
If to add Private v As View in Class_Globals, it's possible to compile.
But v do not have any relation to class. So this is actually "strange".
 
Upvote 0
Top