简体   繁体   English

Flex AS3:使用BitmapImage Child动态创建图形

[英]Flex AS3: Dynamically Create Graphic with BitmapImage Child

I've trawled the net trying to find a solution, but everything seems to be mxml-centric. 我试图找到一个解决方案,但是一切似乎都是以mxml为中心的。 What I want is to dynamically create a series of Graphics objects each with a child BitmapImage. 我想要的是动态创建一系列Graphics对象,每个对象都有一个子BitmapImage。 However, this doesn't seem to work: 但是,这似乎不起作用:

   var bmi:BitmapImage = new BitmapImage();
   bmi.source="@Embed('custom-case.png')";
   var gr:Graphic = new Graphic( );
   gr.addElement( bmi );
   gr.x = 50;
   gr.y = 50;
   this.addElement( gr );

Whereas, this does: 然而,这样做:

   <s:Graphic x="250" y="250">
    <s:BitmapImage source="@Embed('custom-case.png')">
    </s:BitmapImage>
   </s:Graphic>

Thanks in advance for any ideas. 提前感谢任何想法。

Paul 保罗

it is quite different in AS3, you have to define a variable class type like shown below. 在AS3中它是完全不同的,你必须定义一个变量类类型,如下所示。

[Embed("custom-case.png")]
 private var someImage:Class;
 ...
 bmi.source=someImage;

To follow up Shruti's comment/question (I am unable to post comment since my current reputation insufficent): 跟进Shruti的评论/问题(由于我目前的声誉不足,我无法发表评论):

The requirement for dynamic updating of images with mxml is the same as indicated with the original answer, which is that any images you might want to dynamically change to must be pre-embeded in your mxml: 使用mxml动态更新图像的要求与原始答案所示的相同,即您可能想要动态更改的任何图像必须预先嵌入到mxml中:

[Embed(source="image.png")] private var theImage:Class;

which can be later used to update an image source as such: 以后可以用来更新图像源:

<fx:Script>
    <![CDATA[           
    [Embed(source="image.png")] private var theImage:Class;

    private function updateImage():void {
        image.source = theImage;
    }
    ]]>
</fx:Script>

<s:BitmapImage id="image" source="@Embed('defaultImage.png')"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM