现在的位置: 首页 > 综合 > 正文

Flash 平台技术的优化(十) 处理像素

2013年05月27日 ⁄ 综合 ⁄ 共 1649字 ⁄ 字号 评论关闭

使用 setVector() 方法绘制像素。
当绘制像素时,使用 BitmapData 类的相应方法即可进行一些简单优化。快速绘制像素的一种方式是使用 Flash Player 10 中引入的 setVector() 方法:

// Image dimensions
var wdth:int = 200;
var hght:int = 200;
var total:int = wdth*hght;
// Pixel colors Vector
var pixels:Vector.<uint> = new Vector.<uint>(total, true);
for ( var i:int = 0; i< total; i++ )
{
// Store the color of each pixel
pixels[i] = Math.random()*0xFFFFFF;
}
// Create a non-transparent BitmapData object
var myImage:BitmapData = new BitmapData ( wdth, hght, false );
var imageContainer:Bitmap = new Bitmap ( myImage );
// Paint the pixels
myImage.setVector ( myImage.rect, pixels );
addChild ( imageContainer );
如果使用的是较慢的方法,如 setPixel() 或 setPixel32(),请使用 lock() 和 unlock() 方法加快运行速度。在以下代码中,使用了lock() 和 unlock() 方法来改进性能:
var buffer:BitmapData = new BitmapData(200,200,true,0xFFFFFFFF);
var bitmapContainer:Bitmap = new Bitmap(buffer);
var positionX:int;
var positionY:int;
// Lock update
buffer.lock();
var starting:Number=getTimer();
for (var i:int = 0; i<2000000; i++)
{
// Random positions
positionX = Math.random()*200;
positionY = Math.random()*200;
// 40% transparent pixels
buffer.setPixel32( positionX, positionY, 0x66990000 );
}
// Unlock update
buffer.unlock();
addChild( bitmapContainer );
trace( getTimer () - starting );
// output : 670
BitmapData 类的 lock() 方法可以锁定图像,并防止引用该图像的对象在 BitmapData 对象更改时进行更新。例如,如果Bitmap 对象引用 BitmapData 对象,则可以锁定 BitmapData 对象,对其更改后再解锁。在 BitmapData 对象解锁之前,
Bitmap 对象不会更改。要提高性能,请在对 setPixel() 或 setPixel32() 方法进行多次调用之前和之后使用此方法及 unlock()方法。调用 lock() 和 unlock() 可防止屏幕进行不必要的更新。
注: 如果处理的是位图(而不是显示列表)中的像素(双缓冲),有时该技术不会提高性能。如果位图对象没有引用位图缓冲区,则使用 lock() 和 unlock() 不会提高性能。Flash Player 检测到未引用缓冲区,并且位图不会呈现在屏幕上。遍历像素的方法(例如 getPixel()、getPixel32()、setPixel() 和 setPixel32())可能速度很慢,特别是在移动设备上。如果可能,请使用在一次调用中检索所有像素的方法。要读取像素,请使用 getVector() 方法,它比 getPixels() 方法速度快。此外,请记住,尽可能使用依赖于 Vector 对象的 API,因为它们的运行速度可能更快。

【上篇】
【下篇】

抱歉!评论已关闭.