为什么这个图像不会调整大小以适应精灵?

时间:2010-04-13 03:51:02

标签: flex image actionscript scale

我有一段ActionScript代码,它应该调整图像大小以适应精灵:(如 rescale 图像只剪辑它)

package 
{
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        [Embed(source = 'img.png')]
        private var TheImage:Class;

        public static const  TheImageWidth:Number = 1300;
        public static const TheImageHeight:Number = 1300;

        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            // entry point

            var image:Bitmap = new TheImage();

            addChild(image);

            image.scaleX = width / TheImageWidth;
            image.scaleY = height / TheImageHeight;
        }

    }

}

为什么不工作?图像没有得到正确的尺寸。

2 个答案:

答案 0 :(得分:0)

image.image.content.width = TheImageWidth;
image.image.content.height = TheImageHeight;

答案 1 :(得分:0)

我不得不为这个图像做这样的事情。我们在使用ArcGIS for Flex 2.5时使用MapImage和MapImageLayer时遇到了问题。它似乎在3.5中修复,无论如何我必须将图像放入精灵并根据精灵宽度绘制图像。如果要将图像缩放到精灵的尺寸,则需要使用Matrix API并使用缩放方法。传递给它的值应该是精灵的宽度除以图像的宽度,假设图像大于你的精灵。对高度做同样的事情。这是我用来完成任务的代码。

                const point:MapPoint = geometry as MapPoint;

                sprite.x = toScreenX(map, point.x);
                sprite.y = toScreenY(map, point.y);

                // grab left x point for upper left then for lower right
                // actua
                var bottomLeftX:Number = toScreenX(map, geomertyWrapper.extent.xmin);
                var topRightX:Number = toScreenX(map, geomertyWrapper.extent.xmax);

                var bottomLeftY:Number = toScreenY(map, geomertyWrapper.extent.ymin);
                var topRightY:Number = toScreenY(map, geomertyWrapper.extent.ymax);

                iconWidth = Math.abs(topRightX - bottomLeftX);
                iconHeight = Math.abs(topRightY - bottomLeftY);

                sprite.width = iconWidth;
                sprite.height = iconHeight;

                var scaleX:Number = iconWidth/bitmapData.width;
                var scaleY:Number = iconHeight/bitmapData.height;

                m_matrix.a = 1.0;
                m_matrix.d = 1.0;
                m_matrix.scale(scaleX, scaleY);
                m_matrix.tx = -iconWidth / 2;
                m_matrix.ty = -iconHeight / 2;

                //Values needed for drawing AlertNotificationInstance

                sprite.graphics.beginBitmapFill(m_bitmapData, m_matrix, false, false);
                sprite.graphics.drawRect(m_matrix.tx, m_matrix.ty, 
                    iconWidth, 
                    iconHeight);
                sprite.graphics.endFill(); 

希望这有帮助。