8.4.载入外部图片到位图上

问题
我要载入外部图片作为BitmapData处理
解决办法
使用flash.display.Loader 类载入图片,当图片载入完成时,通过loader的content 属性property,它就是个Bitmap。访问Bitmap的bitmapData 属性就在访问载入的图片
讨论
通过Loader类载入外部位图。通过URLRequest 对象和位图的URL,监听loader的complete事件确定是否载入完成:
+展开
-ActionScript
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLRequest;
public class BitmapLoader extends Sprite {
private var _loader:Loader = new Loader( );
public function BitmapLoader( ) {
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_loader.load(new URLRequest("image.jpg"));
}
}
}
public function onComplete(event:Event):void {
var image:Bitmap = Bitmap(_loader.content);
var bitmap:BitmapData = image.bitmapData;
addChild(image);
}

首先得到loader的content 属性,它是个可视化对象表示内容被载入。如果你载入的是外部的.swf,它就是MovieClip类型。在这里载入的是位图,所以content 是一个Bitmap,类型转化为Bitmap 编译器不会抱错。

接着就可以访问包含位图内容的BitmapData了,你可以修改或载入新的图片。下面的例子在图片上画了个白色的矩形:
+展开
-ActionScript
public function onComplete(event:Event):void {
var loadedImage:Bitmap = Bitmap(_loader.content);
// Create a new Bitmap data and draw the
// loaded image to it.
var bitmap:BitmapData = new BitmapData(loadedImage.width,
loadedImage.height,
false, 0xffffffff);
bitmap.draw(loadedImage, new Matrix( ))
// Create a new Bitmap using the BitmapData
// and display it.
var image:Bitmap = new Bitmap(bitmap);
addChild(image);
// Manipulate the pixels as you wish
bitmap.fillRect(new Rectangle(0, 0, 50, 50), 0xffffffff);
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(115)喜欢(0)flash/flex/fcs/AIR