Setting the fill mode on a Spark Image control in Flex Hero

The following example shows how you can set the fill mode on a Spark Image control in Flex Hero by setting the fillMode property to one of the static constants in the mx.graphics.BitmapFillMode class.

The following example(s) require Flash Player 10 and the beta Adobe Flex Hero SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex Hero SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+Hero.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/09/04/setting-the-fill-mode-on-a-spark-image-control-in-flex-hero/ -->
<s:Application name="Spark_Image_fillMode_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="fillMode:">
                <s:DropDownList id="ddl" selectedItem="scale">
                    <s:dataProvider>
                        <s:ArrayList source="[clip,repeat,scale]" />
                    </s:dataProvider>
                </s:DropDownList>
            </s:FormItem>
        </s:Form>
    </s:controlBarContent>
 
    <s:Image id="img"
            source="assets/pattern_149.gif"
            fillMode="{ddl.selectedItem}"
            left="20" right="20" top="20" bottom="20" />
 
</s:Application>

You can also set the fillMode property using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/09/04/setting-the-fill-mode-on-a-spark-image-control-in-flex-hero/ -->
<s:Application name="Spark_Form_BitmapFill_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="fillMode:">
                <s:DropDownList id="ddl"
                        selectedItem="scale"
                        change="ddl_changeHandler(event);">
                    <s:dataProvider>
                        <s:ArrayList source="[clip,repeat,scale]" />
                    </s:dataProvider>
                </s:DropDownList>
            </s:FormItem>
        </s:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import spark.events.IndexChangeEvent;
 
            protected function ddl_changeHandler(evt:IndexChangeEvent):void {
                img.fillMode = ddl.selectedItem;
            }
        ]]>
    </fx:Script>
 
    <s:Image id="img"
            source="assets/pattern_149.gif"
            left="20" right="20" top="20" bottom="20" />
 
</s:Application>

Due to popular demand, here is the “same” example in a more ActionScript-friendly format:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/09/04/setting-the-fill-mode-on-a-spark-image-control-in-flex-hero/ -->
<s:Application name="Spark_Form_BitmapFill_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"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayList;
            import mx.graphics.BitmapFillMode;
            import spark.components.DropDownList;
            import spark.components.Form;
            import spark.components.FormItem;
            import spark.components.Image;
            import spark.events.IndexChangeEvent;
 
            protected var img:Image;
            protected var ddl:DropDownList;
 
            protected function init():void {
                img = new Image();
                img.source = "assets/pattern_149.gif";
                img.left = img.right = img.top = img.bottom = 20;
                addElement(img);
 
                var arr:Array = [BitmapFillMode.CLIP, BitmapFillMode.REPEAT, BitmapFillMode.SCALE];
                var dp:ArrayList = new ArrayList(arr);
 
                ddl = new DropDownList();
                ddl.dataProvider = dp;
                ddl.selectedItem = img.fillMode;
                ddl.addEventListener(IndexChangeEvent.CHANGE, ddl_changeHandler);
 
                var frmItm:FormItem = new FormItem();
                frmItm.label = "fillMode:";
                frmItm.addElement(ddl);
 
                var frm:Form = new Form();
                frm.addElement(frmItm);
 
                controlBarContent = [frm];
            }
 
            protected function ddl_changeHandler(evt:IndexChangeEvent):void {
                img.fillMode = ddl.selectedItem;
            }
        ]]>
    </fx:Script>
 
</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.