TransformAroundPointPlugin

TransformAroundPointPlugin

图片缩放后位置的设置

package {
    
    import com.greensock.TweenLite;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.geom.Rectangle;
    import com.greensock.plugins.TransformAroundCenterPlugin;
    import com.greensock.plugins.TransformAroundPointPlugin;
    import com.greensock.plugins.TweenPlugin;
    import com.greensock.TweenMax;
    
    /**
     * 
     * @author whk
     */
    public class Deom extends MovieClip {
        private var imageW:Number ;
        private var imageH:Number ;
        private var stageW:Number;
        private var stageH:Number;
        private var imageDrageRect:Rectangle;
        private var scaleNum:Number = 1;
        
        public function Deom() {
            if (stage)
                init();
            else
                addEventListener(Event.ADDED_TO_STAGE, init);
        }
        
        private function init(e:Event =null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);
            
            //注册Tween
            TweenPlugin.activate([TransformAroundPointPlugin]);
            
            imageW = stage.stageWidth;
            imageH = stage.stageHeight;
            btn_enlarge.addEventListener(MouseEvent.CLICK, onEnlargeImage);
            btn_narrow.addEventListener(MouseEvent.CLICK, onNarrowImage);
            btn_reset.addEventListener(MouseEvent.CLICK, onResetImage);
        }
        
        /**
         * 还原图片大小位置
         * @param    e
         */
        private function onResetImage(e:MouseEvent):void {
            image.stopDrag();
            image.removeEventListener(MouseEvent.MOUSE_DOWN, onImageDrage);
            stage.removeEventListener(MouseEvent.MOUSE_UP, onStopDrage);
            
            image.x = image.y = 0;
            image.scaleX = image.scaleY = 1;
            scaleNum = 1;
        }
        
        /**
         * 按比例放大图片
         * @param    e
         */
        private function onEnlargeImage(e:MouseEvent):void {
            if (scaleNum < 2) {
                btn_narrow.visible = true;
                scaleNum += 0.2;
                //根据点位置缩放图片
                TweenMax.to(image, .2, {transformAroundPoint: {point: new Point(image.x, image.y), scaleX: scaleNum, scaleY: scaleNum}});
            }
            if (scaleNum >= 2)
                btn_enlarge.visible = false;
            
            setDrageRect(scaleNum);
        }
        
        /**
         * 按比例缩小图片
         * @param    e
         */
        private function onNarrowImage(e:MouseEvent):void {
            if (scaleNum > 1) {
                var a:Number = int(imageW * (scaleNum - 1));
                var b:Number = int(imageH * (scaleNum - 1));
                
                btn_enlarge.visible = true;
                scaleNum -= 0.2;
                var imageX:Number = Math.abs(int(image.x));
                var imageY:Number = Math.abs(int(image.y));
                
                //当图片缩小时,保持图片始终位于舞台
                addEventListener(Event.ENTER_FRAME, checkTrans);
                TweenMax.to(image, .2, {transformAroundPoint: {point: new Point(imageX, imageY), scaleX: scaleNum, scaleY: scaleNum}, onComplete: function() {
                        removeEventListener(Event.ENTER_FRAME, checkTrans);
                        checkTrans();
                    }});
                
            }
            if (scaleNum <= 1) {
                btn_narrow.visible = false;
            }
            setDrageRect(scaleNum);
        }
        
        /**
         * 检测图片当前的位置,若超出舞台则相应移动会舞台
         * @param    e
         */
        private function checkTrans(e:Event = null):void {
            if (image.x > 0 || image.y > 0) {
                removeEventListener(Event.ENTER_FRAME, checkTrans);
                TweenLite.to(image, .2, {x: 0, y: 0});
            }
            if (((imageW * scaleNum + image.x) < imageW)) {
                removeEventListener(Event.ENTER_FRAME, checkTrans);
                TweenLite.to(image, .2, {x: image.x + imageW - (imageW * scaleNum + image.x)});
            }
            if (((imageH * scaleNum + image.y) < imageH)) {
                removeEventListener(Event.ENTER_FRAME, checkTrans);
                TweenLite.to(image, .2, {y: image.y + imageH - (imageH * scaleNum + image.y)});
            }
        }
        
        /**
         * 设置可拖动范围
         * @param    _scaleNum 放大倍数
         */
        private function setDrageRect(_scaleNum:Number):void {
            var disX:Number = imageW - imageW * _scaleNum;
            var disY:Number = imageH - imageH * _scaleNum;
            imageDrageRect = new Rectangle(disX, disY, imageW * _scaleNum - imageW, imageH * _scaleNum - imageH);
            image.addEventListener(MouseEvent.MOUSE_DOWN, onImageDrage);
            stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrage);
        }
        
        private function onStopDrage(e:MouseEvent):void {
            image.stopDrag();
        }
        
        private function onImageDrage(e:MouseEvent):void {
            image.startDrag(false, imageDrageRect);
        }
    }

}

 

http://115.com/file/anay5ck0#
Zoom.rar

posted @ 2012-09-17 17:27  FlashHK  阅读(639)  评论(0编辑  收藏  举报