Android Code Snippet FontAwesome To Bitmap

Discussion in 'Code Snippets' started by Erel, Jul 16, 2018.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Unlike an older "TextToBitmap" sub that you can find in the forum, this code vertically centers the icon.
    It can also be used in B4J and B4i (only need to change the font line).
    Code:
    Sub Activity_Create(FirstTime As Boolean)
      
    'usage example
       Activity.AddMenuItem3("Android""Test", FontAwesomeToBitmap(Chr(0xF17B), 28), True)
       
    Activity.AddMenuItem3("Eye""Test", FontAwesomeToBitmap(Chr(0xF06E), 28), True)
    End Sub

    Sub FontAwesomeToBitmap (Text As String, FontSize As Float) As B4XBitmap
       
    Dim xui As XUI
       
    Dim p As Panel = xui.CreatePanel("")
       p.SetLayoutAnimated(
    00032dip32dip)
       
    Dim cvs1 As B4XCanvas
       cvs1.Initialize(p)
       
    Dim fnt As B4XFont = xui.CreateFont(Typeface.FONTAWESOME, 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, xui.Color_White, 
    "CENTER")
       
    Dim b As B4XBitmap = cvs1.CreateBitmap
       cvs1.Release
       
    Return b
    End Sub
    Depends on: XUI

    [​IMG]
     
  2. Filippo

    Filippo Expert Licensed User

    I think that's better. ;)

    Code:
    Public Sub FontToBitmap (text As String, IsMaterialIcons As Boolean, FontSize As Float, color As Int) As B4XBitmap
        
    Dim xui As XUI
        
    Dim p As Panel = xui.CreatePanel("")
        p.SetLayoutAnimated(
    00032dip32dip)
        
    Dim cvs1 As B4XCanvas
        cvs1.Initialize(p)
        
    Dim t As Typeface
        
    If IsMaterialIcons Then t = Typeface.MATERIALICONS Else t = Typeface.FONTAWESOME
        
    Dim fnt As B4XFont = xui.CreateFont(t, 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, color, 
    "CENTER")
        
    Dim b As B4XBitmap = cvs1.CreateBitmap
        cvs1.Release
        
    Return b
    End Sub
     
    AnandGupta, alimanam3386 and Mashiane like this.
  3. Gunther

    Gunther Active Member Licensed User

    ...and the Line with Panel, since in B4J only knowns Pane.;)
    Code:
    Dim p As B4XView = xui.CreatePanel(""' valid for all
     
    Erel and Filippo like this.
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