Skip to content

Instantly share code, notes, and snippets.

@leeprobert
Created December 6, 2011 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeprobert/1437632 to your computer and use it in GitHub Desktop.
Save leeprobert/1437632 to your computer and use it in GitHub Desktop.
fit a bitmap image to its container regardless of aspect ratio
protected function getNewBitmap(data:BitmapData):Bitmap
{
var horizontalRatio:Number = this.width / data.width;
var verticalRatio:Number = this.height / data.height;
var ratio:Number = Math.max(horizontalRatio,verticalRatio);
var matrix:Matrix = new Matrix();
matrix.scale(ratio,ratio);
var newBitmapData:BitmapData;
var adjustedWidth:Number = data.width*ratio;
var adjustedHeight:Number = data.height*ratio;
matrix.translate((this.width-adjustedWidth)/2,(this.height-adjustedHeight)/2);
newBitmapData = new BitmapData(this.width,this.height,false);
newBitmapData.draw(data,matrix,null,null,null,true);
return new Bitmap(newBitmapData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment