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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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>

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.

 
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.

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

  1. Praveen says:

    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

  2. Praveen says:

    How can pass a dynamically created image url to the Embed tag?

  3. dragovian says:

    it’s better to use the mx:image and mx:canvas for this … sorry flex 4 but no autoload on s:BitmapImage = serious fail

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