使用Loader,运行时加载图片

BitmapImage只能在使用@Embed位图,要想在运行时加载,可以参考下面的代码

 

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
 3                xmlns:s="library://ns.adobe.com/flex/spark" 
 4                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
 5                creationComplete="application1_creationCompleteHandler(event)">
 6     
 7     <fx:Script>
 8         <![CDATA[
 9             import mx.controls.Alert;
10             import mx.events.FlexEvent;
11 
12             protected function application1_creationCompleteHandler(event:FlexEvent):void
13             {
14                 var loader:Loader = new Loader();
15                 loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoaderComplete);
16                 loader.load(new URLRequest("assets/google.png"));
17             }
18             
19             private function handleLoaderComplete(event:Event):void
20             {
21                  var bmp:Bitmap = LoaderInfo(event.target).content as Bitmap;
22                 bf.source = bmp; 
23             }
24             
25         ]]>
26     </fx:Script>
27     
28     <fx:Declarations>
29         <!-- 将非可视元素(例如服务、值对象)放在此处 -->
30     </fx:Declarations>
31     
32     <s:layout>
33         <s:VerticalLayout />
34     </s:layout
35     
36     <s:Graphic>
37         <s:Group>
38             <s:Ellipse width="300" height="300">
39                 <s:fill>
40                     <s:BitmapFill id="bf" fillMode="repeat"/>
41                 </s:fill>
42                 
43             </s:Ellipse>
44         </s:Group>
45     </s:Graphic>
46     
47     
48 </s:Application>

 

posted @ 2012-04-10 15:22  静候良机  阅读(241)  评论(0编辑  收藏  举报