ADOBE FLEX 3
Developer Guide
380
Similar to the snap-to-pixel feature found in the Flash authoring tool, the pixelSnapping property of the Bitmap
class determines whether or not a Bitmap object snaps to its nearest pixel. This property accepts one of three
constants defined in the PixelSnapping class:
ALWAYS, AUTO, and NEVER.
The syntax for applying pixel snapping is as follows:
myBitmap.pixelSnapping = PixelSnapping.ALWAYS;
Often, when bitmap images are scaled, they become blurred and distorted. To help reduce this distortion, use the
smoothing property of the BitmapData class. This Boolean property, when set to true, smooths, or anti-aliases, the
pixels within the image when it is scaled. This gives the image a clearer and more natural appearance.
Understanding the BitmapData class
The BitmapData class, which is in the flash.display package, can be likened to a photographic snapshot of the pixels
contained within a loaded or dynamically created bitmap image. This snapshot is represented by an array of pixel
data within the object. The BitmapData class also contains a series of built-in methods that are useful for creation
and manipulation of pixel data.
To instantiate a BitmapData object, use the following code:
var myBitmap:BitmapData = new BitmapData(width:Number, height:Number, transparent:Boolean,
fillColor:uinit);
The width and height parameters specify the size of the bitmap; the maximum value of both is 2880 pixels. The
transparent parameter specifies whether the bitmap data includes an alpha channel (true) or not (false). The
fillColor parameter is a 32-bit color value that specifies the background color, as well as the transparency value
(if it has been set to
true). The following example creates a BitmapData object with an orange background that is 50
percent transparent:
var myBitmap:BitmapData = new BitmapData(150, 150, true, 0x80FF3300);
To render a newly created BitmapData object to the screen, assign it to or wrap it in a Bitmap instance. To do this,
you can either pass the BitmapData object as a parameter of the Bitmap objects constructor, or you can assign it to
the
bitmapData property of an existing Bitmap instance. You must also add the Bitmap instance to the display list
by calling the
addChild() or addChildAt() methods of the display object container that will contain the Bitmap
instance. For more information on working with the display list, see Adding display objects to the display list” on
page 255.
The following example creates a BitmapData object with a red fill, and displays it in a Bitmap instance:
var myBitmapDataObject:BitmapData = new BitmapData(150, 150, false, 0xFF0000);
var myImage:Bitmap = new Bitmap(myBitmapDataObject);
addChild(myImage);
Manipulating pixels
The BitmapData class contains a set of methods that allow you to manipulate pixel data values.
Manipulating individual pixels
When changing the appearance of a bitmap image at a pixel level, you first need to get the color values of the pixels
contained within the area you wish to manipulate. You use the
getPixel() method to read these pixel values.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.