小小菜鸟的web菜园子

web开发学习。好记性不如烂笔头。每天进步一点点!

导航

约束Image控件加载图片的长宽比.

Image控件的maintainAspectRatio属性.
示例:



代码:
<?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>

还可以通过AS的方法实现:
<?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>

 


from:http://blog.flexexamples.com/2008/06/28/maintaining-an-images-aspect-ratio-on-an-image-control-in-flex/

posted on 2008-07-01 10:43  『小小菜鸟』  阅读(1105)  评论(0编辑  收藏  举报