·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3.0写的很赞的Loading类源代码(2)

时间:2014-01-03 11:22zpointer.iteye.com
packagenet.xueyitong.controls { importcom.greensock.TweenLite; importflash.display.Sprite; importflash.display.StageAlign; importflash.events.TimerEvent; importflash.text.TextField; importflash.text.

  1. package net.xueyitong.controls 
  2. { 
  3.     import com.greensock.TweenLite; 
  4.     import flash.display.Sprite; 
  5.     import flash.display.StageAlign; 
  6.     import flash.events.TimerEvent; 
  7.     import flash.text.TextField; 
  8.     import flash.text.TextFieldAutoSize; 
  9.     import flash.text.TextFormat; 
  10.     import flash.text.TextFormatAlign; 
  11.     import flash.utils.Timer; 
  12.      
  13.     /** 
  14.      * 纯代码的一个圆形Loading组件 
  15.      * 该组件需要com.greensock.TweenLite缓动类库 
  16.      * @author zkl QQ:344209679 
  17.      * 学习交流 CuPlayer.com
  18.      */ 
  19.     public class Loading extends Sprite  
  20.     { 
  21.         //loading的圆点表列 
  22.         private var circles:Array = new Array(); 
  23.         //loading的圆点数 
  24.         private var circleNum:int = 12; 
  25.         //loading的半径 
  26.         private var radius:Number = 20; 
  27.         //loading的圆点的颜色 
  28.         private var color:uint = 0xD4D4D4; 
  29.         //小圆点的半径 
  30.         private var subRadius:Number = 4; 
  31.         //每个圆点之间的延迟计算 
  32.         private var delay:int = 2; 
  33.         private var frameCount:int = 0; 
  34.         //当前圆点的下标 
  35.         private var currentCircleIndex:int = 0; 
  36.         //loading的圆点实例 
  37.         private var currentCircle:LoadingCircle; 
  38.         //loading显示文本域 
  39.         private var percentField:TextField; 
  40.          
  41.         //timer 
  42.         public var frameRate:int; 
  43.         private var loadingTimer:Timer; 
  44.         private var timerPeriod:Number; 
  45.         // 
  46.          
  47.         /** 
  48.          * 创建一个Loading实例 
  49.          * @param   radius :String 半径 
  50.          * @param   color :uint 颜色 
  51.          */ 
  52.         public function Loading(radius:Number = 25, color:uint = 0xD4D4D4, subRadius:Number = 4) { 
  53.             this.radius = radius; 
  54.             this.color = color; 
  55.             this.subRadius = subRadius; 
  56.             init(); 
  57.         } 
  58.         /** 
  59.          * 运行Loading组件 cuplayer.com
  60.          * @param   frameRate : int 帧频 
  61.          */ 
  62.         public function run(frameRate:int = 30):void { 
  63.             percentField.text = ""; 
  64.             this.frameRate = frameRate; 
  65.             timerPeriod = 1000 / frameRate; 
  66.             loadingTimer = new Timer(timerPeriod); 
  67.             loadingTimer.addEventListener(TimerEvent.TIMER, runLoading); 
  68.             loadingTimer.start(); 
  69.         } 
  70.         /** 
  71.          * 停止loading组件 
  72.          */ 
  73.         public function stop():void { 
  74.             loadingTimer.removeEventListener(TimerEvent.TIMER, runLoading); 
  75.             loadingTimer.stop(); 
  76.         } 
  77.         /** 
  78.          * 移除Loading组件 
  79.          */ 
  80.         public function remove():void { 
  81.             loadingTimer.stop(); 
  82.             loadingTimer.removeEventListener(TimerEvent.TIMER, runLoading); 
  83.             for (var i:int = 0; i < this.numChildren; i++ ) { 
  84.                 this.removeChild(getChildAt(0)); 
  85.             } 
  86.             this.removeChild(percentField); 
  87.         } 
  88.         /** 
  89.          * 显示loading文本域 一般是百分比进度 
  90.          * @param   per :String 显示的字符串 
  91.          */ 
  92.         public function percent(per:String):void { 
  93.             perpercentField.text = per; 
  94.             percentField.setTextFormat(textFormat()); 
  95.         } 
  96.         /////////////////////////////////////////////////////////////////////////////// 
  97.         // protected 
  98.         protected function runLoading(e:TimerEvent):void { 
  99.             frameCount++; 
  100.             if (frameCount >= delay) { 
  101.                 render(); 
  102.             } 
  103.         } 
  104.         //初始化loading,排列每个圆点 
  105.         protected function init():void { 
  106.             var step:Number = Math.PI * 2 / circleNum; 
  107.             var angle:Number; 
  108.             for (var i:int = 0; i < circleNum; i++ ) { 
  109.                 currentCircle = new LoadingCircle(subRadius, color); 
  110.                 circles.push(currentCircle); 
  111.                 this.addChild(currentCircle); 
  112.                 angle = step * i; 
  113.                 currentCircle.x = Math.cos(angle) * radius; 
  114.                 currentCircle.y = Math.sin(angle) * radius; 
  115.                 currentCircle.alpha = 0; 
  116.             } 
  117.             createPercentField(); 
  118.         } 
  119.         //缩小 cuplayer.com
  120.         protected function render():void { 
  121.             frameCount = 0; 
  122.             currentCircleIndex++; 
  123.             if ( currentCircleIndex >= circles.length) { 
  124.                 currentCircleIndex = 0; 
  125.             } 
  126.             currentCircle = circles[currentCircleIndex]; 
  127.             currentCirclecurrentCircle.scaleX = currentCircle.scaleY 
    = 
    currentCircle.alpha = 1; 
  128.             TweenLite.to(currentCircle, 50 / frameRate, 
    { scaleX:0, scaleY:0, alpha:0 } ); 
  129.         } 
  130.         //进度文本域 
  131.         protected function createPercentField():void { 
  132.             percentField = new TextField(); 
  133.             percentField.width = 40; 
  134.             percentField.height = 16; 
  135.             percentField.x = -20; 
  136.             percentField.y = -8; 
  137.             percentField.autoSize = TextFieldAutoSize.CENTER; 
  138.             percentField.selectable = false; 
  139.             this.addChild(percentField); 
  140.         } 
  141.         //文本格式 
  142.         protected function textFormat():TextFormat { 
  143.             var format:TextFormat = new TextFormat(); 
  144.             format.font = "宋体"; 
  145.             format.size = 12; 
  146.             format.color = this.color; 
  147.             format.align = TextFormatAlign.CENTER; 
  148.             return format; 
  149.         } 
  150.         //protected 
  151.          
  152.          
  153.     }//end class 
  154.          
  155. }//end package 
  156.  
  157. import flash.display.Sprite; 
  158.      
  159. /** 
  160.  * loading组件的小圆圈 
  161.  * @author zkl 
  162.  */ 
  163. class LoadingCircle extends Sprite 
  164. { 
  165.     public function LoadingCircle(radius:Number = 4, color:uint = 0x7B7B7B) { 
  166.         this.graphics.beginFill(color); 
  167.         this.graphics.drawCircle( -radius / 2, -radius / 2, radius); 
  168.         this.graphics.endFill(); 
  169.     } 
  170.          
  171. }//end class 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器