as3自定义加载图片类
ImageLoader.as类:
package
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
/**
* 图片加载类
* @author sange
*/
public class ImageLoader extends Sprite
{
private var loader:Loader;
private var sw:Number;
private var sh:Number;
/**
* 构造函数
* @param w 图片width
* @param h 图片height
* @param url 图片地址
*/
public function ImageLoader(w:Number,h:Number,url:String)
{
this.sw = w;
this.sh = h;
loader = new Loader();
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgressHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
}
/**
* 加载进度 监听器
* @param e
*/
private function loaderProgressHandler(e:ProgressEvent):void
{
var num:uint = (e.bytesLoaded / e.bytesTotal) * 100;
trace(num+"%");
}
/**
* 加载完成 监听器
* @param e
*/
private function loaderCompleteHandler(e:Event):void
{
var bitmap:Bitmap = e.currentTarget.content as Bitmap;
trace(bitmap);
bitmap.width = this.sw;
bitmap.height = this.sh;
bitmap.x = 0;
bitmap.y = 0;
addChild(bitmap);
}
}
}
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
/**
* 图片加载类
* @author sange
*/
public class ImageLoader extends Sprite
{
private var loader:Loader;
private var sw:Number;
private var sh:Number;
/**
* 构造函数
* @param w 图片width
* @param h 图片height
* @param url 图片地址
*/
public function ImageLoader(w:Number,h:Number,url:String)
{
this.sw = w;
this.sh = h;
loader = new Loader();
loader.load(new URLRequest(url));
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgressHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
}
/**
* 加载进度 监听器
* @param e
*/
private function loaderProgressHandler(e:ProgressEvent):void
{
var num:uint = (e.bytesLoaded / e.bytesTotal) * 100;
trace(num+"%");
}
/**
* 加载完成 监听器
* @param e
*/
private function loaderCompleteHandler(e:Event):void
{
var bitmap:Bitmap = e.currentTarget.content as Bitmap;
trace(bitmap);
bitmap.width = this.sw;
bitmap.height = this.sh;
bitmap.x = 0;
bitmap.y = 0;
addChild(bitmap);
}
}
}
以下二种调用
flex的as工程主类:SImageLoader.as
package {
import flash.display.Sprite;
public class SImageLoader extends Sprite
{
[SWF(width="800",height="600",frameRate="25")]
public function SImageLoader()
{
var img:ImageLoader = new ImageLoader(100,100,"q2.jpg");
addChild(img);
}
}
}
import flash.display.Sprite;
public class SImageLoader extends Sprite
{
[SWF(width="800",height="600",frameRate="25")]
public function SImageLoader()
{
var img:ImageLoader = new ImageLoader(100,100,"q2.jpg");
addChild(img);
}
}
}
还有flash帧上调用代码:
var image:ImageLoader = new ImageLoader(100,100,"q2.jpg");
this.addChild(image);
this.addChild(image);