一些朋友在做播放器,经常会用到文字的滚动效果。这里特意总结了下两种滚动方式。

一种是,从不间断的往一个方向移动。另一种是,从一端移动到另外一端。然后再回来从一端到另一端。如此反复。

上边说的是文字的移动方式。

除了文字移动的功能,还待有有一个两端渐变的效果。这个得用到cacheAsBitmap属性。将显示对象的内部位图形式表现。自动或不自动。

至于,具体细节实现。请看代码。

基本创建步骤:
1.创建文本的实例。注意构造函数内的参数,请设置好基本属性。

2.添加到显示对象中。然后填充setTxt方法,就可以呈现了。

package com.vini123.text
{
    import flash.display.GradientType;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Matrix;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    
    public class BaseTxt extends Sprite
    {
        protected var txtW:int;
        protected var txt:TextField;
        protected var txtInfo:Object;
        protected var speed:Number = 1;
        
        private var txtFormat:TextFormat;
        
        private var size:int;
        private var color:int;
        private var font:String;
        
        private var txtMask:Sprite;
        private var txtHGap:int = 8;
        
        private var isGradient:Boolean = false;
        private var gradientDistance:int = 10;
        private var colors:Array = [];
        private var alphas:Array = [];
        private var ratios:Array = [];
        private var tempOffset:int = 38;
        private var matrix:Matrix;
        
        /**
         * 
         * @param txtW 滚动文字的可视区域宽度
         * @param isGradient 是否有渐变遮罩
         * @param gradientDistance 渐变遮罩渐变色距离 (可以将.mask注释掉,查看效果)
         * @param size 滚动文字大小
         * @param color 滚动文字颜色
         * @param font 滚动文字字体
         * 
         */        
        public function BaseTxt(txtW:int , isGradient:Boolean , gradientDistance:int = 10, size:int = 13 , color:int = 0xffffff , font:String = "Microsoft Yahei")
        {
            this.txtW = txtW;
            this.isGradient = isGradient;
            this.gradientDistance = gradientDistance;
            this.size = size;
            this.color = color;
            this.font = font;
            
            this.gradientDistance = ((this.gradientDistance  * 2 + tempOffset) >255)?(int((255 -tempOffset)/2)):this.gradientDistance
            this.tempOffset = ((this.gradientDistance  * 2 + tempOffset) >255)?(255 - this.gradientDistance  * 2 -10):this.tempOffset;
            
            if(isGradient)
            {
                colors = [0xffffff,0xffffff,0xffffff,0xffffff];
                alphas = [0,100,100,0];
                matrix = new Matrix();
                ratios = [0 , gradientDistance , 255 - gradientDistance - tempOffset , 255];            
            }
            
            initialize();
        }
        
        private function initialize():void
        {
            txtFormat = new TextFormat();
            txtFormat.font = font;
            txtFormat.size = size;
            txtFormat.color = color;
            txtFormat.bold = true;
            
            txt = new TextField();
            txt.autoSize = TextFieldAutoSize.LEFT;
            txt.multiline = false;
            txt.wordWrap = false;
            txt.selectable = false;
            txt.mouseEnabled = txt.mouseWheelEnabled = false;
            addChild(txt);
            
            txt.defaultTextFormat = txtFormat;
            
            txtMask = new Sprite();
            addChild(txtMask);
            txt.mask = txtMask;
            
            txtInfo = {};
        }
        
        public function setSpeed(value:Number):void
        {
            speed = value;
        }
        
        public function setTxt(value:String):void
        {
            if(hasEventListener(Event.ENTER_FRAME))
            {
                removeEventListener(Event.ENTER_FRAME , enterHandler);
            }
            creatMask();
            start();
        }
        
        private function creatMask():void
        {
            if(isGradient)
            {
                matrix.createGradientBox(txtW, (txt.textHeight + txtHGap), Math.PI/1, 0, 0 );
                txtMask.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix );
                txtMask.graphics.drawRect(0, -txtHGap/2, txtW, (txt.textHeight + txtHGap));
                txtMask.graphics.endFill();
                txtMask.cacheAsBitmap = true;
                txt.cacheAsBitmap = true;
            }
            else
            {
                txtMask.graphics.clear();
                txtMask.graphics.beginFill(0x00ffff,0.45);
                txtMask.graphics.drawRect(0,-txtHGap/2,txtW,(txt.textHeight + txtHGap));
                txtMask.graphics.endFill();
            }
        }
        
        protected function enterHandler(e:Event):void
        {
            txt.x -=1;
            if(txt.x <= txtInfo.endPosition)
            {
                txt.x = 0;
            }
        }
        
        private function start():void
        {
            addEventListener(Event.ENTER_FRAME , enterHandler);
        }
        
        public override function get width():Number
        {
            return txtW;
        }
        
        public override function get height():Number
        {
            return txt.textHeight + txtHGap;
        }
    }
}

 

package com.vini123.text
{
    import flash.events.Event;
    
    /**
     * 
     * 循环不间断txt
     * 
     */    
    public class LoopRollTxt extends BaseTxt
    {
        public function LoopRollTxt(txtW:int, isGradient:Boolean, gradientDistance:int=10, size:int=13, color:int=0xffffff, font:String="Microsoft Yahei")
        {
            super(txtW, isGradient, gradientDistance, size, color, font);
        }
        
        public override function setTxt(value:String):void
        {
            txt.x = 0;
            txtInfo.txtNum = 0;
            txtInfo.txtSingleW = null;
            
            txt.text = value;
            txtInfo.txtSingleW = txt.textWidth;
            txt.text = "";
            
            var tempW:int = (txtInfo.txtSingleW > txtW )?txtInfo.txtSingleW:txtW;
            while(txt.textWidth < tempW * 2)
            {
                txt.appendText(value);
                txtInfo.txtNum ++;
            }
            
            super.setTxt(value);
            
            txtInfo.txtNum = Math.floor(txtInfo.txtNum/2);
            txtInfo.endPosition = - txtInfo.txtNum * txtInfo.txtSingleW;     
        }
        
        protected override function enterHandler(e:Event):void
        {
            txt.x -= speed;
            if(txt.x <= txtInfo.endPosition)
            {
                txt.x = 0;
            }
        }
    }
}

 

package com.vini123.text
{
    import flash.events.Event;
    
    /**
     * 
     * 迂回txt
     * 
     */    
    public class RoundaboutTxt extends BaseTxt
    {
        public function RoundaboutTxt(txtW:int, isGradient:Boolean, gradientDistance:int=10, size:int=13, color:int=0xffffff, font:String="Microsoft Yahei")
        {
            super(txtW, isGradient, gradientDistance, size, color, font);
        }
        
        public override function setTxt(value:String):void
        {
            txt.x = txtW;
            txt.text = value;
            super.setTxt(value);
        }
        
        protected override function enterHandler(e:Event):void
        {
            txt.x -= speed;
            if(txt.x <= -txt.textWidth)
            {
                txt.x = txtW;
            }
        }
    }
}

 

package com.vini123.data
{
    public class TxtData
    {
        private static var index:int = -1;
        private static var txtList:Array = [
            "走,一个人走。走的累了,心却碎了。爱,一个人爱。爱的哭了,哭的倦了。",
            "爱是一种方法,方法就是暂停。把她放在遥远,享受一片空灵",
            "君生我未生,我生君已老。隔了百年的光阴,万里的迢递。浮世肮脏,人心险恶。割裂了生和死。到哪里去寻找那一袭纯白如羽的华衣和那莲花般的素颜。",
            "秋风清,秋月明。落叶聚还散。寒鸦栖复惊。相思相见知何日,此时此夜难为情。",
            "我住长江头,君住长江尾。日日思君不见君,共饮长江水。此水几时休,此恨何时已。只愿君心似我心,定不负相思意。"
        ]
        
        public static function get text():String
        {
            index ++;
            index = (index >= txtList.length)?0:index;
            return txtList[index];    
        }
    }
}

 

package com.vini123
{
    import com.vini123.data.TxtData;
    import com.vini123.text.LoopRollTxt;
    import com.vini123.text.RoundaboutTxt;
    
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.utils.clearInterval;
    import flash.utils.setTimeout;
    
    [SWF(width = "680" , height="300" , frameRate="30" , backgroundColor = "0x005578")]
    public class RollTxt extends Sprite
    {
        private var loopRollTxt:LoopRollTxt;
        private var roundaboutTxt:RoundaboutTxt;
        private var container:Sprite;
        private var info:String;
        private var background:Sprite;
        private var time:int = 0;
        public function RollTxt()
        {
            addEventListener(Event.ADDED_TO_STAGE , addToStageHandler);
        }
        
        private function addToStageHandler(e:Event):void
        {    
            removeEventListener(Event.ADDED_TO_STAGE , addToStageHandler);
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            stage.addEventListener(Event.RESIZE , resizeHandler);
            
            initialize();
            resizeHandler();
        }
        
        private function initialize():void
        {
            background = new Sprite();
            addChild(background);
            
            container = new Sprite();
            addChild(container);
            
            loopRollTxt = new LoopRollTxt(300 , true , 80 ,20 ,0xffff00);
            container.addChild(loopRollTxt);
            roundaboutTxt = new RoundaboutTxt(300 , true ,80 ,20,0xffff00);
            container.addChild(roundaboutTxt);
        
            setTxt();
            roundaboutTxt.y = loopRollTxt.y + loopRollTxt.height + 30;
            time = setTimeout(setTxt,30 * 1000);
        }
        
        private function setTxt():void
        {
            info = TxtData.text;
            loopRollTxt.setSpeed(2);
            roundaboutTxt.setSpeed(2);
            loopRollTxt.setTxt(info);
            roundaboutTxt.setTxt(info);
        
            if(time >0)
            {
                clearInterval(time);
            }
            time = setTimeout(setTxt,30 * 1000);
        }
        private function resizeHandler(e:Event = null):void
        {
            container.y = (stage.stageHeight - container.height)>>1; 
            loopRollTxt.x = (stage.stageWidth - loopRollTxt.width)>>1;
            roundaboutTxt.x = (stage.stageWidth - roundaboutTxt.width)>>1;
            
            background.graphics.clear();
            background.graphics.beginFill(0x005578);
            background.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
            background.graphics.endFill();
        }
    }
}

 

原文件下载:http://pan.baidu.com/s/1kTsULdt
提取密码:wf31