问题描述
||
我有以下代码
///Get BitmapData from library in SWC
var ClassReference:Class = getDeFinitionByName(products[i].producticon+\"Data\") as Class;
// Create new BitmapData Instance From it
var bitMapS:BitmapData = new ClassReference();
// Create new Class that contains Bitmap Data
var IconBitmap:Class = new AndroidAsset(bitMapS) as Class;
// Get String name from Class
var s:String = getQualifiedClassName(IconBitmap);
trace(s);
我在跟踪中得到空值。如何制作可用于此位图的动态类,类似于您如何引用和来自嵌入式媒体的类?
解决方法
我不确定我是否完全理解您的问题,但这听起来像您想要一种以类似于创建类的新实例的方式获取此位图数据的副本的方法。如果是这样,您可以执行以下操作:
//grab the name of the asset as a string
var bitmapDataAssetName:String = products[i].producticon+\"Data\";
//get the asset definition from the SWC
var CustomBitmapDataClass:Class = getDefinitionByName(bitmapDataAssetName) as Class;
//store it in an associative array or dictionary for use later
var myBitmaps:Array = [];
myBitmaps[bitmapDataAssetName] = CustomBitmapDataClass;
//then instantiate new bitmaps from the class definition that\'s stored in your array
var testBitmapData:BitmapData = new (myBitmaps[bitmapDataAssetName] as Class)() as BitmapData;
因此,在实际使用场景中,符号定义数组将是某个BitmapManager类或某些类上的成员变量。然后,如果您想使其可以从应用程序中的任何位置访问,只需将其设置为单例即可,并且可以保存-您可以通过以下调用从SWC实例化新库资源:
var localBitmapData:BitmapData = new (BitmapManager.instance.definitions[theNameOfTheBitmapIWant] as Class)() as BitmapData;
或者,一种更好的方法是在BitmapManager中添加一个方法,例如
public function getBitmapAsset(assetName:String):BitmapData {
var symbolDefinition:Class = myBitmaps[bitmapDataAssetName] as Class;
return new symbolDefinition() as BitmapData;
}
所以你可以像这样调用它:
var localBitmapData:BitmapData = BitmapManager.instance.getBitmapAsset(\"theNameOfTheBitmapIWant\");
希望能有所帮助(如果那是您甚至开始质疑的话):P