How To Fit An Image To The Screen While Maintaining Aspect Ratio.

The place to discuss anything and everything about running your LiveCode on Android

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Googie85
Posts: 199
Joined: Tue Aug 05, 2014 10:07 am

How To Fit An Image To The Screen While Maintaining Aspect Ratio.

Post by Googie85 » Mon Jul 05, 2021 3:55 am

Hi Guys!

Just a quick question. As the title reads, I am trying to fit an image to the screen of an Android device and maintain its aspect ratio so as not to distort the image size ratio. I have searched the dictionary and cant seem to find a way to achieve this. If someone could point me in a direction for me to find the information I need (in the dictionary), that would be great!

Many Thanks,

Googie.

Example:
Image

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: How To Fit An Image To The Screen While Maintaining Aspect Ratio.

Post by jmburnod » Mon Jul 05, 2021 9:13 am

Hi,
Here is a post about resize image :D
viewtopic.php?f=8&t=12653&p=60167&hilit=pourcrop#p60167
Best regards
Jean-Marc
https://alternatic.ch

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How To Fit An Image To The Screen While Maintaining Aspect Ratio.

Post by jacque » Mon Jul 05, 2021 8:51 pm

I check the image dimensions to see if I need to resize by height or width (in your example image, you'd want to fit the image by its width.) Then I call one of the following handlers. The params are:
  • pObj = control reference; name or ID or long ID, etc.
  • pOrigImgHeight = the formattedHeight of the image
  • pOrigImgWidth = the formattedWidth of the image

Code: Select all

on resizeToHeight pObj,pOrigImgHeight,pOrigImgWidth
  put (the height of this cd)/pOrigImgHeight into tRatio
  set the rect of pObj to 0,0,(pOrigImgWidth * tRatio),(pOrigImgHeight * tRatio)  
end resizeToHeight

on resizeToWidth pObj,pOrigImgHeight,pOrigImgWidth
  put (the width of this cd)/pOrigImgWidth into tRatio 
  set the rect of pObj to 0,0,(pOrigImgWidth * tRatio),(pOrigImgHeight * tRatio)
end resizeToWidth
This positions the resized image at the top left of the card, which in my case is where I wanted it. If that isn't where you want it, you can move the resized image to its original top left, or to the card loc, or wherever you need it. Or you could adjust the handlers to add the new widths and heights to the original image rect.

It wouldn't be difficult to pass another parameter specifying whether to resize by height or width, and then this could be a single handler instead of two.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Googie85
Posts: 199
Joined: Tue Aug 05, 2014 10:07 am

Re: How To Fit An Image To The Screen While Maintaining Aspect Ratio.

Post by Googie85 » Mon Jul 05, 2021 11:22 pm

Thank you so much Jacque!!! Exactly what I needed! :lol:

Post Reply

Return to “Android Deployment”