开发者

Get scaled image dimensions for spark image

开发者 https://www.devze.com 2023-03-29 01:45 出处:网络
Is there an equivalent to contentWidth and contentHeight for spark images? I can get the size of the image component itself, as well as the sourceWidth and sourceHeight properties to get the unscaled

Is there an equivalent to contentWidth and contentHeight for spark images?

I can get the size of the image component itself, as well as the sourceWidth and sourceHeight properties to get the unscaled size of the image.

But I can't work out the scaled image width and height of the source as displayed in the image component.

Any help gr开发者_JAVA技巧eatly appreciated.


Was just trying to solve this exact issue. An adequate solution I found was to use IMAGE_ID.transform.pixelBounds.height (or width). Note that this won't be set until after the updateComplete event fires for the image. No idea why FLEX doesn't have a simple scaledWidth property.


Try extending the Spark Image component to include 2 new read-only properties to provide the scaled width and height as an equivalent to contentWidth and contentHeight.

The following will create 2 new bindable properties to return the actual scaled width and height of the image displayed inside the Spark Image component.

/**
 * Returns the width of the image display taking into account the actual 
 * constraints of the container.  If no scaling has occurred the actual
 * width of the control is returned.
 */ 
[Bindable(event="scaledWidthChanged")]
public function get scaledWidth():Number
{
    var num:Number = this.width;

    if (scaleMode == "letterbox")
    {
        try
        {
            if ( (width > 0) && (sourceWidth < sourceHeight) )
            {
                num = (sourceWidth/sourceHeight) * width;                       
            }                   
        }
        catch(e:Error)
        {
            num = this.width;
        }
    }

    return num;
}

/**
 * Returns the height of the image display taking into account the actual 
 * constraints of the container.  If no scaling has occurred the actual
 * height of the control is returned.
 */ 
[Bindable(event="scaledHeightChanged")]
public function get scaledHeight():Number
{
    var num:Number = this.width;

    if (scaleMode == "letterbox")
    {
        try
        {
            if ((height > 0) && (sourceHeight < sourceWidth))
            {
                num = (sourceHeight/sourceWidth) * height;
            }                   
        }
        catch(e:Error)
        {
            num = this.height;
        }
    }

    return num;
}
0

精彩评论

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