0

我有这段 ActionScript 代码片段,它应该调整图像的大小以适合精灵:(如重新缩放图像不仅仅是剪辑它)

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

    }

}

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

4

2 回答 2

0

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

                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(); 

希望这可以帮助。

于 2013-11-06T19:31:27.723 回答
0

尝试

image.image.content.width = TheImageWidth;
image.image.content.height = TheImageHeight;
于 2010-04-13T13:40:20.697 回答