harmony-utils之SnapshotUtil,截圖相關工具類
harmony-utils 簡介與說明
[harmony-utils] 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發(fā)者迅速構建鴻蒙應用。其封裝的工具涵蓋了APP、設備、屏幕、授權、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志,異常捕獲、字符、字符串、數字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和操作,能夠滿足各種不同的開發(fā)需求。
[picker_utils] 是harmony-utils拆分出來的一個子庫,包含PickerUtil、PhotoHelper、ScanUtil。
下載安裝ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils
//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
AppUtil.init(this.context);
}
API方法與使用
get 獲取已加載的組件的截圖,傳入組件的組件id,找到對應組件進行截圖
let pixelMap1 = await SnapshotUtil.get('snapshot_id1');
let pixelMap2 = SnapshotUtil.getSync('snapshot_id2');
createFromBuilder 在應用后臺渲染CustomBuilder自定義組件,并輸出其截圖
SnapshotUtil.createFromBuilder(() = > {
this.RandomBuilder()
}).then((pixelMap) = > {
Utils.showSheetImg(pixelMap);
}).catch((err: BusinessError) = > {
ToastUtil.showToast("組件生成圖片異常!");
});
@Builder
RandomBuilder() {
Column() {
Text("Builder截圖")
.fontSize(20)
.fontWeight(FontWeight.Bold)
.fontStyle(FontStyle.Italic)
.margin({ top: 20 })
Image($r("app.media.test_as4"))
.margin({ top: 30, bottom: 10 })
.padding(10)
.width('95%')
.backgroundColor(Color.Pink)
}
.backgroundColor(Color.Blue)
.padding(10)
.width('100%')
.aspectRatio(1)
.margin({ top: 50 })
.id("rBuilder")
}
snapshot 獲取窗口截圖,使用Promise異步回調
let pixelMap3 = await SnapshotUtil.snapshot();
onSnapshotListener 開啟系統(tǒng)截屏事件的監(jiān)聽
SnapshotUtil.onSnapshotListener(() = > {
ToastUtil.showToast("系統(tǒng)截圖了!");
LogUtil.error("系統(tǒng)截圖了!");
});
removeSnapshotListener 關閉系統(tǒng)截屏事件的監(jiān)聽
SnapshotUtil.removeSnapshotListener();
SnapshotUtil.removeSnapshotListener(snapshotCallBack);
創(chuàng)作不易,請給童長老點贊
審核編輯 黃宇
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯系本站處理。
舉報投訴
-
Harmony
+關注
關注
0文章
99瀏覽量
2980
發(fā)布評論請先 登錄
相關推薦
熱點推薦
harmony-utils之EmitterUtil,Emitter工具類
harmony-utils之EmitterUtil,Emitter工具類
harmony-utils之LocationUtil,定位相關工具類
harmony-utils之LocationUtil,定位相關工具類 harmony-utils
harmony-utils之NumberUtil,Number工具類
harmony-utils之NumberUtil,Number工具類 harmony-utils 簡介與說明
harmony-utils之PreviewUtil,文件預覽工具類
harmony-utils之PreviewUtil,文件預覽工具類 harmony-utils 簡介與說明 [
harmony-utils之StrUtil,字符串工具類
harmony-utils之StrUtil,字符串工具類 harmony-utils 簡介與說明 [ha
harmony-utils之TypeUtil,類型檢查工具類
harmony-utils之TypeUtil,類型檢查工具類 harmony-utils 簡介與說明 [
harmony-utils之WindowUtil,窗口相關工具類
harmony-utils之WindowUtil,窗口相關工具類 harmony-utils 簡
harmony-utils之AuthUtil,生物認證相關工具類
# harmony-utils之AuthUtil,生物認證相關工具類 ## harmony-utils
harmony-utils之ArrayUtil,集合工具類
# harmony-utils之ArrayUtil,集合工具類 ## harmony-utils 簡介與說明 ------[
harmony-utils之NetworkUtil,網絡相關工具類
harmony-utils之NetworkUtil,網絡相關工具類 harmony-utils
評論