Flex 加载图片的几种方式:
1.直接标签加入,嵌入swf中
<mx:Image source="@Embed(source='view/test1.png')"/>
2.直接标签加入,动态加入swf中
<mx:Image source="view/test1.png"/>
3.定义变量绑定
[Embed(source='view/test.png')] [Bindable]private var imageSource:Class; <mx:Image source="{imageSource}"/>
4.As代码加入
var img:Image = new Image(); img.source = imageSource; this.addElement(img);
或者
var img:Image = new Image(); img.source = "view/test.png"; this.addElement(img);
5.嵌入外部swf中的图片
1.在fla的库中添加一张图片,选中图片右键-属性-定义它的导出类.例如:imgpng。fla会自动的赋值它的基类为:flash.display.BitmapData。
2.导出为swf,放到flexbulider中。我放到了view文件夹下面。
[Embed(source='view/test.swf',symbol=='imgpng')] [Bindable]private var imageSource:Class; <mx:Image source="{imageSource}"/>
6.从外部swf中动态加载
1.在fla的库中添加一张图片,选中图片右键-属性-定义它的导出类.例如:imgpng。fla会自动的赋值它的基类为:flash.display.BitmapData。
2.导出为swf,放到flexbulider中。我放到了view文件夹下面。
3.加载此swf
var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete); var request:URLRequest = new URLRequest("view/test.swf"); loader.load(request); private function onLoadComplete(event:Event):void { var imgpng:Class = (event.target as LoaderInfo).applicationDomain.getDefinition("imgpng") as Class; var dataBitmpa:Bitmap = new Bitmap(new imgpng(1000,1000)); image.source = dataBitmpa; var dataBitmapAsset:Class = sourceLoadUtility.getClass(new imgpng(1000,1000)); button.setStyle("icon",dataBitmapAsset); }
package com.benstucki.utilities
{ import flash.display.BitmapData; import flash.events.Event; import mx.core.BitmapAsset; import mx.core.UIComponent; public class sourceLoadUtility extends BitmapAsset { private static var sourceBitMapData:BitmapData; public static function getClass(sourceBitMapData1:BitmapData):Class { sourceBitMapData = sourceBitMapData1; return sourceLoadUtility; } public function sourceLoadUtility():void { addEventListener(Event.ADDED, addedHandler, false, 0, true) } private function addedHandler(event:Event):void { if(!bitmapData) { bitmapData = new BitmapData(100, 100, true, 0x00FFFFFF); } bitmapData.draw(sourceBitMapData); if(parent is UIComponent) { var component:UIComponent = parent as UIComponent; component.invalidateSize(); } } } }
(image是图片,button是按钮)
PS:BitmapAsset 是 flash.display.Bitmap 类的子类,表示您在 Flex 应用程序中嵌入的位图图像。它用于实现 IFlexDisplayObject 接口,此接口允许在 Image 控件中显示嵌入的位图图像,或将位图图像用作容器背景或组件外观。
你也可以参考IconUtility.as类来用。