Flex中標籤Embed的幾種使用方式

 

可以在Flex應用程序中嵌入各種元素。被嵌入的元素將編譯進SWF文件。它們不是在運行時載入的,所以不必爲應用程序部署原元素。

可以嵌入的圖像格式有PNG、JPEG、GIF。嵌入後可以同時使用它的多個實例。

下面的例子使用[Embed]原標籤經image嵌入應用程序,並且採用綁定ActionScript類的形式。這就可以綁定Image控件的source屬性到Logo類。可以綁定Logo類到任何可以使用image的組件屬性上,比如Button控件的icon屬性。

例子:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://yecon.blog.hexun.com/29628260_d.html -->
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    viewSourceURL="src/EmbeddingImages/index.html"
    layout="horizontal" width="350" height="250">
   
    <mx:Script>
        <![CDATA[
            [Embed(source="assets/logo.png")]
            [Bindable]

            public var Logo:Class;           
        ]]>
    </mx:Script>

    <mx:Image id="myLogo" source="{Logo}"/>

    <mx:Image id="myLogo2" source="{Logo}"/>

</mx:Application>
-----------------------------------------------------------------
如果想只使用一個實例的話,可以使用內部@Embed直接將圖像嵌入Flex應用程序。
下面的例子添加了一個Image組件,並且使用@Embed直接定業了它的source屬性。要使用相同的image,就必須再次將此image嵌入到那個組件中。
如果想顯示一個嵌入image的多個實例,可以用[Embed]原標籤代替。
例子:<?xml version="1.0" encoding="utf-8"?>
<mx:Application <!-- http://yecon.blog.hexun.com/29628260_d.html -->
    xmlns:mx="http://www.adobe.com/2006/mxml"
    viewSourceURL="src/EmbeddingAnImage/index.html"
    width="200" height="240">
    <mx:Image id="myLogo" source="@Embed('assets/logo.png')"/>

</mx:Application> -----------------------------------------------------------------可以在Flex應用程序中嵌入image,並且可以將它應用於組件的皮膚。可以定義一個CSS選擇器,以設置所有組件的皮膚。在下面的例子中,爲Button控件創建了一個CSS選擇器。例子:<?xml version="1.0" encoding="utf-8"?><!-- http://yecon.blog.hexun.com/29628260_d.html -->
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    viewSourceURL="src/EmbeddingImagesCSS/index.html"
    layout="horizontal" width="270" height="100"
    horizontalAlign="center" verticalAlign="middle">

    <mx:Style>
        Button
        {
            upSkin: Embed("assets/box_closed.png");

            overSkin: Embed("assets/box.png");
            downSkin: Embed("assets/box_new.png");

        }
    </mx:Style>
   
    <mx:Button/>
   
    <mx:Text text="Roll over and click the box!"/>

</mx:Application> -----------------------------------------------------------------嵌入SWF文件的方法與嵌入IMAGE的方法差不多,只是可以把已嵌入的SWF文件實例當做MovieClip類。(不可直接訪問嵌入SWF文件的屬性和方法,但是可以使用LocalConnection來允許他們之間的通信) 例子: <?xml version="1.0" encoding="utf-8"?><!-- http://yecon.blog.hexun.com/29628260_d.html -->
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
     viewSourceURL="src/EmbeddingSwfFiles/index.html"
    layout="horizontal" width="290" height="290"
    horizontalAlign="center" verticalAlign="middle"
 >

    <mx:Script>
    <![CDATA[
            [Embed(source="assets/hourglass.swf")]

            [Bindable]
            public var Hourglass:Class;           
        ]]>
    </mx:Script>

    <mx:Image id="hourglass" source="{Hourglass}"/>
</mx:Application> -----------------------------------------------------------------可以嵌入存在於應用程序的SWF文件庫裏的特殊標籤。Flash定義了三種類型的標籤:Button、MovieClip、Graphic。可以嵌入Button和MovieClip標籤到Flex應用程序,但是不不能嵌入Graphic標籤。 例子:<?xml version="1.0" encoding="utf-8"?><!-- http://yecon.blog.hexun.com/29628260_d.html -->
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    viewSourceURL="src/EmbeddingSwfLibraryAssets/index.html"
    layout="horizontal" width="450" height="240"
    horizontalAlign="center" verticalAlign="bottom">
 
    <mx:Script>
    <![CDATA[
            [Embed(source="assets/library.swf", symbol="BadApple")]

            [Bindable]
            public var BadApple:Class;
           
            [Embed(source="assets/library.swf", symbol="Pumpkin")]

            [Bindable]
            public var Pumpkin:Class;
                       
        ]]>
    </mx:Script>

    <mx:Image id="badApple" source="{BadApple}" width="150" height="151.8"/>

    <mx:Image id="pumpkin" source="{Pumpkin}" width="150" height="131.7"/>   

</mx:Application> ----------------------------------------------------------------- 可以通過使用[Embed]原標籤的方法在Flex應用程序中嵌入MP3文件。 例子:<?xml version="1.0" encoding="utf-8"?><!-- http://yecon.blog.hexun.com/29628260_d.html -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"    layout="vertical" horizontalAlign="center" verticalAlign="center"    viewSourceURL="srcEmbeddingSoundFiles/index.html">

    <mx:Script>
        <![CDATA[
            import mx.core.SoundAsset;
            import flash.media.*;

            [Embed(source="assets/pie-yan-knee.mp3")]

            [Bindable]
            public var Song:Class;

            public var mySong:SoundAsset = new Song() as SoundAsset;
            public var channel:SoundChannel;
           
            public function playSound():void
            {

                // Make sure we don't get multiple songs playing at the same time
                stopSound();
               
                // Play the song on the channel
                channel = mySong.play();
            }
           
            public function stopSound():void
            {

                // Stop the channel, but only if it exists
                if ( channel != null ) channel.stop();
            }

        ]]>
    </mx:Script>
   
    <mx:HBox>
        <mx:Button label="play" click="playSound();"/>

        <mx:Button label="stop" click="stopSound();"/>       
    </mx:HBox>
   
    <mx:Text width="348" textAlign="center" color="#ffffff">

        <mx:htmlText>
            <![CDATA[<a href="http://derekaudette.ottawaarts.com/music.php">Pie-Yan-Knee Written and Performed by: Derek R. Audette (c) 2004 (Creative Commons Attribution License)</a>]]>
        </mx:htmlText>
    </mx:Text>
   
</mx:Application> -----------------------------------------------------------------可以在Flex應用程序中嵌入SVG文件 例子:<?xml version="1.0" encoding="utf-8"?><!-- http://yecon.blog.hexun.com/29628260_d.html -->
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="horizontal"
    viewSourceURL="srcEmbeddingSvgFiles/index.html"
    width="600" height="470"

>
    <mx:Script>
    <![CDATA[
            [Embed(source="assets/frog.svg")]

            [Bindable]
            public var SvgFrog:Class;           
        ]]>
    </mx:Script>

    <mx:Image id="smallFrog" source="{SvgFrog}" width="128" height="130"/>

    <mx:Image id="largeFrog" source="{SvgFrog}"/>
</mx:Application> -----------------------------------------------------------------可以在Flex應用程序中嵌入字體 例子:<?xml version="1.0" encoding="utf-8"?><!-- http://yecon.blog.hexun.com/29628260_d.html -->
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="horizontal"
    horizontalAlign="center"
    verticalAlign="center"
    viewSourceURL="src/EmbeddingFonts/index.html">

    <mx:Style>
        @font-face
        {
            font-family: Copacetix;

            src: url("assets/copacetix.ttf");
            unicode-range:
                U+0020-U+0040, /* Punctuation, Numbers */

                U+0041-U+005A, /* Upper-Case A-Z */
                U+005B-U+0060, /* Punctuation and Symbols */
                U+0061-U+007A, /* Lower-Case a-z */
                U+007B-U+007E; /* Punctuation and Symbols */

        }

        .MyTextStyle
        {
            font-family: Copacetix;
            font-size: 24pt;   
        }

    </mx:Style>

    <mx:Text styleName="MyTextStyle" text="Embedded fonts rock!" width="100%"/>   

</mx:Application>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章