teotigraphix.blog.show()

Flex2 :: DND Bitmap copy

Hello all,

I am making so much stuff, some of this stuff I thought I might share with the community. ;-) Well some of you may say Mike, this is elementary but, Mike says, I am elementary!

While creating some drag and drop things, I decided that I was sick and tired of creating drag proxies. What I found myself doing all the time was trying to recreate the actual drag source.

In my travels of the flexcoders search, I looked for Bitmaps!

Here we have a function that is quite simple yet SO powerful for lazy people like me.

/**
     * Returns a Bitmap wrapped in a UIComponet for use when dragging and other things.
     * Thanks to Manish Jethani for his nice little code snippet,
     * @param target a IUIComponent instance that implements width and height, scaleX, scaelY.
     * @return IUIComponent that holds the copied Bitmap.
     */

    teo_internal function getComponentBitmap(target:IUIComponent):IUIComponent
    {
        var bitmapData:BitmapData = new BitmapData(
                                        Math.round(target.width / target.scaleX),
                                        Math.round(target.height / target.scaleY),
                                        false);
        bitmapData.draw(target);
 
        var bitmap:Bitmap = new Bitmap(bitmapData);

        var bitmapHolder:UIComponent = new UIComponent();
        bitmapHolder.addChild(bitmap);
       
        return bitmapHolder;
    }
 

Ok, I am by no means a Bitmap expert, call me a newbie with this stuff but, I do love it. Can someone tell me why using this and giving the UIComponent return any sort of alpha the text(black) becomes transparent.Other than that little detail, it works good for what I needed it to do. So, for those who share my enthusiasm for things the DON'T know, have fun!

Basically the steps are;

  1. Create a new BitmapData object using the target's width and height, also factor in the scale of the instance.
  2. Use the draw() method of the BitmapData to 'copy pixels' into the BitmapData.
  3. Create a new Bitmap image using the now drawn BitmapData.
  4. Create the holder for the new BitmapImage using UIComponent as the wrapper.
  5. Add the image to the UIComponent using addChild().
  6. Finally, return the 'bundle' back to where you need it.

I thought this was so kewl when it worked! I can think of over 100 things to use this with in the future.

Until next time,

PS, sorry for no hilighting of code, I had the plugin on my other blog and have not had enough time to get it back on this.

Peace, Mike

2 Responses to “Flex2 :: DND Bitmap copy”

  1. Mark Says:

    this doesn’t copy transparency of the orignal, is there a work around?

  2. Tom Van den Eynde Says:

    Could it be that you were using a system font instead? These don’t support rotating and I guess don’t support alpha either. Success!

Leave a Reply