Flex API中的某些方法(如CursorManager.setCursor(cursorClass:Class, priority:int = 2, xOffset:Number = 0, yOffset:Number = 0))为图形采用Class类型的参数。此示例允许您将JPG / BMP / SWF指定为光标,但我知道从图像文件中获取类的唯一方法是在MXML中使用[Embed],这需要在我的情况下在AS3中动态完成。
必须有标准解决方案吗?这些Flex类是否有充分的理由首先使用Class?
编辑:我实际上并不关心模仿[Embed]标签的行为。我只是想能够使用CursorManager的图像文件路径。我确信我已经在其他情况下看到过这种情况,当然Flex人员认为这些东西并不总是硬编码的吗?
EDIT2:为了进一步简化问题,我正在寻找的是一种方法:CursorManager.setCursor(someHandyFunction("myCursor.png"))
如果这是可能的,问题是someHandyFunction
应该做什么?!
答案 0 :(得分:4)
这个看起来很有趣,所以我决定对它进行打击。这就是我能够做到的 - 这可能是一个更优雅的解决方案,但我已经对此进行了测试,它确实有效(here's a working example),所以我想我会把它踢回去给你修补一下如果你愿意,还可以。
首先,你需要一个扩展DisplayObject的自定义类 - 我只选择了Bitmap,因为我知道你正在尝试加载和使用JPG图像:
package
{
import flash.display.Bitmap;
import mx.core.Application;
public class MyLoadedImageClass extends Bitmap
{
public function MyLoadedImageClass()
{
// ClassRef is simply the name of my Flex app
super(ClassRef(Application.application).bitmapData);
}
}
}
...然后这是应用程序代码,它只是加载东西然后调用setCursor():
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
<mx:Script>
<![CDATA[
import mx.managers.CursorManager;
import mx.managers.CursorManagerPriority;
// This public member holds a reference to your loaded bitmapData,
// which MyLoadedImageClass's constructor will use when instantiated
// by the framework during CursorManager.setCursor()
public var bitmapData:BitmapData;
// Here we load the image
private function init():void
{
var urlLoader:URLLoader = new URLLoader();
var urlRequest:URLRequest = new URLRequest("http://roaming.turbonerd.com/m/20090104094515.jpg");
urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(urlRequest);
}
// Here we translate the bytes into a Bitmap
private function urlLoader_complete(event:Event):void
{
var bytes:ByteArray = URLLoader(event.target).data;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.loadBytes(bytes);
}
// And finally, we save off the bytes and set the cursor
private function loader_complete(event:Event):void
{
bitmapData = Bitmap(event.target.content).bitmapData;
CursorManager.setCursor(MyLoadedImageClass, CursorManagerPriority.HIGH);
}
]]>
</mx:Script>
</mx:Application>
希望评论能够详细描述事情。希望能帮助到你! (并感谢您的挑战!)
答案 1 :(得分:1)
查看Ben Stucki的这个示例课程。我已广泛使用它从远程JPG / GIF / PNG动态创建类实例。
答案 2 :(得分:1)
我最近与CursorManager
进行了斗争(试图让他在运行时加载png游标),结果,我在我的实验室blog发布了一个实用工具类。
答案 3 :(得分:0)
据我所知,目前无法使用as3在flash vm内的运行时创建类。 理论上你可以通过将swf-data写入bytearray并加载它来创建一个你需要在运行时所需的类的swf,我不建议你试试这个,除非你遇到很多令人费解,并不介意很多挫折; - )
如果您详细说明您的要求,可能会找到另一种解决方案。
BTW:如果你不介意包含服务器组件的解决方案:你可以让你的服务器编译包含正确类的新swf并加载它们。有多种方法可以让flex编译器根据请求编译。