Android Tutorial Fill or Fit image to ImageView

Discussion in 'Tutorials & Examples' started by Brandsum, Dec 26, 2018.

  1. Brandsum

    Brandsum Member Licensed User

    Hi,

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

    Code:
    '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:
    Code:
    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/
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice