Setting a background image on a Spark Application in Flex 4

by Peter deHaan on March 22, 2009

in Application (Spark), BitmapGraphic, beta1

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 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".

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.

{ 5 comments… read them below or add one }

1 Denis March 22, 2009 at 8:06 pm

It is quite complicated.
Where is backgroundImage property of the Application class?

Reply

2 Peter deHaan March 23, 2009 at 7:28 am

Denis,

The new Gumbo FxApplication tag doesn’t have a backgroundImage style. It allows the FxApplication class can be a lot simpler and have less code/overhead, and thankfully skinning is very easy so creating a custom FxApplication skin with a background image can be done in a few lines.

Peter

Reply

3 Denis March 23, 2009 at 11:29 pm

I see, thank you. will try it soon.

Reply

4 fasdf October 31, 2009 at 5:23 am

where is the source for the skins class file?

Reply

5 Peter deHaan October 31, 2009 at 11:09 am

The custom skin file is above, in the example named CustomApplicationSkin.mxml.

Peter

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post: Using the wireframe theme with Spark controls in Flex 4

Next post: Removing the vertical separator from the ComboBox control in Flex