In my app I load an image into an image view that has a width of 268 and a height of 160. Also if the user clicks the image it shows a full screen version. However users are reporting that their images are distorted.
Thank you sorex, Whats the best way to calculate the aspect ratio? also how would I get it to keep its ratio in an image view or when I display full screen?
I don't even know how to get the dimensions of a picture in b4A, lol.
but it should be something like this
B4X:
ratio=(100%x)/(100%y)
if 100%x > 100%y then
image.width=100%x
image.height=image.height*ratio
else
image.width=image.width*ratio
image.height=100%y
end if
maybe there is a library that does this for you, don't know about that.
at second thought it might've been this
B4X:
ratio=(100%x)/(100%y)
if 100%x > 100%y then
image.width=100%x
image.height=(100%x)*ratio
else
image.width=(100%y)*ratio
image.height=100%y
end if
Thank you both for the responses. Sorry it has taken me so long to get back to you on my progress with this issue. You both have a been a huge help and I think I was able to come up with the right fix. The images that are loaded are always taken in landscape mode so I ended up with code looking like this...
B4X:
Dim AddImage As Bitmap
AddImage = LoadBitmap(File.DirRootExternal, DisplayText.text & ".jpg")
Dim OrigWidth
Dim OrigHeight
Dim NewHeight
OrigWidth = AddImage.Width
OrigHeight = AddImage.Height
NewHeight = OrigHeight / OrigWidth * ImagePreview.Width
AddImage = CreateScaledBitmap(AddImage3,ImagePreview.Width,NewHeight)
ImagePreview.Bitmap = (AddImage)
I don't get any errors and the images fill the ImageView and seem to display without any noticeable distortion on my phone however Ive never really encountered the issue, I have only heard reports of it. So would this code get the job done? Also in order to get the code to work I had to take out the "LoadBitmapSample" part when loading the image. Will loading images without that still be ok? or will it cause issues?
There's a better approach. By using the Accelerated Surface library, you can know the size of your images WITHOUT loading them (no risk of OutOfMemory with large images). And you can resample and rescale them while you load them with LoadScaledBitmap. Example:
B4X:
Dim IU As AS_ImageUtils
Dim mapSize As Map = IU.GetImageDimensions(File.DirRootExternal, DisplayText.text & ".jpg")
Dim NewHeight As Int = mapSize.Get("height") / mapSize.Get("width") * ImagePreview.Width
ImagePreview.Bitmap = IU.LoadScaledBitmap(File.DirRootExternal, DisplayText.text, ImagePreview.Height, ImagePreview.Width, True)
In case of problem, the function traps the OutOfMemory error and returns it as an exception.
Very simply, thank you so much Informatix!
I replaced my original code with the one you listed above and the images are loading with no issues. At first I was getting a an "Invalid Double" error but I removed the following line..
B4X:
Dim NewHeight = mapSize.Get("height") / mapSize.Get("width") *ImagePreview.Width
and it started working.
I don't really need that line since this line does the work right?
Very simply, thank you so much Informatix!
I replaced my original code with the one you listed above and the images are loading with no issues. At first I was getting a an "Invalid Double" error but I removed the following line..
B4X:
Dim NewHeight = mapSize.Get("height") / mapSize.Get("width") *ImagePreview.Width
and it started working.
I don't really need that line since this line does the work right?
I didn't test my code so there's maybe an error with the mapSize.Get in Dim NewHeight. The reason of this line is to compute the height as in your example. I don't know whether it's useful or not. It's you who decide. If you don't need it, then you can remove also the call to GetImageDimensions.
Well I tried getting the height before in an attempted to make sure images display with the correct aspect ratio to eliminate any image distortion.
So does "IU.LoadScaledBitmap" do this for me and make sure images display right across all screen ratios?