recently i needed a way to generate small preview-thumbs and put them on flex' Buttons - as it turned out, it isn't that easy to use dynamic content as icons at all.
after some while i found a nice way to put dynamically loaded images in icons: ben stucki's IconUtility.
i took his class and kicked it up a notch. now it's able to take any DisplayObject as a source and lets you use it as an icon for flex components:
-
var sprite:Sprite = new Sprite();
-
sprite.graphics.beginFill(0xff0000, 0.5);
-
sprite.graphics.drawCircle(0, 0, 15);
-
-
var button:Button = new Button();
-
button.setStyle("icon", IconUtilityPlus.getClass(button, sprite));
of course it works in MXML, too:
-
<mx:Button id="button" icon="{IconUtilityPlus.getClass(button, sprite)}"/>
i've also added a third parameter for animated DisplayObjects like Video or MovieClip - added this just for fun, it will definitely cost some performance to display animated stuff and may be buggy.
thanks again to ben for the previous work, it really is some smart way to bypass adobe's way of only using classes
here it is: IconUtilityPlus.as
edit: fixed drawing method for DisplayObjects with offsets <0

4 Comments
nice work - thanks a lot
Wow!
Can this be used for creating animated icons aswell?
J
jonas, the implementation for animated stuff has been done in a few minutes, but it should work for videos without a problem.
if you want to use a flash-animated movieclip or something similar you have to put a transparent rectangle inside it. this rectangle has to cover the area of movement.
that’s because it sets the width and height only while initialization, so if these values changed during the animation you’d only see a portion of it.
hey should should add this line
bitmapData.fillRect(bitmapData.rect, 0×00FFFFFF);
in the displaySource method so the image is filled with transparent before drawing the new frame. otherwise you get some funny outcome when you have gradients with transparency.
I implemented it between
var bounds:Rectangle ….
and
bitmapData.draw(_displayObject….