约束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/

转载于:https://www.cnblogs.com/xxcainiao/archive/2008/07/01/1232896.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值