[AS3]as3.0写的很赞的Loading类源代码(2)
packagenet.xueyitong.controls { importcom.greensock.TweenLite; importflash.display.Sprite; importflash.display.StageAlign; importflash.events.TimerEvent; importflash.text.TextField; importflash.text.
- package net.xueyitong.controls
- {
- import com.greensock.TweenLite;
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.events.TimerEvent;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- import flash.text.TextFormatAlign;
- import flash.utils.Timer;
- /**
- * 纯代码的一个圆形Loading组件
- * 该组件需要com.greensock.TweenLite缓动类库
- * @author zkl QQ:344209679
- * 学习交流 CuPlayer.com
- */
- public class Loading extends Sprite
- {
- //loading的圆点表列
- private var circles:Array = new Array();
- //loading的圆点数
- private var circleNum:int = 12;
- //loading的半径
- private var radius:Number = 20;
- //loading的圆点的颜色
- private var color:uint = 0xD4D4D4;
- //小圆点的半径
- private var subRadius:Number = 4;
- //每个圆点之间的延迟计算
- private var delay:int = 2;
- private var frameCount:int = 0;
- //当前圆点的下标
- private var currentCircleIndex:int = 0;
- //loading的圆点实例
- private var currentCircle:LoadingCircle;
- //loading显示文本域
- private var percentField:TextField;
- //timer
- public var frameRate:int;
- private var loadingTimer:Timer;
- private var timerPeriod:Number;
- //
- /**
- * 创建一个Loading实例
- * @param radius :String 半径
- * @param color :uint 颜色
- */
- public function Loading(radius:Number = 25, color:uint = 0xD4D4D4, subRadius:Number = 4) {
- this.radius = radius;
- this.color = color;
- this.subRadius = subRadius;
- init();
- }
- /**
- * 运行Loading组件 cuplayer.com
- * @param frameRate : int 帧频
- */
- public function run(frameRate:int = 30):void {
- percentField.text = "";
- this.frameRate = frameRate;
- timerPeriod = 1000 / frameRate;
- loadingTimer = new Timer(timerPeriod);
- loadingTimer.addEventListener(TimerEvent.TIMER, runLoading);
- loadingTimer.start();
- }
- /**
- * 停止loading组件
- */
- public function stop():void {
- loadingTimer.removeEventListener(TimerEvent.TIMER, runLoading);
- loadingTimer.stop();
- }
- /**
- * 移除Loading组件
- */
- public function remove():void {
- loadingTimer.stop();
- loadingTimer.removeEventListener(TimerEvent.TIMER, runLoading);
- for (var i:int = 0; i < this.numChildren; i++ ) {
- this.removeChild(getChildAt(0));
- }
- this.removeChild(percentField);
- }
- /**
- * 显示loading文本域 一般是百分比进度
- * @param per :String 显示的字符串
- */
- public function percent(per:String):void {
- perpercentField.text = per;
- percentField.setTextFormat(textFormat());
- }
- ///////////////////////////////////////////////////////////////////////////////
- // protected
- protected function runLoading(e:TimerEvent):void {
- frameCount++;
- if (frameCount >= delay) {
- render();
- }
- }
- //初始化loading,排列每个圆点
- protected function init():void {
- var step:Number = Math.PI * 2 / circleNum;
- var angle:Number;
- for (var i:int = 0; i < circleNum; i++ ) {
- currentCircle = new LoadingCircle(subRadius, color);
- circles.push(currentCircle);
- this.addChild(currentCircle);
- angle = step * i;
- currentCircle.x = Math.cos(angle) * radius;
- currentCircle.y = Math.sin(angle) * radius;
- currentCircle.alpha = 0;
- }
- createPercentField();
- }
- //缩小 cuplayer.com
- protected function render():void {
- frameCount = 0;
- currentCircleIndex++;
- if ( currentCircleIndex >= circles.length) {
- currentCircleIndex = 0;
- }
- currentCircle = circles[currentCircleIndex];
- currentCirclecurrentCircle.scaleX = currentCircle.scaleY
= currentCircle.alpha = 1;- TweenLite.to(currentCircle, 50 / frameRate,
{ scaleX:0, scaleY:0, alpha:0 } );- }
- //进度文本域
- protected function createPercentField():void {
- percentField = new TextField();
- percentField.width = 40;
- percentField.height = 16;
- percentField.x = -20;
- percentField.y = -8;
- percentField.autoSize = TextFieldAutoSize.CENTER;
- percentField.selectable = false;
- this.addChild(percentField);
- }
- //文本格式
- protected function textFormat():TextFormat {
- var format:TextFormat = new TextFormat();
- format.font = "宋体";
- format.size = 12;
- format.color = this.color;
- format.align = TextFormatAlign.CENTER;
- return format;
- }
- //protected
- }//end class
- }//end package
- import flash.display.Sprite;
- /**
- * loading组件的小圆圈
- * @author zkl
- */
- class LoadingCircle extends Sprite
- {
- public function LoadingCircle(radius:Number = 4, color:uint = 0x7B7B7B) {
- this.graphics.beginFill(color);
- this.graphics.drawCircle( -radius / 2, -radius / 2, radius);
- this.graphics.endFill();
- }
- }//end class
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...