Android Question BitmapDrawable cannot be converted to Bitmap

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Need a circular marker with a specified colour for an Osmdroid markeroverlay and thought I could do it like this:

B4X:
Sub Globals()
 
 Private bmpPatientMarker As BitmapDrawable

End Sub

Sub Activity_Create(FirstTime As Boolean)

 bmpPatientMarker.Initialize(TextToBitmapDrawable("O", 24, Colors.RGB(255, 0, 0)))

End Sub

Sub TextToBitmapDrawable (s As String, FontSize As Float, lFontColour As Long) As BitmapDrawable
 
 Dim bmp As Bitmap
 Dim cvs As Canvas
 Dim bmpD As BitmapDrawable
 
 bmp.InitializeMutable(28dip, 24dip)
 cvs.Initialize2(bmp)
 Dim h As Double = cvs.MeasureStringHeight(s, lblID.Typeface, FontSize)
 cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, lblID.Typeface, FontSize, lFontColour, "CENTER")
 bmpD.Initialize(bmp)
 
 Return bmpD
 
End Sub

But this gives the error: BitmapDrawable cannot be converted to Bitmap in the code in Sub Activity_Create.

Any idea how this could be done?


RBS
 

Star-Dust

Expert
Licensed User
Longtime User
Change the code like this:

B4X:
bmpD.Initialize(cvs.Bitmap)
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
There was another mistake that I had noticed. It must return bitmaps and not BitmapDrawable

This works and I tried it
B4X:
Sub Activity_Create(FirstTime As Boolean)

 bmpPatientMarker.Initialize(TextToBitmap("O", 24, Colors.RGB(255, 0, 0)))

End Sub

Sub TextToBitmap (s As String, FontSize As Float, lFontColour As Long) As Bitmap
 
 Dim bmp As Bitmap
 Dim cvs As Canvas
 
 bmp.InitializeMutable(28dip, 24dip)
 cvs.Initialize2(bmp)
 Dim h As Double = cvs.MeasureStringHeight(s, Typeface.DEFAULT, FontSize)
 cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, Typeface.DEFAULT, FontSize, lFontColour, "CENTER")
 
 Return cvs.Bitmap
 
End Sub
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Got this working now.
It needs be:

B4X:
bmpPatientMarker = TextToBitmapDrawable("O", 24, Colors.RGB(255, 0, 0))


RBS

This is because you were trying to initialize BitmapDrawable with another BitampDrawable, while doing it with a Bitmap.
Now you have assigned the BitmapDrawable of the activity to BitmapDrawable return of the function and so it works well.

But you could also use the second code that I posted to you that return the Bitmap and you can also initialize BitmapDrawable in the activity.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
This is because you were trying to initialize BitmapDrawable with another BitampDrawable, while doing it with a Bitmap.
Now you have assigned the BitmapDrawable of the activity to BitmapDrawable return of the function and so it works well.

But you could also use the second code that I posted to you that return the Bitmap and you can also initialize BitmapDrawable in the activity.

OK, this works now.
I used the same function for menu icons as well and had to adapt that like this:

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
 
    Dim item As ACMenuItem
    Dim oIcon As BitmapDrawable
 
    Menu.Clear
 
    oIcon.Initialize(TextToBitmap("F", 24, Colors.RGB(0, 0, 0)))
    item = Menu.Add2(1, 1, " Find", oIcon)
    item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS

End Sub

As suggested, I changed the function name.


RBS
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
OK, this works now.
I used the same function for menu icons as well and had to adapt that like this:

B4X:
Sub Activity_CreateMenu(Menu As ACMenu)
 
    Dim item As ACMenuItem
    Dim oIcon As BitmapDrawable
 
    Menu.Clear
 
    oIcon.Initialize(TextToBitmap("F", 24, Colors.RGB(0, 0, 0)))
    item = Menu.Add2(1, 1, " Find", oIcon)
    item.ShowAsAction = item.SHOW_AS_ACTION_ALWAYS

End Sub

As suggested, I changed the function name.


RBS

Not sure I need it, but added the bitmap width and height as function arguments:

B4X:
Sub TextToBitmap(s As String, FontSize As Float, lFontColour As Long, iWidth As Int, iHeight As Int) As Bitmap

    Dim bmp As Bitmap
    Dim cvs As Canvas
 
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    cvs.Initialize2(bmp)
    Dim h As Double = cvs.MeasureStringHeight(s, Typeface.DEFAULT, FontSize)
    cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, Typeface.DEFAULT, FontSize, lFontColour, "CENTER")
 
    Return cvs.Bitmap

End Sub


RBS
 
Upvote 0
Top