views:

25

answers:

1

In Flex, how does one hook into a bitmap or bitmapdata variable so that a function is notified whenever the bitmap's data has changed (pixel has changed, or filter's been added)?

I tried setting up binding but it doesn't work.

There must be a way to do it, because I can bind an mx:Image to a bitmap via the 'source' attribute, and the displayed image updates all the time when a I modify the bitmap. How does flex do it? Does it blindly redraw the bitmap at every frame, or is it smart and only redraws when the bitmap changes? If so, how does it know when the bitmap changes?

A: 

This is just sort of a semi-educated guess, with no testing behind it, so take it with some salt.

When Flex binds the source attribute of an Image, the value of .source is of type BitmapAsset.

BitmapAsset has a .bitmapData property, which is a reference to the bitmap in question.

I expect that the binding done by Flex is against that .bitmapData property.

I don't see any reason why you shouldn't be able to do that, too. I think you'd have to do a little circular work, though, as you'd have to create a BitmapAsset instance and populate it with the BitmapData you want to keep tabs on, then bind to the .bitmapData property of the BitmapAsset object.

Assuming a var called 'bitmapData', which is an instance of BitmapData, I think the following should work.

var bitmapAsset:BitmapAsset = new BitmapAsset(bitmapData);

var bitmapDataChangeWatcher:ChangeWatcher = BindingUtils.bindSetter(handleChangeToBitmapData, bitmapAsset, "bitmapData");

private function handleChangeToBitmapData(data:BitmapData):void
{
    // Handle change to the bitmap data
}
Ross Henderson