2010年12月10日 星期五

[AS3] 將Loader類別所載入外部點陣圖BitmapData資料轉植至新建的Bitmap類別實體

Bitmap()建構函式
public function Bitmap(bitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false)
new Bitmap((Loader.content as Bitmap).bitmapData);

991210_BitmapData_1.swf



import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Bitmap;

//
const bmpURL:String = "991210_1.jpg";
//
var ldr:Loader = new Loader();
ldr.load(new URLRequest(bmpURL));
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, bmpWork);
//
var showBmp1:Bitmap;
var showBmp2:Bitmap;
//
function bmpWork(e:Event):void{
 //
 var ldrBmp:Bitmap = ldr.content as Bitmap;
 //
 showBmp1 = new Bitmap(ldrBmp.bitmapData);
 showBmp1.x = 0;
 showBmp1.y = 0;
 addChild(showBmp1);
 //
 showBmp2 = new Bitmap(ldrBmp.bitmapData);
 showBmp2.scaleX = 0.3;
 showBmp2.scaleY = 0.3;
 showBmp2.x = 370;
 showBmp2.y = 255;
 addChild(showBmp2);
}


加上預載進度百分比



import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.Bitmap;
import flash.events.ProgressEvent;
import flash.text.TextField;
import flash.text.TextFormat;

//
var format:TextFormat = new TextFormat();
format.size = 88;
format.color = 0xffff00;
//
var tF:TextField = new TextField();
tF.x = 220;
tF.y = 150;
tF.autoSize = TextFieldAutoSize.LEFT;
tF.defaultTextFormat = format;
addChild(tF);

//
const bmpURL:String = "991210_1.jpg";
//
var ldr:Loader = new Loader();
ldr.load(new URLRequest(bmpURL));
//
ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoaderProgress);
//
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, bmpWork);

//
function onLoaderProgress(e:ProgressEvent):void{
 tF.text = String(Math.floor(e.bytesLoaded/e.bytesTotal*100));
}

//
var showBmp1:Bitmap;
var showBmp2:Bitmap;
//
function bmpWork(e:Event):void{
 //
 var ldrBmp:Bitmap = ldr.content as Bitmap;
 //
 showBmp1 = new Bitmap(ldrBmp.bitmapData);
 showBmp1.x = 0;
 showBmp1.y = 0;
 addChild(showBmp1);
 //
 showBmp2 = new Bitmap(ldrBmp.bitmapData);
 showBmp2.scaleX = 0.3;
 showBmp2.scaleY = 0.3;
 showBmp2.x = 370;
 showBmp2.y = 255;
 addChild(showBmp2);
}


適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__●flash.display.Loader.content屬性
適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__●flash.display.Bitmap類別
適用於 Adobe Flash Professional CS5 的 ActionScript 3.0 參考__●flash.events.ProgressEvent.PROGRESS

沒有留言: