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>
 
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.

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

  1. Nick says:

    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. Isa says:

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

    Greets

    -Isa

  3. peterd says:

    Isa,

    The percentHeight and percentWidth properties should be inherited from the mx.core.UIComponent class (see mx.core.UIComponent#percentHeight and mx.controls.Image)

    Peter

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