开发者

AS3: resize handler stretching image

I have an image loader that I am attempting to resize dynamically. I would like to make sure that the aspect ratio is kept intact so that the image doesn't stretch. W开发者_Go百科hen I reload the image (re-populate it with another image via the loader), there is a resize function that is called. I would like to know what I might be doing wrong with this portion of the resize code:

var aspect:Number = width / height;
var cAspect:Number = _imageBoundary.width / _imageBoundary.height;

if (aspect <= cAspect) 
{
    _ldr.height = _imageBoundary.height;
    _ldr.width = aspect * _ldr.height;
}
else 
{
    _ldr.width = _imageBoundary.width;
    _ldr.height = _ldr.width / aspect;
}

_ldr.x = (_imageBoundary.width - _ldr.width) / 2 + _imageBoundary.x;
_ldr.y = (_imageBoundary.height - _ldr.height) / 2 + _imageBoundary.y;

Any thoughts would be greatly appreciated. I am basing the larger portion of this on bhups's code.

Best


I believe the first line should be

var aspect:Number = _ldr.content.width / _ldr.content.height;


I think the most easy and reliable way is to use the Image class. You can see a good example here: http://blog.flexexamples.com/2008/06/28/maintaining-an-images-aspect-ratio-on-an-image-control-in-flex/.

the Image class may get dynamically change in its source field and load the image accordingly.

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜