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>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值