Android Question How to convert text to transparent bitmap

toby

Well-Known Member
Licensed User
Longtime User
I need some custom bitmaps to show the google map marker titles of all markers automatically without tapping. The created bitmap should just be large enough to show entire text.

I'm facing two problems with following code:
1. B4XCanvas expects a view which I can't provide; the created bitmaps will be shown as part of google map marker icons.
2. I couldn't make the bitmap's background transparent.
following code block creates a bitmap with text provided:
'''''''''''''''''''''''''''''''''''''''
Sub Globals
   Dim cvs As B4XCanvas    
   Private ImageView1 As ImageView
End Sub

sub CreateBitMap
    cvs.Initialize(ImageView1)
    Dim text As String="hello"
    Dim Fnt As B4XFont
    Fnt=xui.CreateDefaultFont(28)

    Dim centerX As Int=100
    Dim centerY As Int=60
    
    
    Dim r As B4XRect = cvs.MeasureText(text, Fnt)
    'ImageView1.Background=BackGround(Colors.Transparent) 'this line makes bitmap invisible
    ImageView1.Width=r.Width
    ImageView1.Height=r.Height
    Dim BaseLine As Int = centerY - r.Height / 2 - r.Top
    cvs.DrawText(text, centerX, BaseLine, Fnt, Colors.red, "CENTER")

   Dim bmp As Bitmap=cvs.CreateBitmap 
   'to do: code to save the created bitmap
end sub


Sub BackGround(Color As Int) As ColorDrawable
    Dim cdb As ColorDrawable

    cdb.Initialize(Color, 5dip)
    Return cdb
End Sub

A few threads in the forum mention "TextToBitmap" which might be what I need; unfortunately I couldn't find out which library it belongs to.

Could someone help me create a bitmap, from text, for use as google map marker icon, and/or locate "TextToBitmap" function/library, please?

TIA
 

TILogistic

Expert
Licensed User
Longtime User
B4X multiplatform:

this works better:

library XUI Views

B4X:
    B4XImageView1.SetBitmap(TextToBitmap("Text to Bitmap", 20, xui.Color_Black, "CENTER"))
    B4XImageView1.mBackgroundColor = xui.Color_Transparent
    B4XImageView1.ResizeMode = "NONE"
    B4XImageView1.Update


B4X:
Public Sub TextToBitmap (Text As String, FontSize As Float, FontColor As Int, TextAlignment As String) As B4XBitmap
    Dim xui As XUI
    Dim p As B4XView = xui.CreatePanel("")
    p.SetLayoutAnimated(0, 0, 0, 700dip, 700dip)
    Dim cvs1 As B4XCanvas
    cvs1.Initialize(p)
    Dim fnt As B4XFont = xui.CreateDefaultFont(FontSize)
    Dim r As B4XRect = cvs1.MeasureText(Text, fnt)
    Dim BaseLine As Int = cvs1.TargetRect.CenterY - r.Height / 2 - r.Top
    cvs1.DrawText(Text, cvs1.TargetRect.CenterX, BaseLine, fnt, FontColor, TextAlignment)
    Dim b As B4XBitmap = cvs1.CreateBitmap
    cvs1.Release
    Return b
End Sub
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Test;
1616206950605.png
 

Attachments

  • TextToBitmap.zip
    3.3 KB · Views: 329
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Some time ago, I made a text to bitmap subroutine for google maps, which you can also save to .png files.

Use this Post:

Demo:
1616222253446.png
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Check this out and it will give you an idea.

to create marker and information window

 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
see comments on this post:

 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
I found the subroutine that I used in a project.

demo:

1616229796385.png
 

Attachments

  • B4XImageView1.png
    B4XImageView1.png
    640 bytes · Views: 270
  • B4XImageView2.png
    B4XImageView2.png
    758 bytes · Views: 257
  • B4XImageView3.png
    B4XImageView3.png
    666 bytes · Views: 253
  • B4XImageView4.png
    B4XImageView4.png
    768 bytes · Views: 262
Upvote 0

AnandGupta

Expert
Licensed User
Longtime User
WOW ! WOW !! oparra !!!

You gave so much help that now toby is spoilt for choice ! Even me :)

Regards,

Anand
 
Upvote 0
Top