写点什么

flutter_custom_cursor

作者:flfljh
  • 2024-12-19
    湖南
  • 本文字数:984 字

    阅读完需:约 3 分钟

该插件允许直接从内存缓冲区创建/设置自定义鼠标光标。

使用

1.先注册自定义光标


// register this cursorcursorName = await CursorManager.instance.registerCursor(CursorData()  ..name = "test"  ..buffer =  Platform.isWindows ? memoryCursorDataRawBGRA : memoryCursorDataRawPNG  ..height = img.height  ..width = img.width  ..hotX = 0  ..hotY = 0);  
复制代码


cacheName 注意,该函数将返回一个字符串 registerCursor,可用于将此游标设置为系统或者删除此游标。


2.设置自定义光标


我们已经实现了 FlutterCustomMemoryImageCursor 类,它是 的子类 MouseCursor。该类将自动为您设置内存光标。保持简单。


   MouseRegion(  cursor: FlutterCustomMemoryImageCursor(key: cursorName),  child: Row(children: [  Text("Memory image here", style: style),],  ),),
复制代码


3.删除光标


await CursorManager.instance.deleteCursor("cursorName");

鸿蒙 OS 代码

创建光标

  createCustomCursor(name: string, buffer: ArrayBufferLike, hotX: number, hotY: number): string | null {try {  let imgSource = image.createImageSource(buffer)  let customCursor: CustomCursor = {pixelMap: imgSource.createPixelMapSync(), focusX: hotX, focusY: hotY  }  this.caches.set(name, customCursor)} catch (e) {  Log.e(TAG, "Catch: createCustomCursor Error : " + JSON.stringify(e));  return null}return name  }
复制代码

设置光标

  setCustomCursor(name: string): boolean {try {  if (!this.caches.has(name)) {return false  }  let cursor = this.caches.get(name)  if (cursor != undefined) {pointer.setCustomCursorSync(  this.mainWindow?.getWindowProperties().id,  cursor.pixelMap,  cursor.focusX,  cursor.focusY)  } else {return false  }} catch (e) {  Log.i(TAG, "Catch: setCustomCursor Error : " + JSON.stringify(e));  return false}return true  }
复制代码

删除

  deleteCustomCursor(name: string): boolean {if (!this.caches.has(name)) {  return false}try {  this.caches.get(name)?.pixelMap.release()  this.caches.delete(name)} catch (e) {  Log.i(TAG, "Catch: deleteCustomCursor Error : " + JSON.stringify(e));  return false}return true  }
复制代码


用户头像

flfljh

关注

还未添加个人签名 2024-10-29 加入

还未添加个人简介

评论

发布
暂无评论
flutter_custom_cursor_flfljh_InfoQ写作社区