Flex: Modify an embedded icon and use it in a button

apache-flexflashiconsimage

Just that, if you embed an icon:

[Embed(source='icons/checkmark.png')]
private static var CheckMark:Class;

You end up with a dynamic class. You can pretty easily assign the icon to a button at runtime by calling the setStyle method:

var btn:Button = new Button();
btn.setStyle("icon", CheckMark);

But what if you wanted to alter the icon at runtime, like changing it's alpha value or even redrawing pixels, before assigning it to the button?

So far I can't find a satisfactory answer…

Best Answer

This is the only answer I could find that seemed close: Dynamic Icons (example with View Source)

His solution involves a custom "DynamicIcon" class which is used in the button's icon setting, and a custom Button class which adds one method to the Button class to draw dynamic icons.

The end result is that you are able to send BitmapData to the DynamicIcon class, which will show up in the button. So, embed your image, instantiate your asset class, get the bitmapasset and modify it however you need to and send the bitmapData to the icon.

It's an interesting problem and it seems like there should be an easier solution, but this works without a lot of hassle.