下面介绍了两种图片加载处理的不同方式了,在工作中我们是经常遇到的,在不同的环境用途下使用的方式也是不同的,
1 package net.burchin.loading {
2 import flash.display.Sprite;
3 import flash.display.Bitmap;
4 import flash.display.BitmapData;
5 import flash.display.Loader;
6 import flash.display.LoaderInfo;
7 import flash.text.TextField;
8 import flash.net.URLRequest;
9 import flash.events.Event;
10 import flash.events.ProgressEvent;
11 import flash.geom.Matrix;
12 public class LoadingDoc extends Sprite {
13 private var _imageData:BitmapData; //图片
14 private var _loader:Loader; //装载
15 private var _rate:TextField; //进度显示
16 public function LoadingDoc() {
17 init();
18 _rate.text = '开始下载';
19 _rate.autoSize = 'center';
20 _rate.textColor = 0x000000;
21 _rate.x = (stage.stageWidth - _rate.width)/2;
22 _rate.y = (stage.stageHeight - _rate.height)/2;
23 this.addChild(_rate);
24 sendRequest('images/flower.jpg');
25 }
26 //初始化
27 private function init() {
28 _imageData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFFFFFFFF);
29 _loader = new Loader();
30 _rate = new TextField();
31 }
32 //发送请求
33 private function sendRequest(p_url:String) {
34 var m_request = new URLRequest(p_url);
35 _loader.load(m_request);
36 _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
37 _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
38 }
39 //事件,下载完毕
40 private function onComplete(e:Event) {
41 _imageData.draw(_loader, new Matrix(stage.stageWidth/_loader.width, 0, 0, stage.stageHeight/_loader.height, 0, 0));
42 var m_image:Bitmap = new Bitmap(_imageData);
43 this.removeChild(_rate);
44 this.addChild(m_image);
45 }
46 //事件,下载中
47 private function onProgress(e:Event) {
48 var m_info:LoaderInfo = e.target as LoaderInfo;
49 var m_percent:uint = (m_info.bytesLoaded/m_info.bytesTotal)*100;
50 _rate.text = '已经下载'+m_percent.toString()+'%';
51 }
52 }
53 }
另一种:
1 package net.burchin.loading {
2 import flash.display.Sprite;
3 import flash.display.BitmapData;
4 import flash.display.Bitmap;
5 import flash.display.Loader;
6 import flash.net.URLRequest;
7 import flash.net.URLLoader;
8 import flash.net.URLLoaderDataFormat;
9 import flash.utils.ByteArray;
10 import flash.events.Event;
11 import flash.geom.Matrix;
12 public class LoadingByByteArrayDoc extends Sprite {
13 private var _imageData:BitmapData; //图片数据
14 private var _loader:Loader; //装载
15 public function LoadingByByteArrayDoc():void {
16 init();
17 sendRequest('images/flower.jpg');
18 }
19 //初始化
20 private function init():void {
21 _imageData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFFFFFFFF);
22 _loader = new Loader();
23 }
24 //发送请求
25 private function sendRequest(p_url:String):void {
26 var m_request:URLRequest = new URLRequest(p_url);
27 var m_loader:URLLoader = new URLLoader();
28 m_loader.dataFormat = URLLoaderDataFormat.BINARY;
29 m_loader.addEventListener(Event.COMPLETE, onSendComplete);
30 m_loader.load(m_request);
31 }
32 //事件,请求发送完毕
33 private function onSendComplete(e:Event):void {
34 var m_content:ByteArray = e.target.data as ByteArray;
35 _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
36 _loader.loadBytes(m_content);
37 }
38 //事件,装载完毕
39 private function onLoadComplete(e:Event):void {
40 _imageData.draw(_loader, new Matrix(stage.stageWidth/_loader.width, 0, 0, stage.stageHeight/_loader.height, 0, 0));
41 var m_image:Bitmap = new Bitmap(_imageData);
42 this.addChild(m_image);
43 }
44 }
45 }