您的位置:首页 > 其它

Flex4之关于Embed外部资源的使用方法总结

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

嵌入资源的利处:

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

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

嵌入资源的弊处:

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

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

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

例子1:一个简单的嵌入资源的例子: <mx:Button label=”Icon Button” icon=”@Embed(source=’logo.gif’)"/>

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

例子2:用变量引用嵌入的资源 <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组件的皮肤时候使用。如下代码: <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"/>

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

嵌入资源的语法:

根据嵌入位置的不同,语法也各不同:
1、[Embed(parameter1, paramater2, ...)] 元数据标签
这主要在AS文件中,或MXML文件中的 <mx:Script>标签中使用。
2、@Embed(parameter1, paramater2, ...) 指令
这主要在MXML标签中使用。
3、Embed(parameter1, paramater2, ...) 指令
这主要在 <mx:Style> 样式表中使用。
根据情况的不同嵌入资源Embed的返回类型可以是Class或String。
针对上面的我贴出来全部代码

Xml代码







<spanstyle="font-size: medium;"><?xmlversion="1.0"encoding="utf-8"?>

<s:Applicationxmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx"minWidth="955"minHeight="600">

<s:layout>

<s:BasicLayout/>

</s:layout>

<fx:Script>

<![CDATA[

[Embed(source='logo.gif')]

var imgClass:Class;

]]>

</fx:Script>

<fx:Style>

@namespace s "library://ns.adobe.com/flex/spark";

@namespace mx "library://ns.adobe.com/flex/mx";

.myButton{

icon:Embed(source="logo.gif");

}

</fx:Style>

<fx:Declarations>

<!-- 将非可视元素(例如服务、值对象)放在此处 -->

</fx:Declarations>

<mx:Buttonicon="@Embed(source=('logo.gif'))" x="28"y="19"/>

<mx:Buttonx="232"y="26"icon="{imgClass}"/>

<mx:Buttonx="449"y="26" styleName="myButton"/>

</s:Application>

</span>

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Script>
<![CDATA[
[Embed(source='logo.gif')]
var imgClass:Class;
]]>
</fx:Script>
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.myButton{
icon:Embed(source="logo.gif");
}
</fx:Style>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<mx:Button icon="@Embed(source=('logo.gif'))"  x="28" y="19"/>
<mx:Button x="232" y="26" icon="{imgClass}"/>
<mx:Button x="449" y="26"  styleName="myButton"/>
</s:Application>

至于这个图片嘛我使用的是JavaEye的LOGO图片

对嵌入的图片资源进行9格缩放(9-slice scaling)

9格图就是把一个图片切分成9个格子,如图:



中间的5区为内容区,将正常缩放;1、3、7、9为角,不进行缩放;2、8将横向缩放;4、6将纵向缩放。
见代码: <fx:Script> <![CDATA[ [Embed(source="slice_9_grid.gif", scaleGridTop="25", scaleGridBottom="125", scaleGridLeft="25", scaleGridRight="125")]
[Bindable] public var imgCls:Class; ]]> </fx:Script>
<mx:Image source="{imgCls}" width="300" height="300"/> <mx:Image source="{imgCls}" width="450" height="450"/>
以上代码中,图片slice_9_grid.gif为30px * 130px大小。通过scaleGridTop、scaleGridBottom、scaleGridLeft、scaleGridRight,上下左右分别留出了5px的边。
以上的9格子方法在制作图片为背景的UI控件皮肤中,是非常有用的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: