Setting a background image on a Spark Application in Flex 4

The following example shows how you can add a background image to a Flex 4 Spark Application by creating a custom skin with a BitmapGraphic and setting the skinClass style in MXML, CSS, or ActionScript.

Full code after the jump.

 

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/22/setting-a-background-image-on-an-fxapplication-in-flex-gumbo/ -->
<s:Application name="Spark_Application_skinClass_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo"
        skinClass="skins.CustomApplicationSkin">
    <s:layout>
        <s:BasicLayout />
    </s:layout>
 
    <s:RichEditableText id="sdkVer"
            editable="false"
            textAlign="center"
            fontSize="72"
            horizontalCenter="0"
            verticalCenter="0"
            width="100%"
            initialize="sdkVer.text = mx_internal::VERSION;" />
 
</s:Application>

The custom skin class, skins/CustomApplicationSkin.mxml, is as follows:

View skins/CustomApplicationSkin.mxml

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/22/setting-a-background-image-on-an-fxapplication-in-flex-gumbo/ -->
<s:Skin name="CustomApplicationSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        alpha.disabled="0.5" >
    <s:states>
        <s:State name="normal" />
        <s:State name="disabled" />
    </s:states>
 
    <fx:Metadata>
    <![CDATA[ 
        [HostComponent("spark.components.Application")]
    ]]>
    </fx:Metadata> 
 
    <!-- fill -->
    <s:BitmapImage id="img"
            source="@Embed('image1.jpg')"
            resizeMode="scale"
            smooth="true"
            left="0" right="0"
            top="0" bottom="0" />
 
    <s:Group id="contentGroup" left="0" right="0" top="0" bottom="0" minWidth="0" minHeight="0" />
 
</s:Skin>

You can also set the skinClass style in an external .CSS file or <Style/> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/22/setting-a-background-image-on-an-fxapplication-in-flex-gumbo/ -->
<s:Application name="Spark_Application_skinClass_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:layout>
        <s:BasicLayout />
    </s:layout>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
 
        s|Application {
            skinClass: ClassReference("skins.CustomApplicationSkin");
        }
    </fx:Style>
 
    <s:RichEditableText id="sdkVer"
            editable="false"
            textAlign="center"
            fontSize="72"
            horizontalCenter="0"
            verticalCenter="0"
            width="100%"
            initialize="sdkVer.text = mx_internal::VERSION;" />
 
</s:Application>

Or, you can set the skinClass style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/22/setting-a-background-image-on-an-fxapplication-in-flex-gumbo/ -->
<s:Application name="Spark_Application_skinClass_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:layout>
        <s:BasicLayout />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            import skins.CustomApplicationSkin;
 
            protected function btn_click(evt:MouseEvent):void {
                setStyle("skinClass", CustomApplicationSkin);
            }
        ]]>
    </fx:Script>
 
    <s:Button id="btn"
            label="Set skin class"
            click="btn_click(event);"
            x="10"
            y="10" />
 
    <s:RichEditableText id="sdkVer"
            editable="false"
            textAlign="center"
            fontSize="72"
            horizontalCenter="0"
            verticalCenter="0"
            width="100%"
            initialize="sdkVer.text = mx_internal::VERSION;" />
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值