Android Question Fit An Image

Ionut Indigo

Member
Licensed User
Longtime User
Hello,
I'm having some issues with stretching/fitting an image in an ImageView. Also i'm using LoadBitmapSample to load it. I need them to nicely in BetterSlidingPanels.

Here is my code:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim panels(15) As Panel
    For i = 0 To panels.Length - 1
        panels(i).Initialize("panels")
        Dim imga As ImageView
        imga.Initialize("")       
        imga.Bitmap = LoadBitmapSample(File.DirAssets, "picture" & i & ".jpg", 100%x, 100%y)
        imga.Gravity = Gravity.Center
        panels(i).AddView(imga,0,0,100%x,100%y)
        Activity.AddView(panels(i), 100%x, 0, 100%x, 100%y)
    Next

    ActiveBitmap.Initialize(File.DirAssets, "indicator_active.png")
    InactiveBitmap.Initialize(File.DirAssets, "indicator_inactive.png")
   
    sd.Initialize
    SlidingPanels.Initialize(sd, panels, True, 50)
    sd.currentPanel = currentPanelBeforePaused - 1
   
    Indicator = SlidingPanels.CreatePageIndicator(panels.Length, InactiveBitmap, 120dip, 16dip)
    Activity.AddView(Indicator, (100%x - 120dip) / 2, 0, 120dip, 16dip)
    Indicator.BringToFront
    ChangePanel(0)
End Sub

Right now with Gravity.Center i get this:
001_now.jpg
But i want to lose the top and bottom blank space.
And with Gravity.Fill i get this:
002_grafityfill.jpg
And that distorts/stretches the image, and it doesn't look natural.
I want to fit it like this:
003_want.jpg
Not sure if the Gravity can help, but 100%x and 100%y doesn't make the image fit on the screen.
 

strat

Active Member
Licensed User
Longtime User
vzjNNr.jpg


I don't know there is a simpler way or not so I tried to explain how you will handle this.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Try this:
B4X:
For i = 0 To panels.Length - 1
  panels(i).Initialize("panels")
  Dim imga As ImageView
  imga.Initialize("")     
  imga.Bitmap = LoadBitmapSample(File.DirAssets, "picture" & i & ".jpg", 100%x, 133%x)
  imga.Gravity = Gravity.Center
  Activity.AddView(panels(i), 100%x, 0, 100%x, 133%x)
  panels(i).AddView(imga,0,0,100%x,133%x)
Next
As the Height/Width ratio is 1024 / 768 = 1.33
Setting the height of the views to 133%x maintains the Height/Width ratio.
Perhaps there are cases where 133%x is too high.
In this case you could keep the height = 100%y but set the width = 75%y
 
Upvote 0

Ionut Indigo

Member
Licensed User
Longtime User
Hey Klaus,
I have done some testing before with your suggestion, it work and not.
Not all my images are 1024x768, and with other sizes there will be some cropping, and because i have a lot of picture (15) i get Downsampling image due to lack of memory and some images are super small.
I'll be trying Informatix library.
 
Upvote 0
Top