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

 

热门文章推荐

请稍候...

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

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