Toggling smoothing on a Spark Border container in Flex 4

The following example shows how you can toggle smoothing on a Spark Border container in Flex 4 by setting the Boolean smooth property on the BitmapFill object.

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 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/01/toggling-smoothing-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundFill_BitmapFill_smooth_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:CheckBox id="chckBx" label="smooth" />
    </s:controlBarContent>
 
    <s:Border id="brdr"
            cornerRadius="5"
            width="90%" height="90%"
            horizontalCenter="0" verticalCenter="0">
        <s:backgroundFill>
            <s:BitmapFill source="@Embed('assets/pattern_149.gif')" smooth="{chckBx.selected}" />
        </s:backgroundFill>
    </s:Border>
 
</s:Application>

[GoogleAdsWide]

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/01/toggling-smoothing-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundFill_BitmapFill_smooth_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:CheckBox id="chckBx" label="smooth" change="chckBx_change(event);" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            protected function chckBx_change(evt:Event):void {
                BitmapFill(brdr.backgroundFill).smooth = chckBx.selected;
            }
        ]]>
    </fx:Script>
 
    <s:Border id="brdr"
            cornerRadius="5"
            width="90%" height="90%"
            horizontalCenter="0" verticalCenter="0">
        <s:backgroundFill>
            <s:BitmapFill source="@Embed('assets/pattern_149.gif')" />
        </s:backgroundFill>
    </s:Border>
 
</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/2009/10/01/toggling-smoothing-on-a-spark-border-container-in-flex-4/ -->
<s:Application name="Spark_Border_backgroundFill_BitmapFill_smooth_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"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import mx.core.FlexGlobals;
            import mx.graphics.BitmapFill;
            import spark.components.Border;
            import spark.components.CheckBox;
 
            [Embed("assets/pattern_149.gif")]
            protected const bgImg:Class;
 
            protected var chckBx:CheckBox;
            protected var brdr:Border;
 
            private function init():void {
                chckBx = new CheckBox();
                chckBx.label = "smooth";
                chckBx.addEventListener(Event.CHANGE, chckBx_change);
                Application(FlexGlobals.topLevelApplication).controlBarContent = [chckBx];
 
                var bgFill:BitmapFill = new BitmapFill();
                bgFill.source = bgImg;
                bgFill.smooth = chckBx.selected;
 
                brdr = new Border();
                brdr.backgroundFill = bgFill;
                brdr.percentWidth = 90;
                brdr.percentHeight = 90;
                brdr.horizontalCenter = 0;
                brdr.verticalCenter = 0;
                brdr.setStyle("cornerRadius", 5);
                addElement(brdr);
            }
 
            protected function chckBx_change(evt:Event):void {
                BitmapFill(brdr.backgroundFill).smooth = chckBx.selected;
            }
        ]]>
    </fx:Script>
 
</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.

2 thoughts on “Toggling smoothing on a Spark Border container in Flex 4

  1. Thanks for the example. In our app we set the image using stylesheet properties and the BackgroundFill property winds up being null. Is there a way to smooth via the stylesheet or to style a programattically created BitmapFill?

  2. I figured out how to smooth a container that has a styled image in it:

    private function smooth(container:BorderContainer, smoothValue:Boolean):void {
    var bgFill:BitmapFill = new BitmapFill();
    bgFill.source = container.getStyle(“backgroundImage”);
    bgFill.smooth = smoothValue;
    container.backgroundFill = bgFill;
    }

Comments are closed.