spark图像 - 实际图像位置

时间:2011-06-09 07:00:45

标签: flash flex image

快速提问..

我有一个带有scaleMode ='letterbox'的火花图像控件。当我加载小于实际控件的图像时,它很好地居中。现在我需要知道的是信箱内图像的ACTUAL位置。换句话说,如果每边填充100px,我需要知道填充量,如果有的话。

有人有什么想法吗?

-JD

1 个答案:

答案 0 :(得分:0)

尝试扩展Spark Image组件,以包含一些有用的只读属性,以访问缩放的图像位置(和尺寸)。

以下将创建4个新的可绑定属性,以返回Spark Image组件内显示的图像的实际缩放x,y,宽度和高度。

package 
{
    import spark.components.Image;

    public class ExtendedSparkImage extends Image
    {

        public function ExtendedSparkImage()
        {
            super();
        }

        /**
         * Returns the X coordinate offset of the image display taking into account 
         * the actual constraints of the container.  If no scaling has occurred it 
         * will return zero.
         */ 
        [Bindable(event="scaledXOffsetChanged")]
        public function get scaledXOffset():Number
        {
            var num:Number = 0;

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

            return num;
        }

        /**
         * Returns the Y coordinate offset of the image display taking into account 
         * the actual constraints of the container.  If no scaling has occurred it 
         * will return zero.
         */ 
        [Bindable(event="scaledYOffsetChanged")]
        public function get scaledYOffset():Number
        {
            var num:Number = 0;

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

            return num;
        }

        /**
         * 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;
        }
    }
}