flex自定义preloader

package cn.ishion.component
{
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import mx.events.FlexEvent;
	import mx.preloaders.IPreloaderDisplay;

	public class ConstomPreloader extends Sprite implements IPreloaderDisplay
	{
		private var url:String="loader1.swf";
		private var loadContent:Object;//加载的内容
		private var swfLoader:Loader;//加载swf东西
		private var hasLoad:Boolean=false; //

		private var preTxt:TextField;

		private var swfWidth:Number=800;
		private var swfHeight:Number=600;
		
		private var _pregressRect:Rectangle;
		
		private var progressSprite:Sprite;
		
		public function get pregressRect():Rectangle{
			return new Rectangle(0,0,400,10);
		}
		
		public function ConstomPreloader()
		{
			super();
			loadSWF();
		}

		private function loadSWF():void
		{
			swfLoader=new Loader();
			
			swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			swfLoader.load(new URLRequest(url));
		}

		private function onComplete(event:Event):void
		{
			var loadInfo:LoaderInfo=event.target as LoaderInfo;
			
					
			this.loadContent=loadInfo.content;
			this.hasLoad=true;
			createChild()
		}

		private function createChild():void
		{
			
			this.loadContent.x=Object(this.stage.stageWidth / 2 - swfWidth / 2);
			this.loadContent.y=Object(this.stage.stageHeight / 2 - swfHeight / 2);
			
			this.addChild(this.loadContent as DisplayObject);
			
			//搞不明白,stage.width stage.stageWidth
			trace(this.stageWidth+"::"+this.stageHeight);
			
			trace(this.stage.stageWidth+":::"+this.stage.stageHeight);
			
			trace(this.stage.stageWidth+":::"+this.stage.stageHeight);
			
			trace(this.loadContent.stage.width+"ssss"+this.loadContent.stage.height);
			
			var contentPoint:Point=new Point;
			contentPoint.x=this.stage.stageWidth/2-this.swfWidth/2;
			contentPoint.y=this.stage.stageHeight/2-this.swfHeight/2;
			preTxt=new TextField();
			preTxt.selectable=false;
			preTxt.x=contentPoint.x  + 200;
			preTxt.y=contentPoint.y + 400;
			
		
			preTxt.text="正在加载中";
			this.addChild(preTxt);
			
			this.progressSprite=new Sprite();
			this.progressSprite.x=this.preTxt.x;
			this.progressSprite.y=this.preTxt.y+20;
			var gra:Graphics=this.progressSprite.graphics
			gra.beginFill(0x0000ff,1);
			gra.drawRect(this.pregressRect.x,this.pregressRect.y,this.pregressRect.width,this.pregressRect.height);
			gra.endFill();
			this.addChild(this.progressSprite);
			
		}

		public function get backgroundAlpha():Number
		{
			return 0;
		}

		public function set backgroundAlpha(value:Number):void
		{

		}

		public function get backgroundColor():uint
		{
			return 0;
		}

		public function set backgroundColor(value:uint):void
		{

		}

		public function get backgroundImage():Object
		{
			return null;
		}

		public function set backgroundImage(value:Object):void
		{
		}

		public function get backgroundSize():String
		{
			return null;
		}

		public function set backgroundSize(value:String):void
		{
			
		}

		public function set preloader(obj:Sprite):void
		{
			//Listen for 正在下载
			obj.addEventListener(ProgressEvent.PROGRESS, handleProgress);
			//Listen for 下载完成
			obj.addEventListener(Event.COMPLETE, handleComplete);
			//Listen for 正在初始化
			obj.addEventListener(FlexEvent.INIT_PROGRESS, handleInitProgress);
			//Listen for 初始化完成
			obj.addEventListener(FlexEvent.INIT_COMPLETE, handleInitComplete);
		}

		protected function handleProgress(event:ProgressEvent):void
		{
			this.setProgress(event.bytesLoaded, event.bytesTotal);
		}

		protected function handleComplete(event:Event):void
		{
			if(this.hasLoad){
				this.preTxt.text="应用程序加载完成";
			}
			
		}

		protected function setProgress(completed:Number, total:Number):void
		{
			if (!isNaN(completed) && !isNaN(total) && completed >= 0 && total > 0)
			{
				var value:Number=Number(completed);
				var maximum:Number=Number(total);
				draw(value, maximum);
			}
		}

		private function draw(value:Number, maxnum:Number):void
		{
			if (this.hasLoad)
			{
				var currentLoad:Number=this.getPercentLoaded(value,maxnum);
				this.preTxt.text="当前加载" +currentLoad  + "%";
				
				var g:Graphics=this.progressSprite.graphics;
				g.beginFill(0xcc6699,1);
				g.drawRect(this.pregressRect.x,this.pregressRect.y+2,this.pregressRect.width*currentLoad/100,this.pregressRect.height-4);
				g.endFill();
				
			}

		}

		protected function getPercentLoaded(loaded:Number, total:Number):Number
		{
			var perc:Number;

			if (loaded == 0 || total == 0 || isNaN(total) || isNaN(loaded))
				return 0;
			else
				perc=100 * loaded / total;

			if (isNaN(perc) || perc <= 0)
				return 0;
			else if (perc > 99)
				return 99;
			else
				return Math.round(perc);
		}

		protected function handleInitProgress(event:FlexEvent):void
		{
			if(this.hasLoad){
				this.preTxt.text="应用程序初始化";
			}
			
		}

		protected function handleInitComplete(event:FlexEvent):void
		{
			this.preTxt.text="应用程序初始化完成,开始进入主见面"
			this.loadContent.addEventListener("framequit",clearLoad);
			this.loadContent.gotoAndPlay("complete");
			
//			var id:int=setTimeout(clearLoad,2000,id);
			
			
		}
		private function clearLoad(event:Event):void{
			this.loadContent.removeEventListener("framequit",clearLoad);
			this.removeChild(this.progressSprite);
			this.removeChild(this.loadContent as DisplayObject);
			this.removeChild(this.preTxt);
			this.swfLoader.unload();//卸载资源
			this.dispatchEvent(new Event(Event.COMPLETE));
		}

		public function get stageHeight():Number
		{
			return 500;
		}

		public function set stageHeight(value:Number):void
		{

		}

		public function get stageWidth():Number
		{
			return 375;
		}

		public function set stageWidth(value:Number):void
		{
		}

		public function initialize():void
		{

		}



	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值