Android Question Canvas Drawtext parameters

ducphu

Active Member
Licensed 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
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).
 

ducphu

Active Member
Licensed 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?
 

ducphu

Active Member
Licensed 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.
 

ducphu

Active Member
Licensed 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

Last edited:

ducphu

Active Member
Licensed 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. :(
 

iamfanghan

Member
Licensed User
Hi there, is there coding to erase the graph so that it wont overlap with the new graph in the same panel?
 
Top