Android Question Clearing an imv, again

HARRY

Active Member
Licensed User
Longtime User
Hallo,

With the following code I draw 3 small images into three imv's. Maybe the code is too complicated, but it works. Because of the variation in the width/height ratio of the images the imv's do not always have the same width.

So, when an image must be displayed with another width/height ratio the existing imv must be destroyed and a new one with the required dimensions must be created. Otherwise part of the larger previous image stays visible. I tried it with removing the 'old' imv and initializing en adding a new one, but it doesn't work; part of the old one stays visible.


Can somebody help me?
B4X:
Dim imvWidth As Int
Dim imvLeft As Int
Dim bmpPhoto As Bitmap
Dim csvPhoto As Canvas
Dim imvPhoto1,imvPhoto2, imvPhoto3 As ImageView
Dim rectPhoto, rectView As Rect
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg") Then
          bmpPhoto.Initialize("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg")
          imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth       
        imvPhoto1.Initialize("")
        imvPhoto1.Bitmap=bmpPhoto               
        Activity.AddView(imvPhoto1,imvLeft,5,imvWidth,155)       
        csvPhoto.Initialize(imvPhoto1)
        rectPhoto.Initialize(0,0,bmpPhoto.width,bmpPhoto.height)
        rectView.Initialize(0,0,imvWidth,155)
        csvPhoto.DrawBitmap(bmpPhoto,rectPhoto, rectView)
       
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg") Then
          bmpPhoto.Initialize("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg")
        imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto2.Initialize("")
        imvPhoto2.Bitmap=bmpPhoto
        Activity.AddView(imvPhoto2,imvLeft,166,imvWidth,155)
        csvPhoto.Initialize(imvPhoto2)
        rectPhoto.Initialize(0,0,bmpPhoto.width,bmpPhoto.height)
        rectView.Initialize(0,0,imvWidth,155)
        csvPhoto.DrawBitmap(bmpPhoto,rectPhoto, rectView)
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg") Then
          bmpPhoto.Initialize("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg")
      imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto3.Initialize("")
        imvPhoto3.Bitmap=bmpPhoto
        Activity.AddView(imvPhoto3,imvLeft,326,imvWidth,155)
        csvPhoto.Initialize(imvPhoto3)
        rectPhoto.Initialize(0,0,bmpPhoto.width,bmpPhoto.height)
        rectView.Initialize(0,0,imvWidth,155)
        csvPhoto.DrawBitmap(bmpPhoto,rectPhoto, rectView)
    End If
   
End Sub
 

HARRY

Active Member
Licensed User
Longtime User
Hallo Erel,

Each time I want to show three small images. I agree, the coding could be improved. There is at this moment no coding to remove previous images as I cannot find how to do so. I tried removing the ImageViews and recreating those, but the images stay on the Activity and the larger ones are only partly overwritten. I also tried to overwrite the area where the three images are shown, by writing an rectangle in the background color, but without success. Removing is necessary anyhow, because sometimes not all three images are available.


Harry
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
So, when an image must be displayed with another width/height ratio the existing imv must be destroyed and a new one with the required dimensions must be created. Otherwise part of the larger previous image stays visible. I tried it with removing the 'old' imv and initializing en adding a new one, but it doesn't work; part of the old one stays visible.


Can somebody help me?
B4X:
Dim imvWidth As Int
Dim imvLeft As Int
Dim bmpPhoto As Bitmap
Dim csvPhoto As Canvas
Dim imvPhoto1,imvPhoto2, imvPhoto3 As ImageView
Dim rectPhoto, rectView As Rect
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg") Then
          bmpPhoto.Initialize("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg")
          imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth      
        imvPhoto1.Initialize("")
        imvPhoto1.Bitmap=bmpPhoto              
        Activity.AddView(imvPhoto1,imvLeft,5,imvWidth,155)      
        csvPhoto.Initialize(imvPhoto1)
        rectPhoto.Initialize(0,0,bmpPhoto.width,bmpPhoto.height)
        rectView.Initialize(0,0,imvWidth,155)
        csvPhoto.DrawBitmap(bmpPhoto,rectPhoto, rectView)
      
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg") Then
          bmpPhoto.Initialize("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg")
        imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto2.Initialize("")
        imvPhoto2.Bitmap=bmpPhoto
        Activity.AddView(imvPhoto2,imvLeft,166,imvWidth,155)
        csvPhoto.Initialize(imvPhoto2)
        rectPhoto.Initialize(0,0,bmpPhoto.width,bmpPhoto.height)
        rectView.Initialize(0,0,imvWidth,155)
        csvPhoto.DrawBitmap(bmpPhoto,rectPhoto, rectView)
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg") Then
          bmpPhoto.Initialize("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg")
      imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto3.Initialize("")
        imvPhoto3.Bitmap=bmpPhoto
        Activity.AddView(imvPhoto3,imvLeft,326,imvWidth,155)
        csvPhoto.Initialize(imvPhoto3)
        rectPhoto.Initialize(0,0,bmpPhoto.width,bmpPhoto.height)
        rectView.Initialize(0,0,imvWidth,155)
        csvPhoto.DrawBitmap(bmpPhoto,rectPhoto, rectView)
    End If
  
End Sub
1) In your code, csvPhoto.Initialize and the following lines using this canvas are useless. The image is already loaded in the ImageView by setting the bitmap property. To adjust its display size, add imvPhoton.Gravity = Gravity.FILL.
2) You should not load your bitmaps with bmpPhoto.Initialize, but with InitializeSample to save some memory.
3) You should not use pixels for sizes, but dips (e.g. 155dip) to take the density of your device into account.
4) To remove an ImageView, call imvPhoton.RemoveView.
 
Upvote 0

HARRY

Active Member
Licensed User
Longtime User
Hi Informatix,
Thanks for your suggestions. Apart from 3) I have implemented those. Now the code looks as shown below. Much cleaner, but................... the larger images stay partly visible behind the smaller ones. Am I still doing something wrong? Could you have as second look?


B4X:
Sub ShowPhotos
Dim imvWidth As Int
Dim imvLeft As Int
Dim bmpPhoto As Bitmap
Dim imvPhoto1,imvPhoto2, imvPhoto3 As ImageView
    imvPhoto1.Initialize ("")
    imvPhoto2.Initialize ("")
    imvPhoto3.Initialize ("")
    Activity.Invalidate
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg") Then
          bmpPhoto.InitializeSample("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg",400,400)
          imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto1.RemoveView
        imvPhoto1.Initialize("")
        imvPhoto1.Bitmap=bmpPhoto
        imvPhoto1.Gravity=Gravity.Fill
        Activity.AddView(imvPhoto1,imvLeft,5,imvWidth,155)                       
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg") Then
          bmpPhoto.InitializeSample("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg",400,400)
        imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto2.RemoveView
        imvPhoto2.Initialize("")
        imvPhoto2.Bitmap=bmpPhoto
        imvPhoto2.Gravity=Gravity.Fill
        Activity.AddView(imvPhoto2,imvLeft,166,imvWidth,155)
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg") Then
          bmpPhoto.InitializeSample("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg",400,400)
          imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto3.RemoveView
        imvPhoto3.Initialize("")
        imvPhoto3.Bitmap=bmpPhoto
        imvPhoto3.Gravity=Gravity.Fill
        Activity.AddView(imvPhoto3,imvLeft,326,imvWidth,155)
    End If   
End Sub

Regards,

Harry
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Hi Informatix,
Thanks for your suggestions. Apart from 3) I have implemented those. Now the code looks as shown below. Much cleaner, but................... the larger images stay partly visible behind the smaller ones. Am I still doing something wrong? Could you have as second look?


B4X:
Sub ShowPhotos
Dim imvWidth As Int
Dim imvLeft As Int
Dim bmpPhoto As Bitmap
Dim imvPhoto1,imvPhoto2, imvPhoto3 As ImageView
    imvPhoto1.Initialize ("")
    imvPhoto2.Initialize ("")
    imvPhoto3.Initialize ("")
    Activity.Invalidate
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg") Then
          bmpPhoto.InitializeSample("/storage/extSdCard/POI-PHOTOS/",CurPhoto1 & ".jpg",400,400)
          imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto1.RemoveView
        imvPhoto1.Initialize("")
        imvPhoto1.Bitmap=bmpPhoto
        imvPhoto1.Gravity=Gravity.Fill
        Activity.AddView(imvPhoto1,imvLeft,5,imvWidth,155)                     
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg") Then
          bmpPhoto.InitializeSample("/storage/extSdCard/POI-PHOTOS/",CurPhoto2 & ".jpg",400,400)
        imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto2.RemoveView
        imvPhoto2.Initialize("")
        imvPhoto2.Bitmap=bmpPhoto
        imvPhoto2.Gravity=Gravity.Fill
        Activity.AddView(imvPhoto2,imvLeft,166,imvWidth,155)
    End If
    If File.Exists("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg") Then
          bmpPhoto.InitializeSample("/storage/extSdCard/POI-PHOTOS/",CurPhoto3 & ".jpg",400,400)
          imvWidth=(bmpPhoto.Width/bmpPhoto.height) * 155
        imvLeft=960-imvWidth
        imvPhoto3.RemoveView
        imvPhoto3.Initialize("")
        imvPhoto3.Bitmap=bmpPhoto
        imvPhoto3.Gravity=Gravity.Fill
        Activity.AddView(imvPhoto3,imvLeft,326,imvWidth,155)
    End If 
End Sub

Regards,

Harry
Without the other parts of the code, I cannot say where you do something wrong. But as you instantiate your ImageViews (Dim ...) in the ShowPhotos sub, they are local instances and you cannot remove them in another sub (except if you call Activity.RemoveAllViews). You should move their Dim in Globals so they become global instances.
 
Upvote 0

HARRY

Active Member
Licensed User
Longtime User
Hi,

I succeeded in removing the ImageViews with the following code:

B4X:
For i= Activity.NumberOfViews-1 To 0 Step -1
        Dim v As View
        v=Activity.GetView(i)
        If v.IsInitialized Then
            If v.Tag <> Null AND  v.Tag="Photo" Then
                Activity.RemoveViewAt(i)
            End If   
        End If   
    Next

I found that piece of code somewhere on this forum and it works well.

Problem solved.

Harry
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Hi,

I succeeded in removing the ImageViews with the following code:

B4X:
For i= Activity.NumberOfViews-1 To 0 Step -1
        Dim v As View
        v=Activity.GetView(i)
        If v.IsInitialized Then
            If v.Tag <> Null AND  v.Tag="Photo" Then
                Activity.RemoveViewAt(i)
            End If
        End If
    Next

I found that piece of code somewhere on this forum and it works well.

Problem solved.

Harry
Yes, but it's a lot easier to call the RemoveView method of the ImageViews:
B4X:
'In Globals:
Dim imvPhoto(3) As ImageView

'To remove them:
For i = 0 to 2
    imvPhoto(i).RemoveView
Next
 
Upvote 0
Top