ActionScript 网络图片加载

 

package {
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.display.DisplayObject;
	
	
	[SWF(width="500",height="300",frameRate="20",backgroundColor="#FFFFFF")]
	public class ImageLoader extends Sprite
	{
		private var loaderIndex:int = 0;
		private var showIndex:int = 0;
		private var imageList:XMLList;
		private var sucImageCount:int = 0;
		private var txt:TextField;
		
		public function ImageLoader()
		{
			txt = new TextField();
			txt.selectable = false;
			txt.text = "LoadingXML配置文件";
			this.addChild(txt);
			
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest("image.xml");
			loader.addEventListener(Event.COMPLETE,onLoadComplete);
			loader.load(request);
		}
		
		private function onLoadComplete(event:Event):void
		{
			var lder:URLLoader = event.target as URLLoader;
			var xml:XML = new XML(lder.data);
			trace("XML数据已经载入");
			imageList = xml.image;
			loadImage();
		}
		
		private function loadImage():void
		{
			var url:String = imageList[loaderIndex].@url;
			var urlRequest:URLRequest = new URLRequest(url);
			var loader:Loader = new Loader();
			trace("load image from url "+url);
			txt.text = "正在载入图片("+(sucImageCount+1)+"/"+imageList.length()+")";
			loader.load(urlRequest);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onImageLoadComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
		}
		
		private function onError(event:IOErrorEvent):void
		{
			trace("load error");
			loaderIndex++;
			loadImage();
		}
		
		private function onImageLoadComplete(e:Event):void
		{
			var img:LoaderInfo = e.target as LoaderInfo;
			img.content.visible = false;
			this.addChild(img.content);
			if (loaderIndex<imageList.length()-1)
			{
				sucImageCount++;
				loaderIndex++;
				trace("load success");
				loadImage();
			}
			else
			{
				this.removeChild(txt);
				this.stage.addEventListener(MouseEvent.CLICK,showNextImage);
				showNextImage(null);
			}
		}
		
		private function showNextImage(e:MouseEvent):void
		{
			trace("showNextImage");
			var img:DisplayObject = this.getChildAt(showIndex);
			if (img.visible==false)
			{
				img.visible = true;
				return ;
			}
			else
			{
				img.visible = false;
			}
			if (showIndex<this.numChildren-1) showIndex++;
			else showIndex=0;
			img = this.getChildAt(showIndex);
			img.visible = true;
		}
	}
}

 image.xml

<images>
	<image url="http://www.google.cn/intl/zh-CN/images/logo_cn.gif"/>
	<image url="http://www.baidu.com/img/baidu_logo.gif"/>
	<image url="http://cn.yimg.com/i/onesearch/1210/oms_001.gif"/>
	<image url="http://statics.verycd.com/images/logo.png"/>
	<image url="http://img.gougou.com/image/rank/logo2.png"/>
</images>
 

 

你可能感兴趣的:(.net,xml,Google,Flash,actionscript)