iOS Question How to fill and keep aspect ratio?

kompiler

Member
Licensed User
Longtime User
How to fill ImageView and keep aspect ratio?

Erel's code resize ImageView1 fine, but I need fill ImageView 1 and keep the aspect ratio

B4X:
Dim scale As Float = GetDeviceLayoutValues.NonnormalizedScale
ImageView1.Bitmap = LoadBitmapResize(File.DirAssets,"newest_filled.png",ImageView1.Width * scale,ImageView1.Height * scale,True)
 

kompiler

Member
Licensed User
Longtime User
Thank you, the example works fine but the image is of poor quality. Can you change the image quality?

IMG-0059.PNG


IMG-0058.PNG
 
Upvote 0

Semen Matusovskiy

Well-Known Member
Licensed User
It's possible to set ContentMode = MODE_FIT and to resize nothing
B4X:
        imageViewPhoto.ContentMode = imageViewPhoto.MODE_FIT
        imageViewPhoto.Bitmap = LoadBitmap ("directory", "filename.jpg")
 
Upvote 0

kompiler

Member
Licensed User
Longtime User
Thank you for your answer.

Erel, here is the code. The picture is stretched, not filled.
B4X:
Image1.Initialize("Image1")
Image1.Visible=True 
Page1.RootPanel.AddView(Image1, 0,0,100%x,100%y/5)
Dim scale As Float = GetDeviceLayoutValues.NonnormalizedScale
Image1.Bitmap = LoadBitmapResize(File.DirAssets,"apple.png",Image1.Width * scale,Image1.Height * scale,True)

Semen Matusovskiy, MODE_FIT is not filled.

10340.jpg
 
Upvote 0

emexes

Expert
Licensed User
I would usually do this by making the ImageView larger than the screen in one direction (and the same size as the screen in the other direction).

For your example in post #6, which is too short for the screen, I would scale the image up until it was the same height as the screen, which will also make the image wider than the screen, and then center it horizontally with something like:
B4X:
ImageView.Left = -(excess_horizontal_size / 2)
Similar procedure if the image is too narrow for the screen: scale it to be same width as screen, image will then be taller than the screen, center it vertically with eg: ImageView.Top = -(ScreenHeight - ImageHeight) / 2

edit: probably no need to center it - I just did it out of habit
 
Last edited:
Upvote 0

kompiler

Member
Licensed User
Longtime User
I used this solution and it works ok. Thank you for all the answers.

B4X:
Image1.Initialize("Image1")
Image1.Visible=True
   
Dim Image_x As Int=100%x
Dim Image_y As Int=100%y/5
Dim Bitmap_x As Int
Dim Bitmap_y As Int  
Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets,"apple.png")
Bitmap_x=(Bitmap1.Width*Image_y)/Bitmap1.Height
Bitmap_y=Image_y
       
Page1.RootPanel.AddView(Image1,-(Bitmap_x-Image_x)/2,0,Bitmap_x,Bitmap_y)  
Image1.Bitmap = LoadBitmap (File.DirAssets,"apple.png")

19836.jpg
 
Upvote 0
Top