Jun 29

接下来的例子演示了Flex中如何利用maintainAspectRatio属性,在Image控件中保持一张图片的原始比率。

让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
  3.         layout="vertical"
  4.         verticalAlign="middle"
  5.         backgroundColor="white">
  6.  
  7.     <mx:Script>
  8.         <![CDATA[
  9.             private function reset():void {
  10.                 img.invalidateDisplayList();
  11.                 validateNow();
  12.             }
  13.         ]]>
  14.     </mx:Script>
  15.  
  16.     <mx:ApplicationControlBar dock="true">
  17.         <mx:Form styleName="plain">
  18.             <mx:FormItem label="maintainAspectRatio:">
  19.                 <mx:CheckBox id="checkBox"
  20.                         selected="false"
  21.                         change="callLater(reset);" />
  22.             </mx:FormItem>
  23.             <mx:FormItem label="percentWidth:"
  24.                     direction="horizontal">
  25.                 <mx:HSlider id="pWidth"
  26.                         minimum="10"
  27.                         maximum="100"
  28.                         value="100"
  29.                         snapInterval="1"
  30.                         tickInterval="10"
  31.                         liveDragging="true" />
  32.                 <mx:Label text="{img.width} px" />
  33.             </mx:FormItem>
  34.             <mx:FormItem label="percentHeight:"
  35.                     direction="horizontal">
  36.                 <mx:HSlider id="pHeight"
  37.                         minimum="10"
  38.                         maximum="100"
  39.                         value="100"
  40.                         snapInterval="1"
  41.                         tickInterval="10"
  42.                         liveDragging="true" />
  43.                 <mx:Label text="{img.height} px" />
  44.             </mx:FormItem>
  45.         </mx:Form>
  46.     </mx:ApplicationControlBar>
  47.  
  48.     <mx:Image id="img"
  49.             source="assets/Fx.png"
  50.             maintainAspectRatio="{checkBox.selected}"
  51.             percentWidth="{pWidth.value}"
  52.             percentHeight="{pHeight.value}" />
  53.  
  54. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:minidxer

written by Minidxer  |  tags: , ,

Leave a Reply