FlexBitmap?

Whats the correct way to display a BitmapData object in Flex 2.0?

I would have thought from the naming that FlexBitmap would hold your BitmapData, but FlexBitmap isn’t a UIComponent so it can only be displayed as in rawChildren.

1. What is the best way to display a BitmapData in Flex (i.e. the Flex 2 version of Bitmap)?

2. What is a FlexBitmap for if it aint an abstract class, it can’t be used in Flex, and it can’t be used in Flash?

Any help appreciated.

UPDATE
Todd posted this pretty simple solution
[as]var myBitmap:Bitmap = new Bitmap(yourBitmapData)
var myImage:Image = new Image()
myImage.addChild(myBitmap)
someContainer.addChild(myImage)[/as]

or if you looking for an object you can use in Flex with the same API as Bitmap, u can use this RealFlexBitmap class (u might want to fill in some of the gaps!)

[as]package tink.display
{
import flash.geom.Rectangle;
import mx.core.FlexBitmap;
import flash.display.DisplayObjectContainer;
import mx.core.IUIComponent;
import flash.events.Event;
import mx.managers.ISystemManager;
import flash.display.Sprite;
import flash.display.DisplayObject;

public class RealFlexBitmap extends FlexBitmap implements IUIComponent
{
public function get owner():DisplayObjectContainer
{
return null;
}

public function set owner(value:DisplayObjectContainer):void
{
}

public function initialize():void
{
}

public function get measuredMinHeight():Number
{
return 0;
}

public function set measuredMinHeight(value:Number):void
{
}

public function get percentHeight():Number
{
return 0;
}

public function set percentHeight(value:Number):void
{
}

public function get measuredMinWidth():Number
{
return 0;
}

public function set measuredMinWidth(value:Number):void
{
}

public function get focusPane():Sprite
{
return null;
}

public function set focusPane(value:Sprite):void
{
}

public function get minHeight():Number
{
return 0;
}

public function owns(displayObject:DisplayObject):Boolean
{
return false;
}

public function get enabled():Boolean
{
return false;
}

public function set enabled(value:Boolean):void
{
}

public function get percentWidth():Number
{
return 0;
}

public function set percentWidth(value:Number):void
{
}

public function parentChanged(p:DisplayObjectContainer):void
{
}

public function get explicitWidth():Number
{
return 0;
}

public function set explicitWidth(value:Number):void
{
}

override public function get opaqueBackground():Object
{
return null;
}

override public function set opaqueBackground(value:Object):void
{
}

public function get explicitHeight():Number
{
return 0;
}

public function set explicitHeight(value:Number):void
{
}

public function get includeInLayout():Boolean
{
return false;
}

public function set includeInLayout(value:Boolean):void
{
}

public function get minWidth():Number
{
return 0;
}

public function getExplicitOrMeasuredHeight():Number
{
return 0;
}

public function get baselinePosition():Number
{
return 0;
}

override public function get scrollRect():Rectangle
{
return null;
}

override public function set scrollRect(value:Rectangle):void
{
}

public function get document():Object
{
return null;
}

public function set document(value:Object):void
{
}

public function get maxHeight():Number
{
return 0;
}

public function get systemManager():ISystemManager
{
return null;
}

public function set systemManager(value:ISystemManager):void
{
}

public function get explicitMinWidth():Number
{
return 0;
}

public function setVisible(value:Boolean, noEvent:Boolean=false):void
{
}

public function get maxWidth():Number
{
return 0;
}

public function get explicitMaxHeight():Number
{
return 0;
}

public function get tweeningProperties():Array
{
return null;
}

public function set tweeningProperties(value:Array):void
{
}

override public function get cacheAsBitmap():Boolean
{
return false;
}

override public function set cacheAsBitmap(value:Boolean):void
{
}

public function getExplicitOrMeasuredWidth():Number
{
return 0;
}

public function get explicitMinHeight():Number
{
return 0;
}

public function get explicitMaxWidth():Number
{
return 0;
}

override public function get name():String
{
return null;
}

override public function set name(value:String):void
{
}

override public function get width():Number
{
return 0;
}

override public function set width(value:Number):void
{
}

override public function get visible():Boolean
{
return false;
}

override public function set visible(value:Boolean):void
{
}

public function move(x:Number, y:Number):void
{
}

override public function get alpha():Number
{
return 0;
}

override public function set alpha(value:Number):void
{
}

public function get measuredHeight():Number
{
return 0;
}

override public function get y():Number
{
return 0;
}

override public function set y(value:Number):void
{
}

public function setActualSize(newWidth:Number, newHeight:Number):void
{
}

override public function get x():Number
{
return 0;
}

override public function set x(value:Number):void
{
}

override public function get parent():DisplayObjectContainer
{
return null;
}

override public function get scaleY():Number
{
return 0;
}

override public function set scaleY(value:Number):void
{
}

override public function get scaleX():Number
{
return 0;
}

override public function set scaleX(value:Number):void
{
}

override public function get height():Number
{
return 0;
}

override public function set height(value:Number):void
{
}

override public function get mask():DisplayObject
{
return null;
}

override public function set mask(value:DisplayObject):void
{
}

public function get measuredWidth():Number
{
return 0;
}

override public function hasEventListener(type:String):Boolean
{
return false;
}

override public function willTrigger(type:String):Boolean
{
return false;
}

override public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0.0, useWeakReference:Boolean=false):void
{
}

override public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
{
}

override public function dispatchEvent(event:Event):Boolean
{
return false;
}

}
}[/as]

9 Responses to “FlexBitmap?”

  1. Will it’s subclass BitmapAsset not do the job?

  2. Tink says:

    nah, it don’t want you to create a BitmapAsset in MXML. Its not there and if you try and use it you get an error.

    I presuming this is just used for embeding bitmaps using AS inside Flex.

  3. Todd says:

    I have done it like this:

    var myBitmap:Bitmap = new Bitmap(yourBitmapData)
    var myImage:Image = new Image()
    myImage.addChild(myBitmap)
    someContainer.addChild(myImage)

    seems to work well, although I agree the names of the classes are _totally_ misleading as to what you can do with them. They really need more coherent examples of using bitmaps in the docs. It took me 3 days to figure out those 4 lines of code…

  4. Tink says:

    Cheers Todd, thats seems fine.

    I also came up with the RealFlexBitmap solution (with some prompting from Mr Leggett) for poeple who want an object that can be used in the same way as a Bitmap within Flex.

  5. Also the other one we thought of was just getting them to have FlexBitmap implement IUIComponent and that would be it, or just have an empty class that extends FlexBitmap and implements IUIComponent ;)

  6. Tink says:

    Also the other one we thought of was just getting them to have FlexBitmap implement IUIComponent…

    yeah thats the one i posted in the post UPDATE. unfortunately it can’t be empty as it has to have the methods defined in IUIComponent.

  7. Josh Tynjala says:

    I ran into this problem the other day too. It seems strange that the framework doesn’t have a Bitmap subclass that can be put into a Container. Luckily, I thought of a solution a lot like Todd’s. I had intented to make a “UIBitmap” class that implemented IUIComponent, but my Flex Builder trial had run out, and I didn’t want to write out all the interface functions by hand.

  8. Tink says:

    Yeah Flex Builder creating all the methods in any interface implemented is a nice touch.

Leave a Reply