Flutter OHOS flutter_custom_cursor(自定义光标)
flutter_custom_cursor
该插件允许直接从内存缓冲区创建/设置自定义鼠标光标。
使用
1.先注册自定义光标
// register this cursor
cursorName = 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
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!