Android Question Canvas Drawtext parameters

ducphu

Active Member
Licensed User
Longtime User
Hi all,

Can some one explain to me in details the parameters for Canvas.Drawtext command. I read through the document but not well understanded. Especially the x,y origin points. Thank you.
 

derez

Expert
Licensed User
Longtime User
The parameters are like those of a label's text. The x is like the label's top and the y is like the label's bottom (so if you draw text on the top of a panel - it will not show).
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Let say I have a bitmap of 100x20(WxH) and I want to draw a text like "Welcome", what does x and y mean? Is (x,y) point the point where it starts to draw, i.e "W" character? How does the alignment(left,center,right) actually effect?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Look at page 302 in the User's Guide.
Page 302 of 182? Did you mean the BeginnersGuide?

Edit: Yes... It´s in the BeginnersGuide

drawtext001.png
 
Last edited:
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
Thanks Don, very nice explaination. Can I ask to which the x and y value refer to, the activity size or the bitmap? I'm working with a widget which an imageview (146x26) I create a bitmap wiith the same size 146x26. According to your post, if I draw a text at 73x26 with center alignment, the text should be draw at the center and bottom of the bitmap/imageview. But when I test it on my phone, it doesn't.
 
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
B4X:
Dim Font As Typeface
        Dim Canvas As Canvas
        Dim imgCC As Bitmap
        Font = Typeface.LoadFromAssets("CORSIVA.ttf")
        imgCC.InitializeMutable(146dip,26dip)
        Canvas.Initialize2(imgCC)
        Canvas.DrawText("A", 0, 26, Font, 18, Colors.White, "LEFT")
        Canvas.DrawText("B", 73, 26, Font, 18, Colors.White, "LEFT")
        Canvas.DrawText("C", 146, 26, Font, 18, Colors.White, "LEFT")
        Canvas.DrawText("A", 0, 26, Font, 18, Colors.Blue, "CENTER")
        Canvas.DrawText("B", 73, 26, Font, 18, Colors.Blue, "CENTER")
        Canvas.DrawText("C", 146, 26, Font, 18, Colors.Blue, "CENTER")
    End If

Then I draw on a imageviewer 146x26. Attached the screenshoot. The grey line is draw across the widget from 1 end to the other end, and is at the bottom of the imageview.
 

Attachments

  • Untitled.png
    Untitled.png
    184.8 KB · Views: 339
Last edited:
Upvote 0

ducphu

Active Member
Licensed User
Longtime User
shouldn´t it be
B4X:
Canvas.DrawText("A", 0dip, 26dip, Font, 18, Colors.White, "LEFT")
instead of
B4X:
Canvas.DrawText("A", 0, 26, Font, 18, Colors.White, "LEFT")
? The same for all others too...

Yeah yeah Don, you are right. My stupid mistake. :(
 
Upvote 0
Top