<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.effects.Zoom;
private var _zoom:Zoom;
private var _zWidth:Number = 1;
private var _zHeight:Number = 1;
private function init():void
{
_zoom = new Zoom;
}
private function zoom():void
{
_zoom.stop();
_zoom.originX = img.width / 2;
_zoom.originY = img.height / 2;
_zoom.zoomWidthFrom = _zWidth;
_zoom.zoomHeightFrom = _zHeight;
_zoom.zoomWidthTo = hslider.value;
_zoom.zoomHeightTo = hslider.value;
_zoom.duration = 1000;
_zoom.target = img;
_zoom.play();
_zWidth = hslider.value;
_zHeight = hslider.value;
}
]]>
</mx:Script>
<mx:HSlider id="hslider" x="260" y="30" minimum="0.1" maximum="3" value="1"
liveDragging="true" change="zoom()" snapInterval="0.01"/>
<mx:Canvas x="200" y="121" width="420" height="315" borderStyle="1">
<mx:Image id="img" source="wt.png" horizontalCenter="0" verticalCenter="0"/>
</mx:Canvas>
</mx:Application>
这个例子可用,其核心因素是因为 horizontalCenter="0" verticalCenter="0"
上述参数的意思是:图片的中心位置距离图片所在的canvas的中心位置为0.这样做的话,img所设置的位置x和y将无效。如果不加horizontalCenter="0" verticalCenter="0",图片虽然会放大缩小,但会到处移动。
为了解决x和y位置固定,围绕图片放大缩小,采用下面的代码,解决了问题:
import mx.effects.Zoom;
private var _zoom:Zoom;
private var _zWidth:Number = 1;
private var _zHeight:Number = 1;
private function zoom():void
{
//_zoom = new zoom;一定要将这行放到init函数里,
//否则,每次移动滑块都会重新生成zoom对象,拖拽滑块时,图像会到处跑。
wt.horizontalGradientMatrix(wt.x,wt.y,wt.width,wt.height);
wt.verticalGradientMatrix(wt.x,wt.y,wt.width,wt.height);
_zoom.target = wt;
_zoom.stop();
_zoom.zoomWidthFrom = _zWidth;
_zoom.zoomHeightFrom = _zHeight;
_zoom.zoomWidthTo = hslider.value;
_zoom.zoomHeightTo = hslider.value;
_zoom.play();
_zWidth = hslider.value;
_zHeight = hslider.value;
}