[as3]as3类文件用于加载图片使用的一个类文件代码供学习
[as3]as3类文件用于加载图片使用的一个类文件代码供学习
https://github.com/chndyyang/AS3SVGRenderer
- package
- {
- import com.lorentz.SVG.display.SVGDocument;
- import com.lorentz.SVG.events.SVGEvent;
- import com.lorentz.processing.ProcessExecutor;
- import flash.display.DisplayObject;
- import flash.display.Loader;
- import flash.display.LoaderInfo;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.IOErrorEvent;
- import flash.net.URLLoader;
- import flash.net.URLLoaderDataFormat;
- import flash.net.URLRequest;
- import flash.utils.ByteArray;
- public class LoadImage extends Sprite
- {
- private var _url:String;
- private var _loader:URLLoader;
- private var _byLoader:Loader;
- private var _isNeedAll:Boolean;
- private var _needW:uint;
- private var _needH:uint;
- public var data:Object;
- public function LoadImage(needWidth:uint = 0, needHeight:uint = 0, url:String = '', isNeedAllResize:Boolean = false)
- {
- super();
- if(url != '') _url = url;
- if(needWidth != 0) _needW = needWidth;
- if(needHeight != 0) _needH = needHeight;
- _isNeedAll = isNeedAllResize;
- _loader = new URLLoader();
- _loader.dataFormat = URLLoaderDataFormat.BINARY;
- }
- public function load(url:String=''):void
- {
- if(url != '') _url = url;
- if(_url == '')
- {
- trace("load image error url is null");
- return;
- }
- _loader.addEventListener(Event.COMPLETE, loadCompleteHandler);
- _loader.addEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler);
- try
- {
- _loader.load(new URLRequest(_url));
- }
- catch(error:Error)
- {
- trace("load url error");
- }
- }
- private function loadCompleteHandler(event:Event):void
- {
- if(_loader == null) return;
- var ba:ByteArray = _loader.data as ByteArray;
- if(ba == null) return;
- var type:String = getFileType(ba);
- _byLoader = new Loader();
- switch(type)
- {
- case ImageType.BMP:
- case ImageType.GIF:
- case ImageType.PNG:
- case ImageType.JPG:
- {
- _byLoader.loadBytes(ba);
- _byLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, byLoadComHandler);
- break;
- }
- case ImageType.SVG:
- {
- ProcessExecutor.instance.initialize(stage);
- var svg:SVGDocument = new SVGDocument();
- svg.parse(ba.readUTFBytes(ba.length));
- svg.addEventListener(SVGEvent.RENDERED,
- function onRendered(event:SVGEvent):void{
- resizeDS(event.target as DisplayObject);
- });
- addChild(svg);
- break;
- }
- default:
- {
- break;
- }
- }
- }
- private function byLoadComHandler(event:Event):void
- {
- var info:LoaderInfo = event.target as LoaderInfo;
- resizeDS(info.content);
- addChild(info.content);
- _loader.removeEventListener(Event.COMPLETE, loadCompleteHandler);
- _loader.removeEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler);
- _byLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, byLoadComHandler);
- }
- private function getFileType(fileData : ByteArray) : String {
- var b0 : int = fileData.readUnsignedByte();
- var b1 : int = fileData.readUnsignedByte();
- fileData.position = 0;
- if(b0 == 66 && b1 == 77) {
- return ImageType.BMP;
- }else if(b0 == 255 && b1 == 216) {
- return ImageType.JPG;
- }else if(b0 == 137 && b1 == 80) {
- return ImageType.PNG;
- }else if(b0 == 71 && b1 == 73) {
- return ImageType.GIF;
- }else if(b0 == 60 && b1 == 63){
- return ImageType.SVG;
- }
- return ImageType.UNKNOW;
- }
- private function loadErrorHandler(event:IOErrorEvent):void
- {
- trace("load image error");
- }
- private function resizeDS(ds:DisplayObject):void
- {
- var temp:uint;
- if(_needH != 0){
- temp = ds.height;
- ds.height = _needH;
- if(!_isNeedAll){
- ds.width = Math.round(ds.height / temp * ds.width);
- }else{
- ds.width = _needW;
- }
- }else if(_needW != 0){
- temp = ds.width;
- ds.width = _needW;
- if(!_isNeedAll){
- ds.height = Math.round(ds.width / temp * ds.height);
- }else{
- ds.height = _needH;
- }
- }
- }
- }
- }
[as3]as3类文件用于加载图片使用的一个类文件代码供学习
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...