HSlider 从中心缩放图片的源代码

 <?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;
                             
                     }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值