Jun 29
接下来的例子演示了Flex中如何利用maintainAspectRatio属性,在Image控件中保持一张图片的原始比率。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <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>
代码:Peter deHaan 翻译/整理/编译:minidxer
