Flutter OHOS flutter_custom_cursor(自定義光標)

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
  }
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容