独立于场景大小更改图像大小

发布于 2024-10-31 01:42:18 字数 1572 浏览 8 评论 0原文

我有三张图像,我想根据场景大小更改图像大小(默认大小为 1024x768)。每个图像完全加载后,当场景大小发生变化时,我调用bindableUtils.setter来设置宽度/高度,但我不知道如何制作指针或类似的东西。我正在使用公共 var img,但它仅适用于最后一个完整图像。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[
            import mx.binding.utils.BindingUtils;

            public var img:Image;
            private function setterWidth(newWidth:Number):void
            {
                img.width = img.content.width * newWidth / 1024;
            }
            private function setterHeight(newHeight:Number):void
            {
                img.height = img.content.height * newHeight / 768;
            }

            protected function image_completeHandler(event:Event):void
            {
                img = event.target as Image;
                BindingUtils.bindSetter(setterWidth, this, "width");
                BindingUtils.bindSetter(setterHeight, this, "height");
            }
        ]]>
    </fx:Script>

    <mx:Image id="img1" source="img1.jpg" complete="image_completeHandler(event)" />
    <mx:Image id="img2" source="img2.jpg" complete="image_completeHandler(event)" y="60"/>
    <mx:Image id="img3" source="img3.jpg" complete="image_completeHandler(event)" y="120"/>
</s:Application>

我没有为每个图像使用额外的 bindSetter 函数。

也许我关于如何根据场景大小改变图像大小的想法是错误的。你会怎么做?

I have three images and I want change the images size based on the scene size (default size is 1024x768). After each image has completely loaded I call bindableUtils.setter to set width/height when scene size changes but I don't know how to make a pointer or something like that. I'm using a public var img, but it only works with the last complete image.

Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx">

    <fx:Script>
        <![CDATA[
            import mx.binding.utils.BindingUtils;

            public var img:Image;
            private function setterWidth(newWidth:Number):void
            {
                img.width = img.content.width * newWidth / 1024;
            }
            private function setterHeight(newHeight:Number):void
            {
                img.height = img.content.height * newHeight / 768;
            }

            protected function image_completeHandler(event:Event):void
            {
                img = event.target as Image;
                BindingUtils.bindSetter(setterWidth, this, "width");
                BindingUtils.bindSetter(setterHeight, this, "height");
            }
        ]]>
    </fx:Script>

    <mx:Image id="img1" source="img1.jpg" complete="image_completeHandler(event)" />
    <mx:Image id="img2" source="img2.jpg" complete="image_completeHandler(event)" y="60"/>
    <mx:Image id="img3" source="img3.jpg" complete="image_completeHandler(event)" y="120"/>
</s:Application>

I don't use a for each image extra bindSetter function.

Maybe my idea on how I can change the size of image based on the size of the scene is wrong. How would you do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆离笙 2024-11-07 01:42:18
<fx:Script>
    <![CDATA[
    ]]>
</fx:Script>

<mx:Image id="img1" source="img1.jpg" scaleX="{width/1024}" scaleY="{height/768}" />
<mx:Image id="img2" source="img2.jpg" scaleX="{width/1024}" scaleY="{height/768}" y="60"/>
<mx:Image id="img3" source="img3.jpg" scaleX="{width/1024}" scaleY="{height/768}" y="120"/>
<fx:Script>
    <![CDATA[
    ]]>
</fx:Script>

<mx:Image id="img1" source="img1.jpg" scaleX="{width/1024}" scaleY="{height/768}" />
<mx:Image id="img2" source="img2.jpg" scaleX="{width/1024}" scaleY="{height/768}" y="60"/>
<mx:Image id="img3" source="img3.jpg" scaleX="{width/1024}" scaleY="{height/768}" y="120"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文