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".
<?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:
<?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:
<?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 }
It is quite complicated.
Where is backgroundImage property of the Application class?
Denis,
The new Gumbo FxApplication tag doesn’t have a
backgroundImagestyle. 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
I see, thank you. will try it soon.
where is the source for the skins class file?
The custom skin file is above, in the example named CustomApplicationSkin.mxml.
Peter