Android Question ListView TwoLinesAndBitmap ImageView does not scale images right?

ronovar

Active Member
Licensed User
Longtime User
I im using ListView and added this:

B4X:
    lvChannels.TwoLinesAndBitmap.ImageView.Height = 37dip
    lvChannels.TwoLinesAndBitmap.ImageView.Width  = 37dip
    lvChannels.TwoLinesAndBitmap.ImageView.Top = 0dip
    lvChannels.TwoLinesAndBitmap.ImageView.Left = 54dip
    lvChannels.TwoLinesAndBitmap.ImageView.Gravity = Gravity.FILL

When i set bitmap using:

B4X:
lvChannels.AddTwoLinesAndBitmap2(Cursor2.GetString("tvg_name"), i+1, bmpChLogo, i)

Bitmaps in ListView are not filled (scalled to fit its imageview size)....how can fit bitmaps to scale defined ImageView width and height?
 

ronovar

Active Member
Licensed User
Longtime User
I have around 100 bitmaps in sqlite3 database...and they each one have different width and height...i im using for to next loop to read bitmaps(blob) from database and add to listview addtwolinesandbitmap...but first three items are scalled (fill) to desired size...and others 97 items are big images and not scalled....what i need to add to code to make all different bitmal items (different width and height) to be same filled for all items?
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Thanks..i use your solution (slight modified) and i get bitmap below bitmap - two bitmap in listview imageview...how to clear previus bitmap when reading next bitmap?

B4X:
Sub FitCenterBitmap(Imv As ImageView, Bmp As Bitmap) As Bitmap
    Private cvs As Canvas
    cvs.Initialize(Imv)
   
    Dim rectDest As Rect
    Dim delta As Int
    If Bmp.Width / Bmp.Height > Imv.Width / Imv.Height Then
        delta = (Imv.Height - Bmp.Height / Bmp.Width * Imv.Width) / 2
        rectDest.Initialize(0, delta,Imv.Width, Imv.Height - delta)
    Else
        delta = (Imv.Width - Bmp.Width / Bmp.Height * Imv.Height) / 2
        rectDest.Initialize(delta, 0, Imv.Width - delta, Imv.Height)
    End If
    cvs.DrawBitmap(Bmp, Null, rectDest)
    Imv.Invalidate
   
    Return cvs.Bitmap
End Sub

And Calling function:

B4X:
ListView1.AddTwoLinesAndBitmap2("Text 1", i+1, FitCenterBitmap(ListView1.TwoLinesAndBitmap.ImageView, bmpChLogo), "ReturnValue")
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
ImageView.Background = Null

Thanks...you save the day:)

I was studiy yesterday why not all images are not scalled in listview imageview..so i then see that one image is: 86x66px (1,30 ratio) another is 50x35 (Ratio 1,42) so now it is all centered and scalled...thank you.
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Just one more question..if it can be done?

I noticed that bitmap in imageview lost quality (images loss sharpness and anti aliasing)....is this because i use canvas? does canvas have quality settings?
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
https://www.b4x.com/android/forum/threads/how-to-set-anti_alias_flag.14579/#post-298936


B4X:
Private Const FILTER_BITMAP_FLAG As Int = 2

SetAntiAlias(cvs, FILTER_BITMAP_FLAG)


Sub SetAntiAlias (c As Canvas, FilterFlag As Int)
   Dim r As Reflector
   Dim NativeCanvas As Object
   r.Target = c
   NativeCanvas = r.GetField("canvas")
   Dim PaintFlagsDrawFilter As Object
   PaintFlagsDrawFilter = r.CreateObject2("android.graphics.PaintFlagsDrawFilter", _
      Array As Object(0, FilterFlag), Array As String("java.lang.int", "java.lang.int"))
   r.Target = NativeCanvas
   r.RunMethod4("setDrawFilter", Array As Object(PaintFlagsDrawFilter), Array As String("android.graphics.DrawFilter"))
End Sub
 
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Hi.

I have using listview and have TwoLinesAndBitmap to add items to my listview. Problem is that i have multiple images in listview that bitmap have different width (height is fixed at 25dip)..and i im unable to center bitmaps in listview and that bitmap is not resized to imageview width and height so that bitmap in imageview looks like original image without cropping scalling and other stuff.

How can i do that? I try but i did not get what i need....maybe if i can disable using java object in listview imageview fill image?

Here i im attaching screenshoot what i get and source code in B4A.

Thanks.

For Example:

First Item Bitmap Width is: 84dip, and Height : 25dip i get in ImageView Width: 37dip and Height: 25dip

Second Item Bitmap Width is: 42dip and Height: 25dip i get in ImageView Width: 37dip and Height: 25dip

Third Item Bitmap Width is: 37dip and Height: 25dip i get in ImageView Width: 37dip and Height: 25dip

So conclusion is that imageview use last width and height from Bitmal Width And Height and ignores above two width and height from Bitmap.

So question is how can i set listview image view to be for each item different Width And Height according for Bitmap.

For example i need in this above example listview image view widh and height to be:

First Item: W: 84dip H: 25dip
Second Item: W: 42dip H: 25dip
Third Item: W: 37dip H: 25dip
 

Attachments

  • listview resize.png
    listview resize.png
    51.5 KB · Views: 463
  • ListView1.zip
    15.3 KB · Views: 328
Last edited:
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Thanks LucaMs this code works excellent if i don't use ImageView inside ListView...but i need this code to work inside ListView...try with ListView1.TwoLinesAndBitmap.ImageView and you will get filled big images inside that ImageView...i found problem and don't know how to solve it...the problem is when you first time set ListView1.TwoLinesAndBitmap.ImageView.Width and ListView1.TwoLinesAndBitmap.ImageView.Height you can't change it later and all images uses first defined ListView1.TwoLinesAndBitmap.ImageView.Width and ListView1.TwoLinesAndBitmap.ImageView.Height for all images that have different width and height for example see this code:

B4X:
'ADD - Items
    For i = 1 To 10
        'LOAD - Bmp1
        Bmp1 = LoadBitmap(File.DirAssets, i&".png")
       
        'SET - ListView Width & Height corresponding to Bmp1 Width & Height
        'ListView1.TwoLinesAndBitmap.ImageView.Width  = Bmp1.Width
        'ListView1.TwoLinesAndBitmap.ImageView.Height = Bmp1.Height

        'CENTER - ImageView corresponding to Bmp1 Height
        'ListView1.TwoLinesAndBitmap.ImageView.Top = (ListView1.TwoLinesAndBitmap.ImageView.Height - Bmp1.Height) / 2

        'ADD - TwoLinesAndBitmap To ListView1
        ListView1.AddTwoLinesAndBitmap("First Item #"&i, "Logo Width: "&Bmp1.Width&" | Height: "&Bmp1.Height, Bmp1)
    Next
   
    'ADD - Activity
    Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
End Sub

So in this example first time ListView1.TwoLinesAndBitmap.ImageView Width is set to: 48dip, and Height to 25dip and imageview uses this correctly...second time it sets it to 36dip and 25dip and imageview width is always 48dip width and 25dip height...so need solutio to change in for to loop imageview width and height.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Sorry but I'm reading quickly (I can't find a bug in my project and... :mad:)

If I "guess" well you should create an "invisible" imageview (ivInvisible), same size of the ListView's imageview, then load your bitmpa into it using that routine (FitCenterBitmap), take its bitmap and pass it to the ListView item.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Sorry but I'm reading quickly (I can't find a bug in my project and... :mad:)

If I "guess" well you should create an "invisible" imageview (ivInvisible), same size of the ListView's imageview, then load your bitmpa into it using that routine (FitCenterBitmap), take its bitmap and pass it to the ListView item.
 

Attachments

  • ListViewBitmaps.zip
    25.1 KB · Views: 375
Upvote 0

ronovar

Active Member
Licensed User
Longtime User
Thanks...this is very close for what i need..i tried your solution and works excellent...just need to make some small modigication....so i will try to explain more detail so that you can undestand what i need to get from image view:

I defined:

ImageView.Width = 64dip
ImageView.Height = 36dip

And need when loading bitmap inside that imageview that bitmap is centered inside that imageview and width and height of bitmap is not changed. So here is example:

I put first bitmap inside imageview and bitmap have:

Width: 50dip
Height: 25dip

and that bitmap is showed centered inside ImageView with its original size.

Second bitmap inside imageview have:

Width: 60dip
Height: 30dip

and that bitmap is also showed centerd inside ImageView with its original size.

So i don't need cropping or upscaling images in ImageView..i need just to show centered bitmap with original size od bitmap width and height inside image view...and that's all.

Thank you very much for posting this code witch works excellent but i was unclear in above post what i need to get...so if you can make small changes to code to get above example i will be very thankfull.

Many thanks.
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    19.3 KB · Views: 392
Upvote 0
Top