Android Question BitmapDrawable cannot be converted to Bitmap

Discussion in 'Android Questions' started by RB Smissaert, Jun 16, 2018.

  1. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Need a circular marker with a specified colour for an Osmdroid markeroverlay and thought I could do it like this:

    Code:
    Sub Globals()
     
     
    Private bmpPatientMarker As BitmapDrawable

    End Sub

    Sub Activity_Create(FirstTime As Boolean)

     bmpPatientMarker.Initialize(TextToBitmapDrawable(
    "O"24Colors.RGB(25500)))

    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(
    28dip24dip)
     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
     
  2. Star-Dust

    Star-Dust Expert Licensed User

    Change the code like this:

    Code:
    bmpD.Initialize(cvs.Bitmap)
     
  3. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Thanks, tried that, but gives me the same error.

    RBS
     
  4. Star-Dust

    Star-Dust Expert Licensed User

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

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

     bmpPatientMarker.Initialize(TextToBitmap(
    "O"24Colors.RGB(25500)))

    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(
    28dip24dip)
     cvs.Initialize2(bmp)
     
    Dim h As Double = cvs.MeasureStringHeight(s, Typeface.DEFAULT, FontSize)
     cvs.DrawText(s, bmp.Width / 
    2, bmp.Height / 2 + h / 2Typeface.DEFAULT, FontSize, lFontColour, "CENTER")
     
     
    Return cvs.Bitmap
     
    End Sub
     
    Last edited: Jun 16, 2018
  5. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

    Got this working now.
    It needs be:

    Code:
    bmpPatientMarker = TextToBitmapDrawable("O"24Colors.RGB(25500))

    RBS
     
  6. Star-Dust

    Star-Dust Expert Licensed 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.
     
  7. DonManfred

    DonManfred Expert Licensed User

    you should rename it to TextToBitmap ;-)
     
    Star-Dust likes this.
  8. Star-Dust

    Star-Dust Expert Licensed User

    Done ;)
     
  9. DonManfred

    DonManfred Expert Licensed User

    Th e example needs to adapted too ;D
     
    Star-Dust likes this.
  10. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

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

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

    End Sub
    As suggested, I changed the function name.


    RBS
     
  11. RB Smissaert

    RB Smissaert Well-Known Member Licensed User

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

    Code:
    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 / 2Typeface.DEFAULT, FontSize, lFontColour, "CENTER")
     
        
    Return cvs.Bitmap

    End Sub


    RBS
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice