Maintaining an image’s aspect ratio on an Image control in Flex

The following example shows how you can maintain a loaded image’s original aspect ratio on a Flex Image control by setting the maintainAspectRatio property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/28/maintaining-an-images-aspect-ratio-on-an-image-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            private function reset():void {
                img.invalidateDisplayList();
                validateNow();
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="maintainAspectRatio:">
                <mx:CheckBox id="checkBox"
                        selected="false"
                        change="callLater(reset);" />
            </mx:FormItem>
            <mx:FormItem label="percentWidth:"
                    direction="horizontal">
                <mx:HSlider id="pWidth"
                        minimum="10"
                        maximum="100"
                        value="100"
                        snapInterval="1"
                        tickInterval="10"
                        liveDragging="true" />
                <mx:Label text="{img.width} px" />
            </mx:FormItem>
            <mx:FormItem label="percentHeight:"
                    direction="horizontal">
                <mx:HSlider id="pHeight"
                        minimum="10"
                        maximum="100"
                        value="100"
                        snapInterval="1"
                        tickInterval="10"
                        liveDragging="true" />
                <mx:Label text="{img.height} px" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:Image id="img"
            source="assets/Fx.png"
            maintainAspectRatio="{checkBox.selected}"
            percentWidth="{pWidth.value}"
            percentHeight="{pHeight.value}" />

</mx:Application>

View source is enabled in the following example.

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/06/28/maintaining-an-images-aspect-ratio-on-an-image-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        creationComplete="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.containers.FormItemDirection;
            import mx.controls.CheckBox;
            import mx.controls.HSlider;
            import mx.controls.Image;
            import mx.controls.Label;
            import mx.events.SliderEvent

            private var checkBox:CheckBox;
            private var pWidth:HSlider;
            private var pHeight:HSlider;
            private var pWidthLbl:Label;
            private var pHeightLbl:Label;
            private var img:Image;

            private function init():void {
                checkBox = new CheckBox();
                checkBox.selected = false;
                checkBox.addEventListener(Event.CHANGE, reset);

                pWidth = new HSlider();
                pWidth.minimum = 10;
                pWidth.maximum = 100;
                pWidth.value = 100;
                pWidth.snapInterval = 1;
                pWidth.tickInterval = 10;
                pWidth.liveDragging = true;
                pWidth.addEventListener(SliderEvent.CHANGE, pWidth_change);

                pHeight = new HSlider();
                pHeight.minimum = 10;
                pHeight.maximum = 100;
                pHeight.value = 100;
                pHeight.snapInterval = 1;
                pHeight.tickInterval = 10;
                pHeight.liveDragging = true;
                pHeight.addEventListener(SliderEvent.CHANGE, pHeight_change);

                pWidthLbl = new Label();
                pHeightLbl = new Label();

                var formItem1:FormItem = new FormItem();
                formItem1.label = "maintainAspectRatio:";
                formItem1.addChild(checkBox);

                var formItem2:FormItem = new FormItem();
                formItem2.label = "percentWidth:";
                formItem2.direction = FormItemDirection.HORIZONTAL;
                formItem2.addChild(pWidth);
                formItem2.addChild(pWidthLbl);

                var formItem3:FormItem = new FormItem();
                formItem3.label = "percentHeight:";
                formItem3.direction = FormItemDirection.HORIZONTAL;
                formItem3.addChild(pHeight);
                formItem3.addChild(pHeightLbl);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem1);
                form.addChild(formItem2);
                form.addChild(formItem3);

                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                Application.application.addChildAt(appControlBar, 0);

                img = new Image();
                img.source = "assets/Fx.png";
                img.maintainAspectRatio = checkBox.selected;
                img.percentHeight = pHeight.value;
                img.percentWidth = pWidth.value;
                addChild(img);

                callLater(doInit);
            }

            private function doInit():void {
                pWidth_change(new SliderEvent(SliderEvent.CHANGE));
                pHeight_change(new SliderEvent(SliderEvent.CHANGE));
            }

            private function pWidth_change(evt:SliderEvent):void {
                img.percentWidth = pWidth.value;
                pWidthLbl.text = img.width.toString() + " px";
            }

            private function pHeight_change(evt:SliderEvent):void {
                img.percentHeight = pHeight.value;
                pHeightLbl.text = img.height.toString() + " px";
            }

            private function reset(evt:Event):void {
                img.maintainAspectRatio = evt.target.selected;
                callLater(doReset);
            }

            private function doReset():void {
                img.invalidateDisplayList();
                validateNow();
            }
        ]]>
    </mx:Script>

</mx:Application>

0 thoughts on “Maintaining an image’s aspect ratio on an Image control in Flex

  1. Hey Peter

    I was wondering if there is a way in Flex to display two of an image on top of each other or side by side when calling just one image from xml. (Possible example?)

    Thanks

    - Nick

  2. Where does the attribute “percentWidth” and “percentHeight” come from?
    Is there a special SDK Version necessary?

    Greets

    -Isa