Flex 加载资源方式

Flex软件中经常需要使用一些外部的资源,如图片、声音、SWF或字体,第一种你也可以在软件运行的时候引入和载入,第二种可能经常需要直接将这些资源编译(Compile)到软件中,也就是直接嵌入资源(Embedding Assets)。Flex中可以直接嵌入图片image,影片movie,MP3,和TrueType文字。

直接引用资源

这种方式相对第二种速度慢,但用起来比较灵活。但我个人感觉flex的控件设计的不够方便,

Image能接收Class,也能接收Bitmap,但是Icon不能接收Bitmap,Icon只能接收的是 Class对象。这就给用的人带来了很大的困扰。但由于很多人都遇到了这个问题,所以网上很多人使用IconUtility来实现直接引用资源。

  1. package com.emavaj.myfriend.util  
  2. {  
  3.     import flash.display.BitmapData;  
  4.     import flash.display.Loader;  
  5.     import flash.display.LoaderInfo;  
  6.     import flash.events.Event;  
  7.     import flash.geom.Matrix;  
  8.     import flash.net.URLRequest;  
  9.     import flash.system.LoaderContext;  
  10.     import flash.utils.Dictionary;  
  11.       
  12.     import mx.containers.accordionClasses.AccordionHeader;  
  13.     import mx.controls.tabBarClasses.Tab;  
  14.     import mx.core.BitmapAsset;  
  15.     import mx.core.UIComponent;  
  16.       
  17.     /** 
  18.      * Provides a workaround for using run-time loaded graphics in styles and properties which require a Class reference 
  19.      */  
  20.     public class IconUtility extends BitmapAsset  
  21.     {  
  22.           
  23.         private static var dictionary:Dictionary;  
  24.           
  25.         /** 
  26.          * Used to associate run-time graphics with a target 
  27.          * @param target A reference to the component associated with this icon 
  28.          * @param source A url to a JPG, PNG or GIF file you wish to be loaded and displayed 
  29.          * @param width Defines the width of the graphic when displayed 
  30.          * @param height Defines the height of the graphic when displayed 
  31.          * @return A reference to the IconUtility class which may be treated as a BitmapAsset 
  32.          * @example <mx:Button id="button" icon="{IconUtility.getClass(button, 'http://www.yourdomain.com/images/test.jpg')}" /> 
  33.          */  
  34.         public static function getClass( target:UIComponent, source:String, width:Number = NaN, height:Number = NaN ):Class {  
  35.             if(!dictionary) {  
  36.                 dictionary = new Dictionary(false);  
  37.             }  
  38.             //if(source is String) {  
  39.                 var loader:Loader = new Loader();  
  40.                 loader.load(new URLRequest(source as String), new LoaderContext(true));  
  41.                 //source = loader;  
  42.             //}  
  43.             dictionary[target] = { source:loader, width:width, height:height };  
  44.             return IconUtility;  
  45.         }  
  46.           
  47.         /** 
  48.          * @private 
  49.          */  
  50.         public function IconUtility():void {  
  51.             addEventListener(Event.ADDED, addedHandler, false, 0, true)  
  52.         }  
  53.           
  54.         private function addedHandler(event:Event):void {  
  55.             if(parent) {  
  56.                 if(parent is AccordionHeader) {  
  57.                     var header:AccordionHeader = parent as AccordionHeader;  
  58.                     getData(header.data);  
  59.                 } else if(parent is Tab) {  
  60.                     var tab:Tab = parent as Tab;  
  61.                     getData(tab.data);  
  62.                 } else {  
  63.                     getData(parent);  
  64.                 }  
  65.             }  
  66.         }  
  67.           
  68.         private function getData(object:Object):void {  
  69.             var data:Object = dictionary[object];  
  70.             if(data) {  
  71.                 var source:Object = data.source;  
  72.                 if(data.width > 0 && data.height > 0) {  
  73.                     bitmapData = new BitmapData(data.width, data.height, true, 0x00FFFFFF);  
  74.                 }  
  75.                 if(source is Loader) {  
  76.                     var loader:Loader = source as Loader;  
  77.                     if(!loader.content) {  
  78.                         loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);  
  79.                     } else {  
  80.                         displayLoader(loader);  
  81.                     }  
  82.                 }  
  83.             }  
  84.         }  
  85.           
  86.         private function displayLoader( loader:Loader ):void {  
  87.             if(!bitmapData) {  
  88.                 bitmapData = new BitmapData(loader.content.width, loader.content.height, true, 0x00FFFFFF);  
  89.             }  
  90.             bitmapData.draw(loader, new Matrix(bitmapData.width/loader.width, 0, 0, bitmapData.height/loader.height, 0, 0));  
  91.             if(parent is UIComponent) {  
  92.                 var component:UIComponent = parent as UIComponent;  
  93.                 component.invalidateSize();  
  94.             }  
  95.         }  
  96.           
  97.         private function completeHandler(event:Event):void {  
  98.             if(event && event.target && event.target is LoaderInfo) {  
  99.                 displayLoader(event.target.loader as Loader);  
  100.             }  
  101.         }  
  102.           
  103.     }  
  104. }  

 

 

 

第二种方式是嵌入资源方式

 

嵌入资源的利处:

1、比起在运行时访问资源,对嵌入资源的访问速度更加快速;

2、可以用简单的变量访问方式,在多个地方引用所嵌入的资源。这是变量就代表资源,提高写代码的效率;

嵌入资源的弊处:

1、增大了SWF文件的大小,因为是将资源直接包含;

2、由于SWF文件增大,将使得初始化的速度变慢;

3、当资源改变后,需要重新编译SWF文件;

例子1:一个简单的嵌入资源的例子:

<?xml version=”1.0”?>
<!-- embed/ButtonIcon.mxml -->
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml”>
             <mx:Button label=”Icon Button” icon=”@Embed(source=’logo.gif’)"/>
</mx:Application>

以上粗体部分,使用了@Embed()指令,将logo.gif这个图片直接嵌入到程序中,作为Button按钮的Icon图标。

例子2:用变量引用嵌入的资源

<?xml version="1.0"?>
<!-- embed/ButtonIconClass.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
             <mx:Script>
                 <![CDATA[
                     [Embed(source="logo.gif")]
                     [Bindable]
                     public var imgCls:Class;

                 ]]>
             </mx:Script> ADOBE FLEX 3 BETA 2

             <mx:Button label="Icon Button 1" icon="{imgCls}"/>
             <mx:Button label="Icon Button 2" icon="{imgCls}"/>

以上粗体部分,表示将logo.gif图片嵌入,并让变量imgCls可以引用该资源。[Bindable]表示该变量imgCls是可以被数据绑定的。之后,就可以在多个地方引用该嵌入资源的变量(见红色粗体)。

另外也可以通过Embed()指令,在样式表中嵌入资源,这通常是在设置UI组件的皮肤时候使用。如下代码:

<?xml version="1.0"?>
<!-- embed/ButtonIconCSS.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:Style>  
                .myCustomButton {
            overSkin:Embed(source="overIconImage.gif");
            upSkin:Embed(source="upIconImage.gif");
            downSkin:Embed(source="downIconImage.gif");
                }
            </mx:Style>
            <mx:Button label="Icon Button Style Def" styleName="myCustomButton"/>
</mx:Application>

以上代码表示在按钮的常态(up)、鼠标悬停(over)、鼠标按下(down)的状态,使用不同的皮肤。overSkin、 upSkin、downSkin是Button的对应状态下的皮肤属性

posted @ 2011-09-22 19:38  rob_2010  阅读(310)  评论(0)    收藏  举报