如何使用BitmapData类
什么是Bitmap类呢?它究竟能为我们做什么呢?
BitmapData/flash.display.BitmapData正如其拉丁文字面意义一样,它为我们提供了一个使用FLASH处理像素级别位图的API,当然这个类能够完成的功能很多,但是在这篇教程里我们主要使用它来光滑在程序运行时动态导入的图片对象
导入图像
在我们使用这个类之前,我们首先要导入它,当然有很多种方法,但是我推荐的最佳方法是导入并使用MovieClipLoader ,他提供了更大的灵活性,如果我们需要在未来改进的时候扩充功能的话,那么无疑使用这个类更具灵活性。
首先创建一个MovieClipLoader类的实例,并为它添加一个监听器以便在图像装载的时候捕捉这个事件并进行相关的处理。
使用onLoadInit我们就可以在图像装载的第一时间对该位图数据进行处理
tempImageListener.onLoadInit = function(mc:MovieClip) {
下面是每个步骤的详细分解
第一步
我们先要创建一个BitmapData 对象并传递一些参数初始化该对象。
var linkImage:BitmapData = new flash.display.BitmapData(width:Number, height:Number, [transparent:Boolean], [fillColor:Number])
width:Number - 位图的宽度【以像素为单位】使用MC的宽度作为要装载的对象的宽度,例如tempMc._width.这就避免了我们为对象设置静态的数据的同时程序装载的图像是不同的尺寸
height:Number - 位图的高度【以像素为单位】同位图的高度
transparent:Boolean [可选] - 确定位图的透明度支持单位像素的透明度,默认值是TRUE(也即透明),如果要创建一个透明的图像,那么将此属性设置为TRUE并且将填充色设置为0x00000000/0
fillColor:Number [可选] - 用来填充位图区域的32位的ARGB色,缺省值是0xFFFFFFFF (白色)
第二步:
下面我们就可以将我们的图像从我们临时的MC中取出并绘制出来。
linkImage.draw(tempMc);
第三步:
将BitmapData 链接到我们的目标MC了,当然最终达成了我们光滑的边界的目标(如果你的目标MC会改变大小,那么该效果将更加明显)
imageContainer.attachBitmap(bmp:BitmapData, depth:Number, [pixelSnapping:String], [smoothing:Boolean]) : Void
bmp:flash.display.BitmapData -这就是你所创建的一个图像,也即透明的位图
depth:Number - 设置在MC的何处放置该位图,整数值
pixelSnapping:String [可选] - 像素拉折模式:auto, always, 和never.缺省值是auto
smoothing:Boolean [可选] -光滑模式当设置为TRUE的时候就是允许的,而FALSE的时候就是不允许的,默认是不允许的,当然要将此值设为TRUE,我们整篇文章为了什么?
对于多个图像,最好将其封装进函数并加载到不同的MC这样你就不用不停的装载图像了。
当然你也可以将这些图像装进一个数组来保持对各个对象的引用
下面是完整的代码
function _loadTempImage(imageURL:String, mc:Movieclip){
//create new MovieClipLoader
var tempImageMCL:MovieClipLoader = new MovieClipLoader();
//create a new listener object for the MovieClipLoader
var tempImageListener:Object = new Object();
//add water, i mean listener to the MovieClipLoader
tempImageMCL.addListener(tempImageListener);
//Now we use loadClip to import our image:
tempImageMCL.loadClip(imageURL, tempMc)
tempImageListener.onLoadInit = function(mc:MovieClip) {
// Step 1: Create a new BitmapData instance and make it the size of the movie clip
// BitmapData(width:Number, height:Number, [transparent:Boolean], [fillColor:Number])
var linkImage:BitmapData = new flash.display.BitmapData(tempMc._width, tempMc._height,true,0x00FFFFFF);
// Step 2: Draw the contents of the movie clip into the linkImage bitmap
linkImage.draw(tempMc);
// Step 3: Attach the bitmap into the target movie clip.
// attachBitmap(bmp:BitmapData, depth:Number, [pixelSnapping:String], [smoothing:Boolean]) : Void
_imageContainer.attachBitmap(linkImage, 2, "auto", true)
_imageContainer.cacheAsBitmap = true;
}
}
希望上面所说的能够对你有所帮助,如果你有什么问题的话,请到下面的连接去留下你的问题
源文档 <http://www.ideaography.net/bitmapdata-and-you/>
BitmapData Class and you
February 20th, 2008 · Comment
What is this BitmapData I hear you asking, and what can it do for me?
BitmapData or flash.display.BitmapData as its known in Latin, provides us with pixel-level control of bitmaps within flash. There are many things that bitmapdata can be used for, but for this tute we will be discussing how it can be used to smooth images that dynamically imported at run-time.
Importing the Image
Before we can actually use the BitmapData class on an image, we need to import it. There are many methods in flash to do this, but the best method to use i think is MovieClipLoader as it allows more flexibility in the future when adding in new functions and pre-loaders.
Firstly create an instance of the MovieClipLoader class and add a listener to launch into attack when a image is loaded, like so:
var tempImageMCL:MovieClipLoader = new MovieClipLoader();
var tempImageListener:Object = new Object();
tempImageMCL.addListener(tempImageListener);
Now we use loadClip to import our image:
tempImageMCL.loadClip(imageURL, tempMc)
Using the onLoadInit, we can now get to the BitmapData Loving once the image has loaded in.
tempImageListener.onLoadInit = function(mc:MovieClip) {
// Step 1: Create a new BitmapData instance and make it the size of the movie clip
// BitmapData(width:Number, height:Number, [transparent:Boolean], [fillColor:Number])
var linkImage:BitmapData = new flash.display.BitmapData(tempMc._width, tempMc._height,true,0x00FFFFFF);
// Step 2: Draw the contents of the movie clip into the linkImage bitmap
linkImage.draw(tempMc);
// Step 3: Attach the bitmap into the target movie clip.
// attachBitmap(bmp:BitmapData, depth:Number, [pixelSnapping:String], [smoothing:Boolean]) : Void
_imageContainer.attachBitmap(linkImage, 2, "auto", true)
_imageContainer.cacheAsBitmap = true;
}
Here's the breakdown of each step:
Step 1
We firstly have to create a BitmapData object, passing through some parameters in the process.
var linkImage:BitmapData = new flash.display.BitmapData(width:Number, height:Number, [transparent:Boolean], [fillColor:Number])
width:Number - The width of the bitmap image in pixels. Use the width of the movieClip that the tempimage loaded into. i.e. tempMc._width. This save's us from having to add static values in the code incase we are using different sized images.
height:Number - The height of the bitmap image in pixels. As with the width, use the height of the movieClip that the tempimage loaded into. i.e. tempMc._height.
transparent:Boolean [optional] - Specifies whether the bitmap image supports per-pixel transparency. The default value is true (transparent). To create a fully transparent bitmap set the value of the transparent parameter to true and the value of the fillColor parameter to 0x00000000 (or to 0).
fillColor:Number [optional] - A 32-bit ARGB colour value that you use to fill the bitmap image area. The default value is 0xFFFFFFFF (solid white).
Step 2
We now can draw the image from our temporary movieClip into our newly created BitmapData object
linkImage.draw(tempMc);
Step 3
Time to attach the BitmapData into our target movieClip (_imageContainer) and marvel in all its smoothed out glory. (You will probably notice the effect more if the target clip has been resized or altered in some way.)
imageContainer.attachBitmap(bmp:BitmapData, depth:Number, [pixelSnapping:String], [smoothing:Boolean]) : Void
bmp:flash.display.BitmapData - A transparent or opaque bitmap image. This is the BitmapData object you just created.
depth:Number - An integer that specifies the depth level within the movie clip where the bitmap image should be placed.
pixelSnapping:String [optional] - The pixel snapping modes are auto, always, and never. The default mode is auto.
smoothing:Boolean [optional] - The smoothing mode is either true for enabled or false for disabled. The default mode is disabled. Be sure to set this option to true, as all our efforts here will be for nothing and the BitmapData gods will be displeased.
That's basically it.
For multiple images, its best to separate out the movieclipLoader and BitmapData into functions and load each image into a unique movieclip so you don't have to keep reloading the images.
You can keep track of all the temp image movieClips by pushing them into an array as i have done in my full screen gallery flash class.
Here's the all the code from above added into a function so we can call it anytime we please:
function _loadTempImage(imageURL:String, mc:Movieclip){
//create new MovieClipLoader
var tempImageMCL:MovieClipLoader = new MovieClipLoader();
//create a new listener object for the MovieClipLoader
var tempImageListener:Object = new Object();
//add water, i mean listener to the MovieClipLoader
tempImageMCL.addListener(tempImageListener);
//Now we use loadClip to import our image:
tempImageMCL.loadClip(imageURL, tempMc)
tempImageListener.onLoadInit = function(mc:MovieClip) {
// Step 1: Create a new BitmapData instance and make it the size of the movie clip
// BitmapData(width:Number, height:Number, [transparent:Boolean], [fillColor:Number])
var linkImage:BitmapData = new flash.display.BitmapData(tempMc._width, tempMc._height,true,0x00FFFFFF);
// Step 2: Draw the contents of the movie clip into the linkImage bitmap
linkImage.draw(tempMc);
// Step 3: Attach the bitmap into the target movie clip.
// attachBitmap(bmp:BitmapData, depth:Number, [pixelSnapping:String], [smoothing:Boolean]) : Void
_imageContainer.attachBitmap(linkImage, 2, "auto", true)
_imageContainer.cacheAsBitmap = true;
}
}
Hope it was as good for you as it was for me. If you have any question's or just want to say how great this tute was, please leave a comment below.
Happy Bitmapping...