要在ActionScript 3中动态加载渐进式JPEG/JPG并在完全加载之前知道其宽度和高度,您可以使用以下步骤:
- 导入必要的类:import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Bitmap;
- 创建一个Loader实例和一个URLRequest实例:var loader:Loader = new Loader();
var urlRequest:URLRequest = new URLRequest("path/to/your/image.jpg");
- 添加事件监听器以便在加载过程中获取宽度和高度:loader.contentLoaderInfo.addEventListener(Event.OPEN, onOpen);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
- 开始加载JPEG/JPG图像:loader.load(urlRequest);
- 定义事件处理函数:function onOpen(event:Event):void {
trace("开始加载...");
}
function onProgress(event:ProgressEvent):void {
var loaded:Number = event.bytesLoaded;
var total:Number = event.bytesTotal;
var percent:Number = (loaded / total) * 100;
trace("加载进度:" + percent.toFixed(2) + "%");
if (loader.content != null && loader.content is Bitmap) {
var bitmap:Bitmap = Bitmap(loader.content);
trace("宽度:" + bitmap.width + ",高度:" + bitmap.height);
}
}
function onComplete(event:Event):void {
trace("加载完成!");
if (loader.content != null && loader.content is Bitmap) {
var bitmap:Bitmap = Bitmap(loader.content);
trace("宽度:" + bitmap.width + ",高度:" + bitmap.height);
}
}
这样,您就可以在ActionScript 3中动态加载渐进式JPEG/JPG,并在完全加载之前知道其宽度和高度。