Android Question Scale & Crop Bitmap to Fill ImageView

Peter Simpson

Expert
Licensed User
Longtime User
B4X:
'CREATE NEW SCALED BITMAP
Sub ScaleBitmap(Image As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
    Dim JO As JavaObject
    Dim BmpScales As Bitmap
        BmpScales = JO.InitializeStatic("android.graphics.Bitmap").RunMethod("createScaledBitmap", _
        Array As Object(Image, Width, Height, Filter))
    Return BmpScales
End Sub
 
Upvote 0

microcacti

New Member
Licensed User
Longtime User
That just creates a scaled bitmap and distorts it when trying to fit into a imageview with a different aspect ratio.
I would like the bitmap to keep it's image properly, and crop out the excess area on the dimension that is too big.

I have solved it by reversing the statement in the FitCenterBitmap in the other thread

B4X:
Sub FitCenterBitmapBMPzoom(Imv As ImageView, bmp 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.Width - bmp.Width / bmp.Height * Imv.Height) / 2
        rectDest.Initialize(delta, 0, Imv.Width - delta, Imv.Height)
    Else
        delta = (Imv.Height - bmp.Height / bmp.Width * Imv.Width) / 2
        rectDest.Initialize(0, delta,Imv.Width, Imv.Height - delta)
    End If
    cvs.DrawBitmap(bmp, Null, rectDest)
    Imv.Invalidate
End Sub
 
Last edited:
Upvote 0
Top