Android Question Chat Example - BCTextEngine

Lucas Eduardo

Active Member
Licensed User
I did not understude how to put the image in the list of "Runs"

I tryed the code below, but with not success. What type should the image have?

B4X:
Dim imgRun As B4XBitmap = LoadBitmapResize(File.DirAssets, "imagemArquivo.png", 15dip, 15dip, True)
Dim TextRun As BCTextRun = Engine.CreateRun(Text & CRLF)
TextRun.TextFont = xui.CreateFont(fontRegular,14)
Dim time As BCTextRun = Engine.CreateRun(hora)
time.TextFont = xui.CreateFont(fontRegular,10)
If Right Then time.TextColor = 0xFFF1F1F1 Else time.TextColor = xui.Color_Gray

Return Array(imgRun, TextRun, time)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Lucas Eduardo

Active Member
Licensed User
I'm forgetting something, all other tags works, but tag "[img" does not work. Maybe something after i call
B4X:
BBCodeView1.Text = BuildMessage(Text, Right, timeParsed)

That's my BuildMessage:

B4X:
Dim msg As String
msg = $"[img FileName="imagemArquivo.png" width=15/] [font=fontRegular size=14]${Text}[/font] [font=fontRegular size=10]${CRLF & hora}[/font]"$
Return msg

The code after i call "BuildMessage" it's equals from original example

B4X:
Dim ivText As B4XView = CreateImageView
'get the bitmap from BBCodeView1 foreground layer.
Dim bmpText As B4XBitmap = GetBitmap(BBCodeView1.ForegroundImageView)
'the image might be scaled by Engine.mScale. The "correct" dimensions are:
Dim TextWidth As Int = bmpText.Width / Engine.mScale
Dim TextHeight As Int = bmpText.Height / Engine.mScale
'bc is not really used here. Only the utility method.
bc.SetBitmapToImageView(bmpText, ivText)
Dim ivBG As B4XView = CreateImageView
'Draw the bubble.
Dim bmpBG As B4XBitmap = DrawBubble(TextWidth, TextHeight, Right)
bc.SetBitmapToImageView(bmpBG, ivBG)
p.SetLayoutAnimated(0, 0, 0, CLV.sv.ScrollViewContentWidth - 2dip, TextHeight + 3 * Gap)
'2020-01-16 16:54:29
DateTime.DateFormat = "yyyy-MM-dd HH:mm:ss"
If Right Then
        p.AddView(ivBG, p.Width - bmpBG.Width * xui.Scale, Gap, bmpBG.Width * xui.Scale, bmpBG.Height * xui.Scale) 'BALÃO
        p.AddView(ivText, p.Width - Gap - ArrowWidth - TextWidth, 2 * Gap, TextWidth, TextHeight) 'TEXTO
...
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
I played around with this and came up with

B4X:
    'BBCodeView1.ExternalRuns = BuildMessage(Text, User)
    'BBCodeView1.ParseAndDraw

    BBCodeView1.Text = $"[img FileName="myimg.png" width=50/][b]${User}[/b]
    [Alignment=Left]${Text}[/Alignment]
    [TextSize=10][Color=#999999]${DateTime.Time(DateTime.Now)}[/Color][/TextSize]
    "$
   
    Dim bmpText As B4XBitmap = GetBitmap(BBCodeView1.ForegroundImageView)
    'get the bitmap from BBCodeView1 foreground layer for dimensions.
    'the image might be scaled by Engine.mScale. The "correct" dimensions are:
    Dim TextWidth As Int = 50 + bmpText.Width / Engine.mScale
    Dim TextHeight As Int = - 10 + bmpText.Height / Engine.mScale

    'Now get the whole image
    Dim bmpAll As B4XBitmap = BBCodeView1.mBase.Snapshot
    bmpText = bmpAll.Crop(0, 0, TextWidth, TextHeight)

The reason I had to take a snapshot was that the ForegroundImageView did not show the image.
Also I had to remove the border from the BBCodeView1 in designer.


screenShot.png
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Right, I had to tweek the horizontal and vertical size (I don't know why). You can change the numbers (50 and -10) to dip values or whatever you need.


B4X:
    Dim TextWidth As Int = 50 + bmpText.Width / Engine.mScale
    Dim TextHeight As Int = - 10 + bmpText.Height / Engine.mScale
 
Upvote 0
Top