Thx to both of you for providing insight and help to my problem. I tried
both options and got them to work. So now I have 2 versions of my
component with dif implementations! Thx a bunch!
Here's the version I ended up using as a Custom Item Renderer. I
modified the code Vadeem had submitted to my needs and to be used as an
Item Renderer. Not sure if this is the most efficient or optimized, but
for production purposes(right now anyways :) it does the trick
Thx again!
cheers
erick
/**
*Custom Image Component
*/
package com.customComp
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.net.URLRequest;
import mx.controls.Label;
import mx.controls.listClasses.IListItemRenderer;
import mx.core.IDataRenderer;
import mx.core.UIComponent;
import mx.events.FlexEvent;
public class CustomImageBmp extends UIComponent implements
IListItemRenderer,IDataRenderer
{
protected var _bitmap:Bitmap;
protected var _source:String;
protected var _originalWidth:Number;
protected var _originalHeight:Number;
protected var _width:Number=0;
protected var _height:Number=0;
public var textDisplay:Label;
private var presetSize:uint = 90;
private var _data:Object;
private var _bottomOffset:uint = 15;
public function CustomImageBmp()
{
super();
}
[Bindable("dataChange")]
public function get data():Object
{
return _data;
}
[Inspectable(type=String)]
public function set data(value:Object):void
{
_data = value;
if(_data.image != null || _data != Object(undefined))
{
_source = value.image;
updateSource();
dispatchEvent(new FlexEvent(FlexEvent.DATA_CHANGE));
}
super.invalidateProperties();
}
protected function updateSource():void
{
if(_bitmap!=null && _bitmap.bitmapData!=null)
_bitmap.bitmapData.dispose();
while(this.numChildren>0)
this.removeChildAt(0);
if(_source)
{
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT,onImageInit);
loader.load(new URLRequest(_source));
}
}
override protected function createChildren():void
{
super.createChildren();
if(!textDisplay)
textDisplay = new Label();
textDisplay.styleName = this;
}
protected function onImageInit(event:Event):void
{
if(LoaderInfo(event.currentTarget).content is Bitmap)
{
_bitmap = LoaderInfo(event.currentTarget).content as
Bitmap;
_originalWidth = _bitmap.bitmapData.width;
_originalHeight = _bitmap.bitmapData.height;
this.addChild(_bitmap);
this.addChild(textDisplay);
textDisplay.text = _data.name;
update();
}
}
override protected function
updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth,unscaledHeight);
this._width=unscaledWidth;
this._height=unscaledHeight;
update();
}
protected function update():void
{
if(_bitmap!=null)
{
if(_originalWidth>_width || _originalHeight>_height)
{
var k:Number = _width/_originalWidth;
if(k*_originalHeight>_height)
k = _height/_originalHeight;
// _bitmap.width = _originalWidth*k;
// _bitmap.height = _originalHeight*k;
_bitmap.width = Math.min(presetSize,_width);
_bitmap.height = Math.min(presetSize,_height);
}
_bitmap.x = (_width-_bitmap.width)/2;
_bitmap.y = (_height-_bitmap.height)/2;
textDisplay.y = _bitmap.height+_bottomOffset;
}
}
}
}