TweenMax除了有基础的运动,loader等功能外,还有一些特殊的功能。比如滤镜的缓动,中心旋转,投影的缓动,变色的缓动等。

这些用到了其插件。这些插件在plugins包下。特意提出来,使用方法也有点不一样。使用插件效果分两步。

1,使用TweenPlugin的静态activate方法激活相应的插件。

TweenPlugin.activate([GlowFilterPlugin , TransformAroundCenterPlugin]);

2,用TweenLite启用插件,设置插件参数。

createScale();
curRotation = int(720 * Math.random());
TweenLite.to(image , 1.5 , {glowFilter:{color:int(0xffffff * Math.random()), blurX:10, blurY:10, strength:1, alpha:1} ,
                transformAroundCenter:{scale:curScale, rotation:curRotation}});

注意:用这个需要去下载greensock。免费插件会相对少一些功能。可以网上找全的。


完整的demoCode:

package
{
    import com.greensock.TweenLite;
    import com.greensock.plugins.GlowFilterPlugin;
    import com.greensock.plugins.TransformAroundCenterPlugin;
    import com.greensock.plugins.TweenPlugin;
    
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    
    [SWF(width = "680" , height="450" , frameRate="24")]
    
    public class PhotoTween extends Sprite
    {
        [Embed(source="myassets/meitu01.jpg")]
        private const Image:Class;
        
        private var background:Sprite;
        
        private var image:Bitmap;
        
        private var rawWidth:int;
        
        private var rawlHeight:int;
        
        private var maxScale:Number;
        
        private var minScale:Number;
        
        private var curScale:Number;
        
        private var curRotation:int;
        
        public function PhotoTween()
        {
            if(stage)
            {
                addToStageHandler();
            }
            else
            {
                addEventListener(Event.ADDED_TO_STAGE , addToStageHandler);
            }
        }
        
        private function addToStageHandler(e:Event = null):void
        {
            if(e)
                removeEventListener(Event.ADDED_TO_STAGE , addToStageHandler);
            
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.addEventListener(Event.RESIZE , resizeHandler);
            
            initialize();
            calculateSize();
            createScale();
            image.scaleX = image.scaleY = curScale;
            intLogTxt();
            
            TweenPlugin.activate([GlowFilterPlugin , TransformAroundCenterPlugin]);
            
            stage.dispatchEvent(new Event(Event.RESIZE));
            stage.addEventListener(MouseEvent.CLICK , clickHandler);
            stage.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }
        
        private function initialize():void
        {
            background = new Sprite();
            addChild(background);
            
            image = new Image() as Bitmap;
            addChild(image);
            
            rawWidth = image.width;
            rawlHeight = image.height;
        }
        
        private function clickHandler(e:MouseEvent):void
        {
            createScale();
            curRotation = int(720 * Math.random());
            TweenLite.to(image , 1.5 , {glowFilter:{color:int(0xffffff * Math.random()), blurX:10, blurY:10, strength:1, alpha:1} ,
                transformAroundCenter:{scale:curScale, rotation:curRotation}});
            log();
        }
        
        private function resizeHandler(e:Event):void
        {
            if(background)
            {
                background.graphics.clear();
                background.graphics.lineStyle(2 , 0x00796A , 0.8);
                background.graphics.drawRect(0 , 0 , stage.stageWidth , stage.stageHeight);
                background.graphics.endFill();
            }
            
            image.x = stage.stageWidth * 0.5 - image.width * 0.5;
            image.y = stage.stageHeight * 0.5 - image.height * 0.5;
        
            calculateSize();
        }
        
        private function calculateSize():void
        {
            if(stage.stageWidth/stage.stageHeight > rawWidth/rawlHeight)
            {
                maxScale = stage.stageHeight/rawWidth * 0.8;
            }
            else
            {
                maxScale = stage.stageWidth/rawWidth * 0.8;
            }
            maxScale = transNum(maxScale);
            minScale = transNum(maxScale * 0.2);
        }
        
        private function createScale():void
        {
            curScale = maxScale * Math.random();
            curScale = curScale <= minScale ? (minScale + 0.15) : curScale;
            curScale = transNum(curScale);
        }
        
        private function transNum(value:Number):Number
        {
            var numStr:String = value.toFixed(2);
            return Number(numStr);
        }
        
        private function log():void
        {
            logTxt.text = "点击去作用图片: 当前Scale -> " + curScale + " 允许最大Scale -> " + maxScale + " 允许最小Scale -> " + minScale;
        }
        
        private var logTxt:TextField;
        
        private function intLogTxt():void
        {
            logTxt = new TextField();
            logTxt.autoSize = TextFieldAutoSize.LEFT;
            logTxt.defaultTextFormat = new TextFormat("微软雅黑" , 13 , 0x00796A , true);
            addChild(logTxt);
            
            logTxt.x = logTxt.y = 15;
        }
    }
}