How to get a stage calculation to resize an image w/r/t c

我是研究僧i 提交于 2019-12-12 05:06:56

问题


I am trying to center an image in the middle of the stage and scale it proportionally to the correct aspect ratio based on its loader's size when the image is loading.

In my main app runner I do:

private var rec:Rectangle = new Rectangle();
private var img:BitmapDisplay = new BitmapDisplay();
stage.addEventListener(Event.RESIZE, setRect);
img.imageURL = "some/path/to/image.jpg";
addChild(img);

private function setRect():void
{
    var horz:Number = stage.stageWidth / 16;
    var vert:Number = stage.y + stage.stageHeight / 16;

    rec.x = horz;
    rec.y = 80;
    rec.width = stage.stageWidth - horz;
    rec.height = stage.stageHeight - (vert * 2) - rec.y;

    img.boundary = rec;
}

Then in my image (BitmapDisplay) class I have:

// sets the boundary of the image and resizes it
public function set boundary(rect:Rectangle):void 
{
    _imageBoundary = rect;
    resizeImage();
}

private function resizeImage():void 
{
    var aspect:Number = _ldr.content.width / _ldr.content.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;
    }

    var _pad:int = 7;

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

Is there something obvious keeping this from working right? I want a 16th of the stage to be the bounds for the image and for the image to scale/resize based on this rect.

Thanks...


回答1:


It turns out that this had more to do with the loader not being ready to be resized. There was some error checking in my code that needed to be reformatted, so now when the resize function gets called, it doesn't do anything until the loader is ready and the boundary is set.



来源:https://stackoverflow.com/questions/1997655/how-to-get-a-stage-calculation-to-resize-an-image-w-r-t-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!