package tc.hk.erikyang
{
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.events.IOErrorEvent;
import com.greensock.TweenNano;
import net.stevensacks.preloaders.CircleSlicePreloader;
public class ImageLoader extends Sprite
{
// ------- Properties -------
public var imageLoader:Loader = new Loader();
public var imageURLReq:URLRequest = new URLRequest();
private var preloader:CircleSlicePreloader = new CircleSlicePreloader();
public var maxWidth:Number = -1;
public var maxHeight:Number = -1;
// ------- Constructor -------
public function ImageLoader()
{
// set the name value, if specified
trace("ImageLoader()");
}
// ------- Methods -------
public function load(url:String, mwidth = 0, mheight = 0):void
{
imageURLReq.url=url;
if(mwidth)
this.maxWidth = mwidth;
if(mheight)
this.maxHeight = mheight;
//this.removeChild(imageLoader);
preloader.x = this.width/2;
preloader.y = this.height/2;
imageLoader.scaleX = imageLoader.scaleY = 1;
this.addChild(preloader);
try
{
imageLoader.load(imageURLReq);
}
catch (error:IOErrorEvent)
{
trace("Error: load()");
}
catch (error:Error)
{
trace("Error: load()");
}
imageLoader.contentLoaderInfo.addEventListener(Event.INIT, imageLoaded);
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProgress);
}
private function imageProgress(evt:ProgressEvent):void
{
trace(Math.round((evt.bytesLoaded / evt.bytesTotal)*100));
}
private function imageLoaded(evt:Event):void
{
//var targetLoader:Loader = Loader(evt.target.loader);
trace("complete");
if((imageLoader.width>this.maxWidth && maxWidth>-1) || (imageLoader.height>this.maxHeight && maxHeight>-1))
{
imageLoader.width = this.maxWidth;
imageLoader.scaleY = imageLoader.scaleX;
}
this.removeChild(preloader);
trace("imageLoader: ", imageLoader.width, imageLoader.height);
this.addChild(imageLoader);
imageLoader.alpha = 0.1;
TweenNano.to(imageLoader, 1.5, {alpha:1} );
imageLoader.contentLoaderInfo.removeEventListener(Event.INIT,myRemoveListener);
imageLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS,myRemoveListener);
}
private function myRemoveListener(evt:Event):void
{
trace("Event removed");
}
}
}
No comments:
Post a Comment