B4J Question CreateBitmap Snapshot 1x1 in B4J

IdasI4A

Active Member
Licensed User
Longtime User
This function
B4X:
Sub CreateBitmap(text As String) As B4XBitmap
    Dim lbl As Label
    lbl.Initialize("")
    Dim xlbl As B4XView = lbl
    xlbl.SetLayoutAnimated(0, 0, 0, 200dip, 100dip)
   
    xlbl.Text = text
    xlbl.TextColor = xui.Color_Yellow
    xlbl.SetTextAlignment("CENTER", "CENTER")

    xlbl.SetColorAndBorder(xui.Color_Green, 2dip, xui.Color_Black, 5dip)
    Return xlbl.Snapshot.As(B4XBitmap)

End Sub

B4X:
    Dim B As B4XBitmap=CreateBitmap("Hola")
    Log(B.Width & "x" & B.Height) ' In B4J return 1x1
                                  ' In B4a return 200x100

Why it didn't work in B4J?
Thank you
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use this code:
B4X:
Sub CreateBitmap(text As String) As B4XBitmap
    Dim pnl As B4XView = xui.CreatePanel("")
    pnl.SetLayoutAnimated(0, 0, 0, 300dip, 100dip)
    Dim cvs As B4XCanvas
    cvs.Initialize(pnl)
    Dim CenterX = pnl.Width / 2, CenterY = pnl.Height / 2 As Float
    Dim fnt As B4XFont = xui.CreateDefaultFont(15)
    Dim r As B4XRect = cvs.MeasureText(text, fnt)
    Dim BaseLine As Int = CenterY - r.Height / 2 - r.Top
    cvs.DrawText(text, CenterX, BaseLine, fnt, xui.Color_Black, "CENTER")
    Dim b As B4XBitmap = cvs.CreateBitmap
    Return b.Crop(CenterX - r.Width / 2, CenterY - r.Height / 2, r.Width, r.Height)
End Sub

Based on: https://www.b4x.com/android/forum/t...e-text-measurement-and-drawing.92810/#content
 
Upvote 0

IdasI4A

Active Member
Licensed User
Longtime User
Thanks. But with the first function, I don't have to worry about text alignment or multiline printing. I solve it by putting those features in XLBL.
 
Upvote 0

IdasI4A

Active Member
Licensed User
Longtime User
In B4A I get
1748767190280.png

But, in B4J
1748767260585.png



Because?
 

Attachments

  • EjemploCreateBitmapB4A.zip
    10.4 KB · Views: 111
  • EjemploCreateBitmapB4J.zip
    3.3 KB · Views: 119
Upvote 0

teddybear

Well-Known Member
Licensed User
Why it didn't work in B4J?
Thank you
In B4J, you need to add a B4XView pane for the label instead of SetLayoutAnimated.
 

Attachments

  • EjemploCreateBitmap.zip
    92.1 KB · Views: 110
Last edited:
Upvote 0
Top