将鼠标光标更改为位图(Flex 4)?

时间:2010-06-24 15:42:11

标签: flex flex4

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

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

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

我想将UIComponent绘制为游标。

我使用Maxims答案和Flex Cookbox article的组合来管理它。我必须对Maxim回答的唯一改变是:

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

否则staticBitmapData在构造函数中为null。

答案 2 :(得分:0)

以下是使用位图图像更改默认光标的几个简单步骤:

  1. 使用您选择的图像创建Bitmap类型的光标。您还可以在运行时动态设置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;

  5. 要恢复默认光标,请从舞台中删除光标位图,然后调用Mouse.show()。