·您当前的位置:首页 > 技术教程 > Flex技术 >

[Flex]flex做的加载进度条loading源代码实例

时间:2014-07-14 12:27酷播
[Flex]flex做的加载进度条loading源代码实例 ,flex进度条,as3进度条

[Flex]flex做的加载进度条loading源代码实例

  1. package com.iman.sps.sheet.ascript 
  2.     import flash.display.Sprite; 
  3.     import flash.display.*;  
  4.     import flash.events.*;  
  5.     import flash.filters.BitmapFilterQuality;  
  6.     import flash.filters.BlurFilter;  
  7.     import flash.geom.Matrix;  
  8.     import flash.net.*;  
  9.     import flash.text.TextField;  
  10.     import flash.utils.*;  
  11.     import flash.system.Capabilities;  
  12.      
  13.     import mx.events.*;  
  14.  
  15.     import mx.preloaders.IPreloaderDisplay; 
  16.      
  17.     public class MyProgressBar extends Sprite implements IPreloaderDisplay 
  18.     { 
  19.         [Embed(source="../image/logo.gif"mimeType="application/octet-stream")]  
  20.         public var _loadingLogo:Class;  
  21.          
  22.         private var dpbImageControl:Loader;  
  23.         private var _barSprite:Sprite;  
  24.         private var progressText:TextField;         
  25.         private var ProgressBarSpritIsAdded:Boolean = false;  
  26.  
  27.         public function MyProgressBar() 
  28.         { 
  29.             super(); 
  30.         } 
  31.          
  32.         // Specify the event listeners.  
  33.         public function set preloader(preloader:Sprite):void {  
  34.             //Listen for 正在下载  
  35.             preloader.addEventListener(ProgressEvent.PROGRESS, handleProgress);  
  36.             //Listen for 下载完成  
  37.             preloader.addEventListener(Event.COMPLETE, handleComplete);  
  38.             //Listen for 正在初始化  
  39.             preloader.addEventListener(FlexEvent.INIT_PROGRESS, handleInitProgress);  
  40.             //Listen for 初始化完成  
  41.             preloader.addEventListener(FlexEvent.INIT_COMPLETE, handleInitComplete);  
  42.         }  
  43.          
  44.         // Initialize the Loader control in the override  
  45.         // of IPreloaderDisplay.initialize().  
  46.         public function initialize():void {  
  47.             //添加logo图  
  48.             dpbImageControl = new Loader();        
  49.             dpbImageControl.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_completeHandler);  
  50.             dpbImageControl.loadBytes(new _loadingLogo() as ByteArray);  
  51.             //dpbImageControl.load(new URLRequest("loadinglogo.swf"));  
  52.         }  
  53.          
  54.          
  55.         // After the SWF file loads, set the size of the Loader control.  
  56.         private function loader_completeHandler(event:Event):void  
  57.         {          
  58.             addChild(dpbImageControl);  
  59.             dpbImageControl.width = 136;  
  60.             dpbImageControl.height54;  
  61.             dpbImageControl.x = this.stage.stageWidth/2 - dpbImageControl.width/2;  
  62.             dpbImageControl.y = this.stage.stageHeight/2 - dpbImageControl.height/2 - 40;  
  63.         }    
  64.          
  65.         //  
  66.         private function addProgressBarSprit():void{  
  67.             //绘制背景渐变  
  68.             var matrix:Matrix=new Matrix();  
  69.             matrix.createGradientBox(this.stage.stageWidth,this.stage.stageHeight,Math.PI/2);  
  70.             var colors:Array=[0x3399ff,0xFFFFFF];  
  71.             var alphas:Array=[1,1];  
  72.             var ratios:Array=[0,255];  
  73.             this.graphics.lineStyle();  
  74.             this.graphics.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,matrix);         
  75.             this.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);  
  76.             this.graphics.endFill();  
  77.              
  78.             //绘制中心白色发光  
  79.             var _Sprite1:Sprite = new Sprite();  
  80.             addChild(_Sprite1);  
  81.             _Sprite1.graphics.beginFill(0xffffff,0.45);  
  82.             _Sprite1.graphics.drawEllipse(this.stage.stageWidth/2-130, this.stage.stageHeight/2-90, 280, 100);  
  83.             _Sprite1.graphics.endFill();  
  84.             //滤镜实现发光边缘柔和             
  85.             var blur:BlurFilter = new BlurFilter();  
  86.             blur.blurX = 100;  
  87.             blur.blurY = 50;  
  88.             blur.quality = BitmapFilterQuality.HIGH;  
  89.             _Sprite1.filters = [blur];  
  90.              
  91.             //-------------------------------------------------  
  92.              
  93.             //绘制进度条背景  
  94.             var _Sprite2:Sprite = new Sprite();  
  95.             addChild(_Sprite2);  
  96.             _Sprite2.graphics.lineStyle(1, 0xCCCCCC);  
  97.             _Sprite2.graphics.beginFill(0xFFFFFF);  
  98.             _Sprite2.graphics.drawRect((this.stage.stageWidth/2 - 152), (this.stage.stageHeight/2 - 10), 304, 20);    
  99.             _Sprite2.graphics.endFill();  
  100.              
  101.             //-------------------------------------------------  
  102.              
  103.             //加载进度条Sprite  
  104.             _barSprite = new Sprite();  
  105.             addChild(_barSprite);  
  106.             _barSprite.x = this.stage.stageWidth/2 - 150;  
  107.             _barSprite.y = this.stage.stageHeight/2 - 8;  
  108.              
  109.             //-------------------------------------------------  
  110.              
  111.             //加载进度条文字  
  112.             progressText = new TextField();  
  113.             addChild(progressText);  
  114.             progressText.textColor = 0x333333;  
  115.             progressText.width = 300;  
  116.             progressText.height = 18;  
  117.             progressText.x = this.stage.stageWidth/2 - 152;  
  118.             progressText.y = this.stage.stageHeight/2 + 20;  
  119.         }  
  120.          
  121.         //刷新进度条  
  122.         private function drawProgressBar(bytesLoaded:Number, bytesTotal:Number):void  
  123.         {    
  124.             if (_barSprite != null && progressText != null){  
  125.                 var g:Graphics = _barSprite.graphics;  
  126.                 g.clear();  
  127.                 g.beginFill(0xCCCCCC);  
  128.                 g.drawRect(0, 0, 300*(bytesLoaded/bytesTotal),16);    
  129.                 g.endFill();     
  130.                  
  131.                 var matrix:Matrix=new Matrix();  
  132.                 matrix.createGradientBox(300*(bytesLoaded/bytesTotal),16,Math.PI/2);  
  133.                 var colors:Array=[0x0099CC,0x99cc77];  
  134.                 var alphas:Array=[1,1];  
  135.                 var ratios:Array=[0,255];  
  136.                 g.lineStyle();  
  137.                 g.beginGradientFill(GradientType.LINEAR,colors,alphas,ratios,matrix);         
  138.                 g.drawRect(0,0,300*(bytesLoaded/bytesTotal),16);  
  139.                 g.endFill();   
  140.                  
  141.                  
  142.             }  
  143.         }  
  144.          
  145.         //正在下载的进度  
  146.         private function handleProgress(event:ProgressEvent):void {  
  147.             //第一次处理时绘制进度条Sprit  
  148.             if (ProgressBarSpritIsAdded == false){  
  149.                 ProgressBarSpritIsAdded = true;  
  150.                 addProgressBarSprit();  
  151.             }  
  152.              
  153.             if (progressText != null){  
  154.                 progressText.text = "下载进度:已下载 " + event.bytesLoaded + " byte,总大小 " + event.bytesTotal + " byte.";  
  155.             }          
  156.             drawProgressBar(event.bytesLoaded, event.bytesTotal);  
  157.         }  
  158.          
  159.         private function handleComplete(event:Event):void {  
  160.             if (progressText != null){  
  161.                 progressText.text = "下载完成.";  
  162.             }  
  163.             drawProgressBar(1,1);         
  164.         }  
  165.          
  166.         private function handleInitProgress(event:Event):void {  
  167.             if (progressText != null){  
  168.                 progressText.text = "正在初始化...";  
  169.             }  
  170.             drawProgressBar(1,1);  
  171.         }  
  172.          
  173.         private function handleInitComplete(event:Event):void {  
  174.             if (progressText != null){  
  175.                 progressText.text = "初始化完成.";  
  176.             }  
  177.             drawProgressBar(1,1);          
  178.              
  179.             //0.03秒后抛出完成事件  
  180.             var timer:Timer = new Timer(300,1);  
  181.             timer.addEventListener(TimerEvent.TIMER, dispatchComplete);  
  182.             timer.start();       
  183.         }  
  184.          
  185.         private function dispatchComplete(event:TimerEvent):void {  
  186.             dispatchEvent(new Event(Event.COMPLETE));  
  187.         }  
  188.  
  189.          
  190.         public function get backgroundAlpha():Number 
  191.         { 
  192.             return 0; 
  193.         } 
  194.          
  195.         public function set backgroundAlpha(value:Number):void 
  196.         { 
  197.         } 
  198.          
  199.         public function get backgroundColor():uint 
  200.         { 
  201.             return 0; 
  202.         } 
  203.          
  204.         public function set backgroundColor(value:uint):void 
  205.         { 
  206.         } 
  207.          
  208.         public function get backgroundImage():Object 
  209.         { 
  210.             return null; 
  211.         } 
  212.          
  213.         public function set backgroundImage(value:Object):void 
  214.         { 
  215.         } 
  216.          
  217.         public function get backgroundSize():String 
  218.         { 
  219.             return null; 
  220.         } 
  221.          
  222.         public function set backgroundSize(value:String):void 
  223.         { 
  224.         } 
  225.          
  226.          
  227.          
  228.         public function get stageHeight():Number 
  229.         { 
  230.             return 0; 
  231.         } 
  232.          
  233.         public function set stageHeight(value:Number):void 
  234.         { 
  235.         } 
  236.          
  237.         public function get stageWidth():Number 
  238.         { 
  239.             return 0; 
  240.         } 
  241.          
  242.         public function set stageWidth(value:Number):void 
  243.         { 
  244.         } 
  245.     } 

使用方法:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"  
  3.                xmlns:s="library://ns.adobe.com/flex/spark" 
  4.                 preloader="com.iman.sps.sheet.ascript.MyProgressBar"> 

 

热门文章推荐

请稍候...

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

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