Android Question Draw text to png image

vfafou

Well-Known Member
Licensed User
Longtime User
Hello!
I need to make some google map markers with dynamic text on them, placed in runtime.
Which is the best way to do that?

Thank you in advance!
 

vfafou

Well-Known Member
Licensed User
Longtime User
Hello Erel!
Thank you for your response!
I'm trying to draw text on a bitmap but the result is to display only the text on Google Map Marker!
The bitmap I want to use as marker image is not displayed ( SPin = AR.GetApplicationDrawable("s" & mSts) )
I'm surely doing something wrong with the mutable bitmap initialisation but I can't figure out what!

My code is doing the following:
B4X:
Private Sub setFleetMarker(mPosition As LatLng,mText As String,mSts As String) As Marker
    Private m As Marker
  
    Private mOp As MarkerOptions
          
    mOp.Initialize
  
    Private bDs As BitmapDescriptor
    Private bDf As BitmapDescriptorFactory
    Private pin As Bitmap
  
    Private AR       As AndroidResources
    Private SPin  As BitmapDrawable
  
    SPin = AR.GetApplicationDrawable("s" & mSts)
    pin = SPin.Bitmap
  
    Private mCol As Int
    If mSts = "2" Then
        mCol = Colors.RGB(90,100,0)
    Else
        mCol = Colors.White
    End If
  
    Private NPin As Bitmap = Utils.GetMBitmap(pin,mText,mCol)
  
    bDs = bDf.FromBitmap(NPin)
    mOp.Icon(bDs)
    mOp.Anchor(mOp.GetAnchorU,mOp.GetAnchorV/2)
    mOp.Position(mPosition)
    mOp.Title(mSts)
    m = GMapEx.AddMarker(GMap, mOp)
  
    Return m
  
End Sub
Where Utils.GetMBitmap(pin,mText,mCol) is:
B4X:
Public Sub GetMBitmap(pImg As Bitmap,pText As String,pCol As Int) As Bitmap
    Private mBitmap As Bitmap = pImg
    Private mCanvas As Canvas
  
    mBitmap.InitializeMutable(pImg.Width,pImg.Height)
    mCanvas.Initialize2(mBitmap)
    mCanvas.DrawText(pText,5dip,10dip,Starter.UbuntuB,26,pCol,"CENTER")
  
    Return mCanvas.Bitmap
  
End Sub
Thank you in advance!
 
Upvote 0

vfafou

Well-Known Member
Licensed User
Longtime User
It's working like a charm, Erel! Thank you!!!
Two last questions:
1. Do I need to do Activity.Invalidate, anywhere in the process?
2. It seems that text is not placed where I want to place it! If I do:
B4X:
mCanvas.DrawText(pText,pImg.Width/2,pImg.Height/3,Starter.UbuntuB,15,pCol,"CENTER")
the text is placed on top of the bitmap! Am I doing something wrong?


UPDATE:
It's OK the text placement! I've just used mRect.Height/2
 
Last edited:
Upvote 0
Top