[Flex]加载图片的例子允许手工设置图片的宽和高
时间:2016-10-30 来源:cnblogs.com 点击:
加载图片的例子,并且允许手工设置图片的宽和高
- <fx:Script>
- <![CDATA[
- protected function button1_clickHandler(event:MouseEvent):void
- {
- img.width = Number(imgWidth.text);
- img.height = Number(imgHeight.text);
- }
- ]]>
- </fx:Script>
- <fx:Declarations>
- <!-- 将非可视元素(例如服务、值对象)放在此处 -->
- </fx:Declarations>
- <mx:Image x="106" y="162" source="@Embed('/test/1.jpg')" width="211" height="182" id="img"/>
- <s:Button x="220" y="38" label="设置" click="button1_clickHandler(event)"/>
- <s:TextInput x="109" y="38" width="35" id="imgWidth"/>
- <s:TextInput x="162" y="38" width="35" id="imgHeight"/>
我把1.jpg放到test包里了,写路径的话就需要这么写:@Embed('/test/1.jpg'),不然可以会出现“无法解析用于转换代码”的错误;
Number()用于将给定值转换成数字值,用法很简单;
如果需要让图片加载的时候宽度和高度跟图片原始宽高度一样,可以这么写:<mx:Image x="106" y="162" source="@Embed('/test/1.jpg')" width="100%" height="100%" id="img" autoLoad="true" scaleContent="false"/>