用xml加载图片
<?xml version="1.0" encoding="UTF-8"?> <root len="5"> <pic name="work" path="resource/pic/pic1.jpg" size="200,250"></pic> <pic name="wu" path="resource/pic/pic2.jpg" size="100,50"></pic> <pic name="girl" path="resource/pic/pic3.jpg" size="30,60"></pic> <pic name="jd" path="resource/pic/pic4.jpg" size="60,90"></pic> <pic name="together" path="resource/pic/pic5.jpg" size="120,80"></pic> </root>
以上为xml文档
通过xml来加载图片分两个重要步骤,首先用URLLoader类来加载xml文档(用for each方法遍历xml文档,获取图片信息),然后用Loader类来加载图片
package
{
/**
* @author zoe
*
*/
import data.PhotoData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import view.Pic;
public class Main extends Sprite
{
private var picLoader:URLLoader;
private var loader:Loader;
private var photoDataOList:Array;
//此变量很重要,是个索引值,用来处理依次加载的photoDataOList里的图片的。
//每加载完一张照片,此值会+1,直到所有的图片都加载完
private var crtLoadedPicIndex:int;
public function Main()
{
init();
}
private function init():void
{
crtLoadedPicIndex = 0;
//给这个数组初始化,此数组是用来存放解析后的xml文件里的数据
photoDataOList = [];
//创建了一个URlLoadrer的实例并赋值给picLoadr这个变量,
//picLoader是用来加载远程的xml文件的
picLoader = new URLLoader();
//让picLoader监听自己的加载事件,当被记载的文件加载完成之后,picLoader会自动派发这个事件
picLoader.addEventListener(Event.COMPLETE,completeHandler);
//根据传入的URL地址开始加载
picLoader.load(new URLRequest("resource/xml/pictureLoaderXml.xml"));
}
private function completeHandler(evt:Event):void
{
//获取到通过URLLoader加载到的文件数据,此时的xml变量指向的就是xml文件里的根节点(root节点)
var xml:XML = new XML(picLoader.data);
// trace(xml);
//从上面的xml这个变量中获取到pic这一串节点,并赋值给photoXMlList
var photoXmlList:XMLList = XMLList(xml.pic);
// trace(XML(photoXmlList[1]).@name);
// trace(XML(photoXmlList[1]).@path);
//使用for each来遍历整串的xmllist,即遍历每一个pic节点
for each(var elem:XML in photoXmlList)
{
//先创建了一个PhotoData的实例,并复制给currrentData
var currrentData:PhotoData = new PhotoData();
//从当前for each遍历到的xml中获取到数据,并赋值给currrentData相应的属性
currrentData.picName = elem.@name;
currrentData.picPath = elem.@path;
//将赋好值之后的图片数据对象currentData存储到数组photoDataOList的最后面
photoDataOList.push(currrentData);
}
addPics();
}
private function addPics():void
{
loader = new Loader();
//loader就是个工具,是用来加载一些可视化的东西,
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,picloaderHandler);
//第一次调用loadpic();
loadpic();
}
//次方法会被不断的调用
private function loadpic():void
{
//根据crtLoadedPicIndex这个变量来加载图片,第一次加载是,crtLoadedPicIndex的值是0
var crtPhotoData:PhotoData = PhotoData(photoDataOList[crtLoadedPicIndex]);
loader.load(new URLRequest(crtPhotoData.picPath));
}
protected function picloaderHandler(event:Event):void
{
var crtLoadedPicSource:Bitmap = Bitmap(LoaderInfo(event.target).content);
这里用定义变量为Bitmap类型,因为as中图片都是位图
// var picdata:PhotoData = photoDataOList[crtLoadedPicIndex];
// picdata.picSource = crtLoadedPicSource;
//
// var pic:Pic = new Pic();
// pic.setData(picdata);
addChild(crtLoadedPicSource);
crtLoadedPicSource.x = crtLoadedPicIndex * 20;
crtLoadedPicSource.y = crtLoadedPicIndex * 20;
crtLoadedPicIndex++;
if(crtLoadedPicIndex >= photoDataOList.length)
{
//over
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,picloaderHandler);
loader = null;
return;
}
loadpic();
}
}
}