The following example shows how you can set the background color on a Spark Image control in Flex Hero by setting the backgroundColor style.

The following example(s) require Flash Player 10 and the beta Adobe Flex Hero 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 Hero SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+Hero.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/10/25/setting-the-background-color-on-a-spark-image-control-in-flex-hero/ -->
<s:Application name="Spark_Image_backgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <s:Form>
            <s:FormItem label="backgroundColor:">
                <mx:ColorPicker id="cp" selectedColor="black" />
            </s:FormItem>
            <s:FormItem label="source:">
                <s:layout>
                    <s:HorizontalLayout />
                </s:layout>
                <s:Button label="image1.jpg" click="img.source = 'http://helpexamples.com/flash/images/image1.jpg';" />
                <s:Button label="logo" click="img.source = 'http://helpexamples.com/flash/images/logo.png';" />
                <s:Button label="(404)" click="img.source = 'http://helpexamples.com/flash/images/404.gif';" />
                <s:Button label="(null)" click="img.source = null;" />
            </s:FormItem>
        </s:Form>
    </s:controlBarContent>
 
    <s:Image id="img"
             backgroundColor="{cp.selectedColor}"
             width="200" height="200"
             horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

You can also set the backgroundColor 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/2010/10/25/setting-the-background-color-on-a-spark-image-control-in-flex-hero/ -->
<s:Application name="Spark_Image_backgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <s:Form>
            <s:FormItem label="source:">
                <s:layout>
                    <s:HorizontalLayout />
                </s:layout>
                <s:Button label="image1.jpg" click="img.source = 'http://helpexamples.com/flash/images/image1.jpg';" />
                <s:Button label="logo" click="img.source = 'http://helpexamples.com/flash/images/logo.png';" />
                <s:Button label="(404)" click="img.source = 'http://helpexamples.com/flash/images/404.gif';" />
                <s:Button label="(null)" click="img.source = null;" />
            </s:FormItem>
        </s:Form>
    </s:controlBarContent>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
 
        s|Image {
            background-color: haloSilver;
        }
    </fx:Style>
 
    <s:Image id="img"
             width="200" height="200"
             horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/10/25/setting-the-background-color-on-a-spark-image-control-in-flex-hero/ -->
<s:Application name="Spark_Image_backgroundColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <s:Form>
            <s:FormItem label="backgroundColor:">
                <mx:ColorPicker id="cp" selectedColor="black" change="cp_changeHandler(event);" />
            </s:FormItem>
            <s:FormItem label="source:">
                <s:layout>
                    <s:HorizontalLayout />
                </s:layout>
                <s:Button label="image1.jpg" click="img.source = 'http://helpexamples.com/flash/images/image1.jpg';" />
                <s:Button label="logo" click="img.source = 'http://helpexamples.com/flash/images/logo.png';" />
                <s:Button label="(404)" click="img.source = 'http://helpexamples.com/flash/images/404.gif';" />
                <s:Button label="(null)" click="img.source = null;" />
            </s:FormItem>
        </s:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function cp_changeHandler(evt:ColorPickerEvent):void {
                img.setStyle("backgroundColor", evt.color);
            }
        ]]>
    </fx:Script>
 
    <s:Image id="img"
             width="200" height="200"
             horizontalCenter="0" verticalCenter="0" />
 
</s:Application>

This entry is based on a beta version of the Flex Hero 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 Hero SDK.

 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

Leave a Reply

Your email address will not be published.

You may 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