Setting a background image fill on a Spark Border container in Flex 4

by Peter deHaan on November 16, 2009

in Border (Spark), beta2

The following example shows how you can set a background image fill on a Spark Border container in Flex 4 by setting the backgroundImage style.

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/11/16/setting-a-background-image-fill-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundImage_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:Border id="brdr"
            backgroundImage="@Embed('fx_appicon-tn.gif')"
            backgroundImageFillMode="repeat"
            borderColor="haloOrange"
            borderWeight="10"
            width="320" height="240"
            horizontalCenter="0" verticalCenter="0">
        <s:Label id="sdkVer"
                color="haloOrange"
                fontSize="48"
                fontWeight="bold"
                left="20" top="20"
                creationComplete="sdkVer.text = mx_internal::VERSION;" />
    </s:Border>
 
</s:Application>

You can also set the backgroundImage and backgroundImageFillMode styles 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/11/16/setting-a-background-image-fill-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundImage_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">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/halo";
 
        s|Border {
            backgroundImage: Embed("fx_appicon-tn.gif");
            backgroundImageFillMode: repeat;
            borderColor: haloOrange;
            borderWeight: 10;
        }
    </fx:Style>
 
    <s:Border id="brdr"
            width="320" height="240"
            horizontalCenter="0" verticalCenter="0">
        <s:Label id="sdkVer"
                color="haloOrange"
                fontSize="48"
                fontWeight="bold"
                left="20" top="20"
                creationComplete="sdkVer.text = mx_internal::VERSION;" />
    </s:Border>
 
</s:Application>

Or you can set the backgroundImage and backgroundImageFillMode styles using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/11/16/setting-a-background-image-fill-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundImage_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:controlBarContent>
        <s:Graphic click="brdr.setStyle('backgroundImage', cfFill);">
            <s:BitmapImage source="{cfFill}" />
        </s:Graphic>
        <s:Graphic click="brdr.setStyle('backgroundImage', flPlayerFill);">
            <s:BitmapImage source="{flPlayerFill}" />
        </s:Graphic>
        <s:Graphic click="brdr.setStyle('backgroundImage', fxFill);">
            <s:BitmapImage source="{fxFill}" />
        </s:Graphic>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            [Embed("cf_appicon-tn.gif")]
            protected const cfFill:Class;
 
            [Embed("fl_player_appicon-tn.gif")]
            protected const flPlayerFill:Class;
 
            [Embed("fx_appicon-tn.gif")]
            protected const fxFill:Class;
        ]]>
    </fx:Script>
 
    <s:Border id="brdr"
            backgroundImage="{fxFill}"
            backgroundImageFillMode="repeat"
            borderWeight="10"
            width="320" height="240"
            horizontalCenter="0" verticalCenter="0">
        <s:Label id="sdkVer"
                color="haloOrange"
                fontSize="48"
                fontWeight="bold"
                left="20" top="20"
                creationComplete="sdkVer.text = mx_internal::VERSION;" />
    </s:Border>
 
</s:Application>

You can also set the background image by setting the backgroundFill property to a BitmapFill object, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/11/16/setting-a-background-image-fill-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundImage_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:Border id="brdr"
            borderColor="haloOrange"
            borderWeight="10"
            width="320" height="240"
            horizontalCenter="0" verticalCenter="0">
        <s:backgroundFill>
            <s:BitmapFill source="@Embed('fx_appicon-tn.gif')" fillMode="repeat" />
        </s:backgroundFill>
        <s:Label id="sdkVer"
                color="haloOrange"
                fontSize="48"
                fontWeight="bold"
                left="20" top="20"
                creationComplete="sdkVer.text = mx_internal::VERSION;" />
    </s:Border>
 
</s:Application>

Or you could embed the image using ActionScript and use data binding to define the BitmapFill object’s source property, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/11/16/setting-a-background-image-fill-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundImage_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">
 
    <fx:Script>
        <![CDATA[
            [Embed("fx_appicon-tn.gif")]
            private const fxFill:Class;
        ]]>
    </fx:Script>
 
    <s:Border id="brdr"
            borderColor="haloOrange"
            borderWeight="10"
            width="320" height="240"
            horizontalCenter="0" verticalCenter="0">
        <s:backgroundFill>
            <s:BitmapFill source="{fxFill}" fillMode="repeat" />
        </s:backgroundFill>
        <s:Label id="sdkVer"
                color="haloOrange"
                fontSize="48"
                fontWeight="bold"
                left="20" top="20"
                creationComplete="sdkVer.text = mx_internal::VERSION;" />
    </s:Border>
 
</s:Application>

Or you could declare the BitmapFill object in an <fx:Declarations/> tag and use data binding to set the backgroundFill property, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/11/16/setting-a-background-image-fill-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundImage_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">
 
    <fx:Declarations>
        <s:BitmapFill id="fxFill" source="@Embed('fx_appicon-tn.gif')" fillMode="repeat" />
    </fx:Declarations>
 
    <s:Border id="brdr"
            backgroundFill="{fxFill}"
            borderColor="haloOrange"
            borderWeight="10"
            width="320" height="240"
            horizontalCenter="0" verticalCenter="0">
        <s:Label id="sdkVer"
                color="haloOrange"
                fontSize="48"
                fontWeight="bold"
                left="20" top="20"
                creationComplete="sdkVer.text = mx_internal::VERSION;" />
    </s:Border>
 
</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.

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:

Next post: