Created
December 6, 2011 10:03
-
-
Save leeprobert/1437632 to your computer and use it in GitHub Desktop.
fit a bitmap image to its container regardless of aspect ratio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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