<?xml version="1.0" encoding="utf-8"?><mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
initialize="appInit()"
layout="absolute"
viewSourceURL="srcview/index.html"
backgroundGradientAlphas="[1.0, 1.0]"
backgroundGradientColors="[#FFFFFF, #FFFFFF]"><mx:Script><![CDATA[// Progressive Image Loading
// create by Ted Patrick ted@adobe.com
importflash.net.URLStream;
importflash.net.URLRequest;
importflash.utils.getTimer;
// loader to hold the loaded bytes
publicvarloader:Loader;
// URLStream to load the image bytes
publicvarimageStream:URLStream;
// ByteArray to hold aggregate image data
publicvarimageData:ByteArray;
// Set up initial conditions when the application initializes
publicfunctionappInit():void{//create a URLStream to load data in. We will reuse this!
imageStream=newURLStream();
//add some event listeners for PROGRESS and COMPLETE
imageStream.addEventListener(ProgressEvent.PROGRESS , imageStreamProgress);
imageStream.addEventListener(Event.COMPLETE , imageStreamComplete);
//create a fresh loader instance
loader=newLoader();
// add the new loader into the imageCanvas
// since loader isn't a UIComponent we use the rawChildren
// not a good practice but it works!
imageCanvas.rawChildren.addChild(loader);
}publicfunctionimageStreamProgress(event:Event):void{// if there are no bytes do nothing
if(imageStream.bytesAvailable== 0 )return// ooo bytes process the image data
this.processImageData();
}publicfunctionimageStreamComplete(event:Event):void{// if connected, stop that.
if(imageStream.connected)imageStream.close();
// lets refresh the displayList after rendering cycle
imageCanvas.callLater(this.processImageData);
}publicfunctionprocessImageData():void{// if connected, read all the bytes that have been loaded into the aggregate bytearray
if(imageStream.connected)imageStream.readBytes(imageData , imageData.length);
// clean out all the crud in that loader
loader.unload();
//push the aggregate bytearray of loaded image data in there.
loader.loadBytes(imageData);
}publicfunctionloadImage(input:String):void{//if connected we need to stop that
if(imageStream.connected)imageStream.close();
//lets load a new image url
imageStream.load(newURLRequest(input+'?'+getTimer()));
// clean out all the crud in that loader
loader.unload();
//create a fresh bytearray to store aggregate image data
imageData=newByteArray();
}]]></mx:Script><mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/png.png')" y="10" label="Load PNG1" x="10"/><mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/tpng.png')" y="10" label="Load PNG2" x="105"/><mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/gif.gif')" y="10" label="Load GIF" x="200"/><mx:Button click="loadImage('http://onflex.org/flexapps/applications/ProgressiveImageLoading/jpg.jpg')" y="10" label="Load JPG" x="285"/><mx:Canvas id="imageCanvas" left="10" top="40" right="10" bottom="10"/></mx:Application>