Change mouse cursor to a bitmap (Flex 4)?

巧了我就是萌 提交于 2019-12-02 17:28:06

问题


In Flex 4, how can I change the cursor to a Bitmap image determined at runtime? All the examples I've seen use CursorManager.setCursor to set the cursor to a class specified at compile time.

What I want to do is change the cursor to a bitmap whose bitmapData is determined by the context.


回答1:


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);
    }
}
}

Usage:

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



回答2:


I wanted to draw a UIComponent as a cursor.

I managed it using a combination of Maxims answer and this Flex Cookbox article. The only change I had to make to Maxim answer was a s follows:

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

Otherwise staticBitmapData came through as null in the constructor.




回答3:


Here are a few simple steps to change the default cursor with a bitmap image:

  1. Create your cursor of type Bitmap by using an image of your choice. You can also set the bitmapData dynamically during runtime.
    
    var DEFAULT_CURSOR_IMAGE : Class;
    var myCursorBitmap : Bitmap;
    ...
    myCursorBitmap = new DEFAULT_CURSOR_IMAGE();
  2. Register to receive mouse move events and update cursor position accordingly.
    
    function onMouseMove(event : MouseEvent) : void
    {
       myCursorBitmap.x = event.localX;
       myCursorBitmap.y = event.localY;
    }
  3. Hide the real cursor by using Mouse.hide().

  4. Show your custom cursor. You may update cursor shape later by setting bitmapData dynamically.

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

To restore the default cursor, remove your cursor bitmap from the stage and call Mouse.show().



来源:https://stackoverflow.com/questions/3111533/change-mouse-cursor-to-a-bitmap-flex-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!