Flash spark Image-实际图像位置

Flash spark Image-实际图像位置,flash,apache-flex,image,Flash,Apache Flex,Image,快速提问 我有一个带有scaleMode='信箱'的spark图像控件。当我加载一个比实际控件小的图像时,它会很好地居中。现在我需要知道的是信箱内图像的实际位置。换句话说,如果它的每一面都填充了100px,我需要知道填充量,如果有的话 有人有什么想法吗 -JD尝试扩展Spark Image组件,以包括一些用于访问缩放图像位置(和尺寸)的有用只读属性 下面将创建4个新的可绑定属性,以返回Spark image组件内显示的图像的实际缩放x、y、宽度和高度 package { import

快速提问

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

有人有什么想法吗


-JD

尝试扩展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;
        }
    }
}
包
{
导入spark.components.Image;
公共类扩展SparkImageExtendedImage
{
公共函数ExtendedSparkImage()
{
超级();
}
/**
*返回图像显示的X坐标偏移,并考虑
*容器的实际约束。如果未发生缩放,则
*将返回零。
*/ 
[Bindable(event=“scaledXOffsetChanged”)]
公共函数get scaledXOffset():Number
{
var num:Number=0;
如果(scaleMode==“信箱”)
{
尝试
{                   
如果((宽度>0)和&(源宽度<源高度))
{       
num=(宽度-缩放宽度)/2;
}                   
}
捕获(e:错误)
{
num=0;
}
}
返回num;
}
/**
*返回图像显示的Y坐标偏移,并考虑
*容器的实际约束。如果未发生缩放,则
*将返回零。
*/ 
[Bindable(event=“scaledYOffsetChanged”)]
公共函数get scaledYOffset():Number
{
var num:Number=0;
如果(scaleMode==“信箱”)
{
尝试
{                   
如果((高度>0)和&(源高度<源宽度))
{
num=(高度-缩放高度)/2;
}                   
}
捕获(e:错误)
{
num=0;
}
}
返回num;
}
/**
*返回图像显示的宽度,并考虑实际宽度
*容器的约束。如果未发生缩放,则实际
*返回控件的宽度。
*/ 
[可绑定(event=“scaledWidthChanged”)]
公共函数get scaledWidth():Number
{
var num:Number=this.width;
如果(scaleMode==“信箱”)
{
尝试
{
如果((宽度>0)和&(源宽度<源高度))
{
num=(sourceWidth/sourceHeight)*宽度;
}                   
}
捕获(e:错误)
{
num=此宽度;
}
}
返回num;
}
/**
*返回图像显示的高度,并考虑实际的
*容器的约束。如果未发生缩放,则实际
*返回控件的高度。
*/ 
[Bindable(event=“scaledHeightChanged”)]
公共函数get scaledHeight():编号
{
var num:Number=this.width;
如果(scaleMode==“信箱”)
{
尝试
{
如果((高度>0)和&(源高度<源宽度))
{
num=(sourceHeight/sourceWidth)*高度;
}                   
}
捕获(e:错误)
{
num=这个高度;
}
}
返回num;
}
}
}

我没有使用spark图像控件(我猜它是新Flex SDK Hero的一部分),但是图像控件是否有contentWidth/Height属性,您可以使用它与组件的实际宽度/高度进行比较?您可以查看spark组件框架中的as文件,并尝试获取加载的图像。然后你可以进入它的位置。风险在于包含图像的属性可能是私有的。