flutter_custom_cursor
該插件允許直接從內(nèi)存緩沖區(qū)創(chuàng)建/設置自定義鼠標光標。
使用
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注意,該函數(shù)將返回一個字符串registerCursor,可用于將此游標設置為系統(tǒng)或者刪除此游標。
2.設置自定義光標
我們已經(jīng)實現(xiàn)了FlutterCustomMemoryImageCursor類,它是 的子類MouseCursor。該類將自動為您設置內(nèi)存光標。保持簡單。
MouseRegion(
cursor: FlutterCustomMemoryImageCursor(key: cursorName),
child: Row(
children: [
Text("Memory image here", style: style),
],
),
),
3.刪除光標
await CursorManager.instance.deleteCursor("cursorName");
鴻蒙OS代碼
創(chuàng)建光標
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
}