Android Tutorial Fill or Fit image to ImageView

Brandsum

Well-Known Member
Licensed User
Hi,

Here is how you can set the scale mode of an ImageView like CSS background-size property.

B4X:
'scale = cover,contain,fill
Public Sub SetImageView(iv As ImageView,bmp As Bitmap, scale As String)
    Dim jo As JavaObject=iv
    jo.RunMethod("setImageBitmap",Array(bmp))
    Select scale
        Case "cover": jo.RunMethod("setScaleType",Array("CENTER_CROP"))
        Case "contain": jo.RunMethod("setScaleType",Array("CENTER_INSIDE"))
        Case "fill": jo.RunMethod("setScaleType",Array("FILL_XY"))
    End Select
End Sub
Usage:
B4X:
Dim bmp as bitmap = LoadBitmap(File.DirAssets,"img.jpg")
Dim iv As ImageView
iv.Initialize("")
SetImageView(iv,bmp,"cover")
If you want to set fill/fit image to view's background then use this method by @Erel
https://www.b4x.com/android/forum/threads/b4x-xui-fill-and-fit-images-without-distortion.86627/
 
Top