With the increasing frequency of full browser and full screen Flash sites, many require dynamic content scaling. RatioUtil makes it easy to scale items while maintaining their aspect ratio.
Assuming the Mona Lisa bitmap is 175x150 at its native size and the target bounding box (bounds) is 143x143, here is how scaleToFit would work:
var monaLisaSize:Rectangle = new Rectangle(0, 0, 175, 150); // The Rectangle's x and y values are ignored. var bounds:Rectangle = new Rectangle(0, 0, 143, 143); // The Rectangle's x and y values are ignored. var result:Rectangle = RatioUtil.scaleToFit(monaLisaSize, bounds); monaLisaBitmap.width = result.width; monaLisaBitmap.height = result.height;
Which would look something like this:

If the image needs to completely fill the bounds you would use the scaleToFill method. The code is exactly the same as above, besides the method substitution:
var monaLisaSize:Rectangle = new Rectangle(0, 0, 175, 150); // The Rectangle's x and y values are ignored. var bounds:Rectangle = new Rectangle(0, 0, 143, 143); // The Rectangle's x and y values are ignored. var result:Rectangle = RatioUtil.scaleToFill(monaLisaSize, bounds); monaLisaBitmap.width = result.width; monaLisaBitmap.height = result.height;
The results would look similar to this:

The RatioUtil class has many additional methods not covered in this tip. See the documentation for further information.
Written by Jeffery Nebbett on January 30th, 2009
Used this today for a scrapbooking application. Worked perfectly. Thanks, pal.