微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flex 加载资源方式

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

直接引用资源

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

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

package com.emavaj.myfriend.util
{
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;
	
	import mx.containers.accordionClasses.AccordionHeader;
	import mx.controls.tabBarClasses.Tab;
	import mx.core.BitmapAsset;
	import mx.core.UIComponent;
	
	/**
	 * Provides a workaround for using run-time loaded graphics in styles and properties which require a Class reference
	 */
	public class IconUtility extends BitmapAsset
	{
		
		private static var dictionary:Dictionary;
		
		/**
		 * Used to associate run-time graphics with a target
		 * @param target A reference to the component associated with this icon
		 * @param source A url to a JPG,PNG or GIF file you wish to be loaded and displayed
		 * @param width Defines the width of the graphic when displayed
		 * @param height Defines the height of the graphic when displayed
		 * @return A reference to the IconUtility class which may be treated as a BitmapAsset
		 * @example <mx:Button id="button" icon="{IconUtility.getClass(button,'http://www.yourdomain.com/images/test.jpg')}" />
		 */
		public static function getClass( target:UIComponent,source:String,width:Number = NaN,height:Number = NaN ):Class {
			if(!dictionary) {
				dictionary = new Dictionary(false);
			}
			//if(source is String) {
				var loader:Loader = new Loader();
				loader.load(new URLRequest(source as String),new LoaderContext(true));
				//source = loader;
			//}
			dictionary[target] = { source:loader,width:width,height:height };
			return IconUtility;
		}
		
		/**
		 * @private
		 */
		public function IconUtility():void {
			addEventListener(Event.ADDED,addedHandler,false,true)
		}
		
		private function addedHandler(event:Event):void {
			if(parent) {
				if(parent is AccordionHeader) {
					var header:AccordionHeader = parent as AccordionHeader;
					getData(header.data);
				} else if(parent is Tab) {
					var tab:Tab = parent as Tab;
					getData(tab.data);
				} else {
					getData(parent);
				}
			}
		}
		
		private function getData(object:Object):void {
			var data:Object = dictionary[object];
			if(data) {
				var source:Object = data.source;
				if(data.width > 0 && data.height > 0) {
					bitmapData = new BitmapData(data.width,data.height,true,0x00FFFFFF);
				}
				if(source is Loader) {
					var loader:Loader = source as Loader;
					if(!loader.content) {
						loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler,true);
					} else {
						displayLoader(loader);
					}
				}
			}
		}
		
		private function displayLoader( loader:Loader ):void {
			if(!bitmapData) {
				bitmapData = new BitmapData(loader.content.width,loader.content.height,0x00FFFFFF);
			}
			bitmapData.draw(loader,new Matrix(bitmapData.width/loader.width,bitmapData.height/loader.height,0));
			if(parent is UIComponent) {
				var component:UIComponent = parent as UIComponent;
				component.invalidateSize();
			}
		}
		
		private function completeHandler(event:Event):void {
			if(event && event.target && event.target is LoaderInfo) {
				displayLoader(event.target.loader as Loader);
			}
		}
		
	}
}


 

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

嵌入资源的利处:

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的对应状态下的皮肤属性

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐