Android Question Fit image on screen (portrait or landscape)

BarryW

Active Member
Licensed User
Longtime User
Hi can anyone help to to scale any image to fit on my screen without stretching or distorting this image.
This is like the betterimageview but this time i want to get the new fit size of the image in my screen..
I want my image fit in my screen the get its height and width for creating panel

B4X:
Sub Globals
    Dim Panel_Image As Panel
    Dim Bitmap1 As Bitmap
    Dim New_Height, New_Width As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Load image to bitmap1
    Bitmap1.Initialize(File.DirAssets, "Any Image.jpg")
  
    'Scale height and width to fit image in screen without stretching
    If Bitmap1.Height > 100%y Then
        New_Height = 100%y
        New_Width = Bitmap1.Width - (Bitmap1.Height - 100%y)
    Else
        New_Height = Bitmap1.Height
        New_Width = Bitmap1.Width
    End If
  
    If New_Width > 100%x Then
        New_Height = New_Height - (New_Width - 100%y)
        New_Width = 100%x
    End If
  
    'Add panel to activity
    Panel_Image.Initialize("")
    Activity.AddView(Panel_Image, 0, 0, New_Width, New_Height)
  
    'Set panel background
    Panel_Image.SetBackgroundImage(Bitmap1)
  
    'Center panel in activity
    Panel_Image.Top = 50%y - (Panel_Image.Height / 2)
    Panel_Image.Left = 50%x - (Panel_Image.Width / 2)
End Sub
 
Top