Setting the corner radius on a Spark BitmapImage control in Flex 4

The following example shows how you can set the corner radius on a Spark BitmapImage control in Flex 4 by creating a mask with a corner radius (in this case a Spark BorderContainer container with the cornerRadius style set).

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/2010/02/07/setting-the-corner-radius-on-a-spark-bitmapimage-control-in-flex-4/ -->
<s:Application name="Spark_BitmapImage_cornerRadius_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>
        <mx:Form>
            <mx:FormItem label="cornerRadius:">
                <s:HSlider id="sl"
                        minimum="0"
                        maximum="{bmpImg.height/2}" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:BitmapImage id="bmpImg"
            source="@Embed('assets/image1.jpg')"
            horizontalCenter="0" verticalCenter="0">
        <s:mask>
            <s:BorderContainer id="bmpMask"
                    cornerRadius="{sl.value}"
                    width="{bmpImg.width}" height="{bmpImg.height}" />
        </s:mask>
    </s:BitmapImage>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

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/02/07/setting-the-corner-radius-on-a-spark-bitmapimage-control-in-flex-4/ -->
<s:Application name="Spark_BitmapImage_cornerRadius_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.containers.Form;
            import mx.containers.FormItem;
            import mx.core.FlexGlobals;
            import mx.events.ResizeEvent;
 
            import spark.components.BorderContainer;
            import spark.components.HSlider;
            import spark.primitives.BitmapImage;
 
            [Embed("assets/image1.jpg")]
            protected const IMG:Class;
 
            protected var sl:HSlider;
            protected var bmpImg:BitmapImage;
            protected var bmpMask:BorderContainer;
 
            protected function init():void {
                var image:Object = new IMG();
 
                sl = new HSlider();
                sl.maximum = image.height / 2;
                sl.addEventListener(Event.CHANGE, sl_changeHandler);
 
                var frmItm:FormItem = new FormItem();
                frmItm.label = "cornerRadius:";
                frmItm.addElement(sl);
 
                var frm:Form = new Form();
                frm.addElement(frmItm);
 
                Application(FlexGlobals.topLevelApplication).controlBarContent = [frm];
 
                bmpMask = new BorderContainer();
                bmpMask.setStyle("cornerRadius", sl.value);
                bmpMask.width = image.width;
                bmpMask.height = image.height;
 
                bmpImg = new BitmapImage();
                bmpImg.source = image;
                bmpImg.mask = bmpMask;
                bmpImg.horizontalCenter = 0;
                bmpImg.verticalCenter = 0;
                addElement(bmpImg);
            }
 
            protected function sl_changeHandler(evt:Event):void {
                bmpMask.setStyle("cornerRadius", sl.value);
            }
        ]]>
    </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.

4 thoughts on “Setting the corner radius on a Spark BitmapImage control in Flex 4

  1. Hi
    How can i add an image to the Embed tag whose url i create dynamically to the bitmap source?
    I create it in the following way “http://abc.com/images/”+id+”/45.jpg”

    Thanks in advance

Comments are closed.