2010-06-24 82 views
0

在Flex 4中,如何将光标更改为在运行时处确定的位图图像?我见过的所有示例都使用CursorManager.setCursor将光标设置为编译时指定的类。将鼠标光标更改为位图(Flex 4)?

我想要做的是将光标更改为其位图数据由上下文确定的位图。

回答

2
package cursor 
{ 
import flash.display.BitmapData; 
import flash.display.PixelSnapping; 

import mx.core.BitmapAsset; 

public class RuntimeBitmap1 extends BitmapAsset 
{ 

    public static var staticBitmapData:BitmapData; 

    public function RuntimeBitmap1() 
    { 
     super(staticBitmapData); 
    } 
} 
} 

用法:

var bitmapData:BitmapData = new BitmapData(50, 50, false, 0x88888888); 
RuntimeBitmap1.staticBitmapData = bitmapData; 
cursorManager.setCursor(RuntimeBitmap1, 0); 
+0

这工作,谢谢! – justkevin 2010-06-25 14:41:12

0

这里有几个简单的步骤来更改默认光标用位图图像:

  1. 使用的图像创建类型位图的光标你选择。您也可以在运行时动态设置bitmapData。
    
    var DEFAULT_CURSOR_IMAGE : Class; 
    var myCursorBitmap : Bitmap; 
    ... 
    myCursorBitmap = new DEFAULT_CURSOR_IMAGE();
  2. 注册以接收鼠标移动事件并相应地更新光标位置。
    
    function onMouseMove(event : MouseEvent) : void 
    { 
        myCursorBitmap.x = event.localX; 
        myCursorBitmap.y = event.localY; 
    }
  3. 使用Mouse.hide()隐藏真实光标。

  4. 显示您的自定义光标。您可以稍后通过动态设置bitmapData来更新光标形状。

    
    addChild(myCursorBitmap); 
    ... 
    myCursorBitmap.bitmapData = myNewCursor;

要恢复默认光标,从舞台上删除您的光标位图,并呼吁Mouse.show()。

+0

这是一个Flex项目,所以我无法将addChild添加到主要的mxml类。 – justkevin 2010-06-25 14:41:57

+0

@justkevin:是的,你是对的 - 在这种情况下,你必须把它放在一个UIComponent中。 – 2010-06-28 09:43:48

1

我想画一个UIComponent作为游标。

我管理它使用Maxims的答案和这个的组合。我不得不做出马克西姆答案的唯一变化是A S如下:

public function RuntimeBitmap1() 
{ 
    super(RuntimeBitmap1.staticBitmapData); 
} 

否则staticBitmapData通过为空值排在构造函数中。