开发者

spark Image - actual image location

Quick question ..

I have a spark image control with scaleMode = 'letterbox'. When I load an image smaller than the actual control it centers nicely. Now what I need to know is the ACTUAL location of the image within the letterbox. In other words, if it is padded 100px on each side开发者_JAVA百科, I need to know the amount of padding, if any.

Anyone have any ideas?

-JD


Try extending the Spark Image component to include some helpful read-only properties for accessing the scaled image location (and dimensions).

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

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;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜