AS3加载图片的两种方法

package net.burchin.loading {
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.text.TextField;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.geom.Matrix;
    public class LoadingDoc extends Sprite {
        private var _imageData:BitmapData;    //图片
        private var _loader:Loader;    //装载
        private var _rate:TextField;    //进度显示
        public function LoadingDoc() {
            init();
            _rate.text = '开始下载';
            _rate.autoSize = 'center';
            _rate.textColor = 0x000000;
            _rate.x = (stage.stageWidth - _rate.width)/2;
            _rate.y = (stage.stageHeight - _rate.height)/2;
            this.addChild(_rate);
            sendRequest('images/flower.jpg');
        }
        //初始化
        private function init() {
            _imageData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFFFFFFFF);
            _loader = new Loader();
            _rate = new TextField();
        }
        //发送请求
        private function sendRequest(p_url:String) {
            var m_request = new URLRequest(p_url);
            _loader.load(m_request);
            _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
            _loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
        }
        //事件,下载完毕
        private function onComplete(e:Event) {
            _imageData.draw(_loader, new Matrix(stage.stageWidth/_loader.width, 0, 0, stage.stageHeight/_loader.height, 0, 0));
            var m_image:Bitmap = new Bitmap(_imageData);
            this.removeChild(_rate);
            this.addChild(m_image);
        }
        //事件,下载中
        private function onProgress(e:Event) {
            var m_info:LoaderInfo = e.target as LoaderInfo;
            var m_percent:uint = (m_info.bytesLoaded/m_info.bytesTotal)*100;
            _rate.text = '已经下载'+m_percent.toString()+'%';
        }
    }
}

另一种方法:

package net.burchin.loading {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.utils.ByteArray;
import flash.events.Event;
import flash.geom.Matrix;
public class LoadingByByteArrayDoc extends Sprite {
private var _imageData:BitmapData; //图片数据
private var _loader:Loader; //装载
public function LoadingByByteArrayDoc():void {
init();
sendRequest('images/flower.jpg');
}
//初始化
private function init():void {
_imageData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xFFFFFFFF);
_loader = new Loader();
}
//发送请求
private function sendRequest(p_url:String):void {
var m_request:URLRequest = new URLRequest(p_url);
var m_loader:URLLoader = new URLLoader();
m_loader.dataFormat = URLLoaderDataFormat.BINARY;
m_loader.addEventListener(Event.COMPLETE, onSendComplete);
m_loader.load(m_request);
}
//事件,请求发送完毕
private function onSendComplete(e:Event):void {
var m_content:ByteArray = e.target.data as ByteArray;
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
_loader.loadBytes(m_content);
}
//事件,装载完毕
private function onLoadComplete(e:Event):void {
_imageData.draw(_loader, new Matrix(stage.stageWidth/_loader.width, 0, 0, stage.stageHeight/_loader.height, 0, 0));
var m_image:Bitmap = new Bitmap(_imageData);
this.addChild(m_image);
}
}
}



转载于:https://my.oschina.net/u/263424/blog/633437

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值