chapter29 Embedding Asset

来源:互联网 发布:windows qt开发环境 编辑:程序博客网 时间:2022/08/11 16:27

Flex应用可以使用外部的asset,比如 image、sound、font。
Although you can reference and load assets at run time, you often compile these assets into your applications.
什么是Embedding Asset?
The process of compiling an asset into your application is called embedding the asset.

【About embedding assets】
When you embed an asset, you compile it into your application’s SWF file.
优点:swf的访问速度比运行时访问的速度快。
缺点:swf文件比运行时访问 大
【Examples of embedding assets】


【Syntax for embedding assets】
1、[Embed(parameter1, paramater2, ...)] metadata tag
在 AS 或 MXML的<mx:Script>中使用,下文有使用方法
2、@Embed(parameter1, paramater2, ...) directive
在MXML的标签中使用,下文有使用方法
3、Embed(parameter1, paramater2, ...) directive
在 <mx:Style> 中使用,下文有使用方法

【Escaping the @ character】 转义@符号
/@ 等同于 @
//@ 等同于 /@
【Embed parameters】
每种形式的embed语法都有可选参数
The exact syntax that you use to embed assets depends on where they are embedded.
Some of these parameters are available regardless of what type of asset you are embedding, and others are specific to a particular type of media.
参数 source :Specifies the name and path of the asset to embed; either an absolute path or a path relative to the file containing the embed statement. The embedded asset must be a locally stored asset. Therefore you cannot specify a URL for an asset to embed.
参数 mimeType:Specifies the mime type of the asset. 详见P979 Supported file types

【About the MIME type】
If you do not specify a mimeType parameter, Flex makes a best guess about the type of the imported file based on the file extension. If you do specify it, the mimeType parameter overrides the default guess of the asset type.

Flex supports the following MIME types:
.Application/octet-stream
.Application/x-font
.Application/x-font-truetype
.Application/x-shockwave-flash
.Audio/mpeg
.Image/gif
.Image/jpeg
.Image/png
.Image/svg
.Image/svg-xml

1、Using the [Embed] metadata tag
You can use the [Embed] metadata tag to import JPEG, GIF, PNG, SVG, SWF, TTF, and MP3 files.
必须在变量定义之前使用 [Embed]元数据标签,变量的类型是Class。
例子:
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="100" height="80" borderStyle="solid">
 <mx:Script>
 <![CDATA[
  [Embed(source="logo.gif")]
  [Bindable]
  public var imgCls:Class;
 ]]>
 </mx:Script>

 <mx:Image source="{imgCls}"/>
</mx:Application>

2、Using the @Embed() directive in MXML
Many Flex components, such as Button and TabNavigator, take an icon property or other property that lets you  specify an image to the control.
You can use the @Embed() directive to set an MXML tag property, or to set a property value by using a child tag. The @Embed() directive returns a value of type Class or String.
<?xml version="1.0"?>
<!-- embed/ButtonAtEmbed.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
 <mx:Button label="Icon Button" icon="@Embed(source='logo.gif')"/>
</mx:Application>

3、Embedding assets in style sheets
Many style properties of Flex components support imported assets. Most frequently, you use these style properties to set the skins for a component. Skinning is the process of changing the appearance of a component by modifying or replacing its visual elements.
详见 Chapter20 Creating Skins

【Embedding asset types】
嵌入的各种资源类型举例,详见 P985