Android Question b4xImageView and Invalidate

bocker77

Active Member
Licensed User
Longtime User
I am trying to place numerous bitmaps on a b4xImageView using a touch event from an underlying panel. You use to be able to this, I believe, by invalidating an imageview after each placement. In the AI when doing a search on the web it states that you can invalidate a b4xImageView.

B4X:
B4XImageView1.Bitmap = xui.LoadBitmap(File.DirAssets, "image.png")
B4XImageView1.Invalidate ' Force redraw

But b4xImageView does support an "Invalidate". Also I didn't find any hit in the forum.

How do I go about doing this?

Thanks
 

zed

Well-Known Member
Licensed User
B4XImageView is not designed to display multiple overlapping bitmaps.
It displays only one bitmap at a time, and disabling the ImageView does not force a "repaint" with multiple images as was possible in the past.

The solution would be to create a bitmap “canvas”, draw all your images on it, then assign the result to your B4XImageView.
Exemple:
Sub Process_Globals
    Private xui As XUI
End Sub

Sub Globals
    Private iv As B4XImageView
    Private pnl As Panel
    Private baseBitmap As B4XBitmap
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")

    ' Base bitmap (for example a background image)
    baseBitmap = xui.LoadBitmap(File.DirAssets, "backround.png")

    iv.Bitmap = baseBitmap
End Sub

Sub pnl_Touch (Action As Int, X As Float, Y As Float)
    If Action = pnl.ACTION_DOWN Then
        AddImage(X, Y)
    End If
End Sub

Sub AddImage(X As Float, Y As Float)
    Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirAssets, "icone.png")

    ' Create a temporary canvas
    Dim c As B4XCanvas
    Dim newBmp As B4XBitmap = xui.CreateBitmap(baseBitmap.Width, baseBitmap.Height)
    c.Initialize(newBmp)

    ' Draw base image
    c.DrawBitmap(baseBitmap, 0, 0, baseBitmap.Width, baseBitmap.Height)

    ' Draw the new image
    c.DrawBitmap(bmp, X - bmp.Width/2, Y - bmp.Height/2, bmp.Width, bmp.Height)

    c.Invalidate

    ' Update the base bitmap
    baseBitmap = newBmp

    ' Show result
    iv.Bitmap = baseBitmap
End Sub
untested code
 
Upvote 1

bocker77

Active Member
Licensed User
Longtime User
zed, sorry that I did not reply and thank you sooner but I am in the process of upgrading to a new PC. Getting everything copied over and programs that require activation keys have been taking up most of my time. Still having issues with one program that is driving me nuts.

Anyway again thanks for your response and code.

Greg
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…