Android Question how to redraw an image?

OdyMiles

Member
Licensed User
Hi,

that's what I have

B4X:
Sub zeigegrafik(xkoord As Int, ykoord As Int, thewidth As Int, theheight As Int, name As String)
    ' ------------------------------------------------------------------------------------------------------------------------
    ' Zeigt an x,y an die Grafik <name> an mit den Maßen breite und höhe
    ' ------------------------------------------------------------------------------------------------------------------------
    Dim bild As Bitmap
    Dim imgView1 As ImageView
    bild=LoadBitmap(File.DirAssets,name)                        '3
    imgView1.Initialize(bild)
    Activity.AddView(imgView1, xkoord, ykoord,thewidth,theheight)    'vars are given to sub
    imgView1.Gravity=Gravity.FILL
    imgView1.setBackgroundImage(bild)                       

    'Msgbox("Done","")
End Sub

When called it shows a picture :))
When I call the sub a 2nd time with new coordinates I want to force the former drawn image to disappear. Right now it overlays...
e.g.
run1 show picture1 at position 10,10
run2 show picture2 at position 10,10

Thanks for your ideas.

Chris
 

DonManfred

Expert
Licensed User
Longtime User
I want to force the former drawn image to disappear
then you need to remove the image from the activity first. Or use the same reference instead of creating a new one each time.

What you are doing here i not "drawing" a image at a specific position.
You create an object of type imageview, load a image to it and place the imageview as a new view in the activity.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Two suggestions:
1) Almost like your code.
B4X:
Sub zeigegrafik(xkoord As Int, ykoord As Int, thewidth As Int, theheight As Int, name As String)
    ' ------------------------------------------------------------------------------------------------------------------------
    ' Zeigt an x,y an die Grafik <name> an mit den Maßen breite und höhe
    ' ------------------------------------------------------------------------------------------------------------------------
    If imgView1.IsInitialized Then
        imgView1.RemoveView
    End If
    Dim imgView1 As ImageView                    
    imgView1.Initialize(bild)
    Activity.AddView(imgView1, xkoord, ykoord, thewidth, theheight)    'vars are given to sub
    imgView1.Gravity=Gravity.FILL
    imgView1.Bitmap = LoadBitmap(File.DirAssets,name)                     

    'Msgbox("Done","")
End Sub

2) Or, definig it once
B4X:
Sub zeigegrafik(xkoord As Int, ykoord As Int, thewidth As Int, theheight As Int, name As String)
    ' ------------------------------------------------------------------------------------------------------------------------
    ' Zeigt an x,y an die Grafik <name> an mit den Maßen breite und höhe
    ' ------------------------------------------------------------------------------------------------------------------------
    If imgView1.IsInitialized Then
        imgView1.SetLayout(xkoord, ykoord, thewidth, theheight )
    Else  
        Dim imgView1 As ImageView
        imgView1.Initialize(bild)
        Activity.AddView(imgView1, xkoord, ykoord, thewidth, theheight)    'vars are given to sub
        imgView1.Gravity=Gravity.FILL
    End If
    imgView1.Bitmap = LoadBitmap(File.DirAssets,name)                     

    'Msgbox("Done","")
End Sub
You should set imgView1.Bitmap directly, no need to define a Bitmap each time.
With your code, defining a Bitmap each time, you can get an out of memory error if you call the routine many times.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
When I call the sub a 2nd time with new coordinates I want to force the former drawn image to disappear. Right now it overlays...

this is because you always add a new imageview to the activity in your code:

B4X:
Activity.AddView(imgView1, xkoord, ykoord,thewidth,theheight)

you can set the imageview as a global variable and then change his bitmap whenever you want or you could use canvas to draw a new bitmap to your activity (after you clear the old bitmap)
 
Last edited:
Upvote 0

OdyMiles

Member
Licensed User
Hi Klaus and Ilan,

thanks.
Suggestion one and defining in globals works perfectly.

@klaus: "If imgView1.IsInitialized Then" forces a compiling error due to a missing initialize during the first call..

Danke/Thanks
Chris
 
Upvote 0
Top