Drawing Text on a bitmap

splatt

Active Member
Licensed User
Longtime User
I have an ImageView containing a .png file. I need to draw text along the bottom of the image. What is the best way to achieve this?
 

splatt

Active Member
Licensed User
Longtime User
Thanks for the reply SSG.

I tried the label option, but it doesn't work adequately, as I have too many ImageViews and the Zorder can change.

I need the text drawn onto the bitmap canvas, if thats possible. I haven't got my head round the different types of bitmaps that can be used in B4A/Android yet, so was looking for some guidance with this.

Thanks.
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi Steve,

Canvas would be the better way then. It is easy to initialise a new canvas with the imageview as the target.

Then run the drawbitmap and drawtext functions of the canvas.

Finally run the invalidate function of the imageview.

The tricky part is gonna be the positioning (x and y) of the text at the bottom of the image view.

Is the text dynamic? If it isn't and the text is fixed, drawtext functionality of canvas has x and y placement which should help.

Cheers!
 
Upvote 0

splatt

Active Member
Licensed User
Longtime User
The text is dynamic, but I can deal with that.

Problem I've been having is initialising the canvas. I tried a few different ways, but keep getting Null Pointer exceptions.

Do you have an example?
 
Upvote 0

ssg

Well-Known Member
Licensed User
Longtime User
Hi Steve,

Here is a sample code to get a text and picture shown on an image view:


B4X:
Sub Globals
   Dim c As Canvas
   Dim b As Bitmap
   Dim t As String
   Dim i As ImageView
   Dim brect As Rect
   Dim tf As Typeface
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   i.Initialize("")
   Activity.AddView(i, 0, 0, 100, 100)
   
   b.Initialize(File.DirAssets, "pic1.png")
   
   brect.Initialize(0,0,b.Width, b.Height)
   
   c.Initialize(i)
   
   c.DrawColor(Colors.White)
   c.DrawBitmap(b, Null, brect)
   c.DrawText("hello", 10,10,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 10, Colors.Red, "LEFT")
   i.Invalidate
   
End Sub

If you are still hitting error, do post up your code.

Good luck!

ps - sorry for the super lame variable naming....
 
Upvote 0
Top