harmony-utils之WindowUtil,窗口相關工具類
harmony-utils 簡介與說明
[harmony-utils] 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發者迅速構建鴻蒙應用。其封裝的工具涵蓋了APP、設備、屏幕、授權、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志,異常捕獲、字符、字符串、數字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和操作,能夠滿足各種不同的開發需求。
[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方法與使用
setPreferredOrientation 設置窗口的顯示方向屬性
WindowUtil.setPreferredOrientation(window.Orientation.LANDSCAPE).then(() = > {
ToastUtil.showToast(`設置成功!`)
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
getPreferredOrientation 獲取窗口的顯示方向屬性,主窗口調用
let orientation = WindowUtil.getPreferredOrientation();
DialogHelper.showToast(`窗口屏幕方向:${orientation}`);
setWindowPrivacyMode 設置窗口是否為隱私模式。設置為隱私模式的窗口,窗口內容將無法被截屏或錄屏
WindowUtil.setWindowPrivacyMode(true).then(() = > {
ToastUtil.showToast("您已設置隱私模式,禁止截屏、錄像");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
isPrivacyMode 窗口是否隱私模式,默認主窗口
let isPrivacyMode = WindowUtil.isPrivacyMode();
ToastUtil.showToast(`窗口是否隱私模式:${isPrivacyMode}`);
setWindowLayoutFullScreen 設置窗口的布局是否為沉浸式布局(該沉浸式布局狀態欄、導航欄仍然顯示)
WindowUtil.setWindowLayoutFullScreen(true).then(() = > {
ToastUtil.showToast(`沉浸式布局已設置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
isLayoutFullScreen 判斷窗口是否為沉浸式,默認主窗口
let isLayoutFullScreen = WindowUtil.isLayoutFullScreen();
ToastUtil.showToast(`窗口是否為沉浸式:${isLayoutFullScreen}`);
setWindowSystemBarProperties 設置主窗口三鍵導航欄、狀態欄的屬性
WindowUtil.setWindowSystemBarProperties({
statusBarColor: '#F00FF0',
statusBarContentColor: '#0FF00F',
isStatusBarLightIcon: true,
navigationBarColor: '#F06060',
navigationBarContentColor: "#0606F0",
isNavigationBarLightIcon: true
}).then(() = > {
ToastUtil.showToast("設置成功!");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
getWindowSystemBarProperties 獲取主窗口三鍵導航欄、狀態欄的屬性
let properties = WindowUtil.getWindowSystemBarProperties();
let jsonStr = JSON.stringify(properties, null, 2);
setImmersiveModeEnabledState 設置當前窗口是否開啟沉浸式布局,該調用不會改變窗口模式和窗口大小
WindowUtil.setImmersiveModeEnabledState(true);
getImmersiveModeEnabledState 查詢當前窗口是否已經開啟沉浸式布局
let enabled = WindowUtil.getImmersiveModeEnabledState();
ToastUtil.showToast(`是否開啟沉浸式布局:${enabled}`);
setWindowGrayScale 設置窗口灰階。該接口需要在調用loadContent()或setUIContent()使窗口加載頁面內容后調用。
WindowUtil.setWindowGrayScale(1.0);
setWindowBackgroundColor 設置窗口的背景色。Stage模型下,該接口需要在loadContent()或setUIContent()調用生效后使用
WindowUtil.setWindowBackgroundColor('#9932CC');
ToastUtil.showToast("設置背景色成功!");
setWindowSystemBarEnable 設置主窗口三鍵導航欄、狀態欄、底部導航條的可見模式,狀態欄與底部導航條通過status控制、三鍵導航欄通過navigation控制
WindowUtil.setWindowSystemBarEnable(['status', 'navigation']).then(() = > {
ToastUtil.showToast(`設置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
setSpecificSystemBarEnabled 設置主窗口三鍵導航欄、狀態欄、底部導航條的顯示和隱藏
WindowUtil.setSpecificSystemBarEnabled('navigationIndicator', true).then(() = > {
ToastUtil.showToast(`設置成功!`);
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
setWindowKeepScreenOn 設置屏幕是否為常亮狀態
WindowUtil.setWindowKeepScreenOn(true).then(() = > {
ToastUtil.showToast("你已設置常亮");
}).catch((err: BusinessError) = > {
LogUtil.error(err);
});
isKeepScreenOn 屏幕是否常亮
let isKeepScreenOn = WindowUtil.isKeepScreenOn();
ToastUtil.showToast(`屏幕是否常亮:${isKeepScreenOn}`);
setWindowBrightness 設置屏幕亮度值
WindowUtil.setWindowBrightness(0.7).then(() = > {
ToastUtil.showToast(`您已設置亮度!`);
}).catch((err: BusinessError) = > {
LogUtil.error(`異常信息-code: ${err.code} - msg: ${err.message}`)
});
getBrightness 獲取屏幕亮度。該參數為浮點數,可設置的亮度范圍為[0.0, 1.0],其取1.0時表示最大亮度值。如果窗口沒有設置亮度值,表示亮度跟隨系統,此時獲取到的亮度值為-1
let brightness = WindowUtil.getBrightness();
ToastUtil.showToast(`屏幕亮度:${brightness}`);
setWindowFocusable 設置使用點擊或其他方式使該窗口獲焦的場景時,該窗口是否支持窗口焦點從點擊前的獲焦窗口切換到該窗口
WindowUtil.setWindowFocusable(true).then(() = > {
ToastUtil.showToast("設置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設置失敗!");
});
isFocusable 窗口是否可聚焦,默認主窗口
let isFocusable = WindowUtil.isFocusable();
ToastUtil.showToast(`窗口是否可聚焦:${isFocusable}`);
setWindowTouchable 設置窗口是否為可觸狀態
WindowUtil.setWindowTouchable(true).then(() = > {
ToastUtil.showToast("設置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設置失敗!");
});
isTouchable 窗口是否可觸摸,默認主窗口
let isTouchable = WindowUtil.isTouchable();
ToastUtil.showToast(`窗口是否可觸摸:${isTouchable}`);
getWindowProperties 獲取當前窗口的屬性,默認主窗口
let properties = WindowUtil.getWindowProperties();
let jsonStr = `${JSON.stringify(properties, null, 2)}`;
getWindowAvoidArea 獲取當前應用窗口內容規避的區域。如系統欄區域、劉海屏區域、手勢區域、軟鍵盤區域等與窗口內容重疊時,需要窗口內容避讓的區域
let area = WindowUtil.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM);
let jsonStr = `${JSON.stringify(area, null, 2)}`;
getWindowType 獲取窗口類型,默認主窗口
let windowType = WindowUtil.getWindowType();
getWindowStatus 獲取當前應用窗口的模式
let status = WindowUtil.getWindowStatus();
isFullScreen 判斷窗口是否全屏,默認主窗口
let isFullScreen = WindowUtil.isFullScreen();
isFocused 判斷當前窗口是否已獲焦
let isFocused = WindowUtil.isFocused();
isTransparent 窗口是否透明,默認主窗口
let isTransparent = WindowUtil.isTransparent();
isWindowShowing 判斷當前窗口是否已顯示,默認主窗口
let isWindowShowing = WindowUtil.isWindowShowing();
isWindowSupportWideGamut 判斷當前窗口是否支持廣色域模式,,默認主窗口
let isWindowSupportWideGamut = await WindowUtil.isWindowSupportWideGamut();
setDialogBackGestureEnabled 設置模態窗口是否響應手勢返回事件,非模態窗口調用返回錯誤碼
WindowUtil.setDialogBackGestureEnabled(true).then(() = > {
ToastUtil.showToast("設置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設置失敗!");
});
setGestureBackEnabled 設置當前窗口是否禁用返回手勢功能,僅主窗全屏模式下生效,2in1設備下不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
WindowUtil.setGestureBackEnabled(!isGestureBack).then(() = > {
ToastUtil.showToast("設置成功啦^·^");
}).catch((err: BusinessError) = > {
ToastUtil.showToast("設置失敗!");
});
isGestureBackEnabled 獲取當前窗口是否禁用返回手勢功能,僅主窗全屏模式下生效,2in1設備不生效。
let isGestureBack = WindowUtil.isGestureBackEnabled();
ToastUtil.showToast(`當前窗口是否禁用返回:${isGestureBack}`);
創作不易,請給童長老點贊
審核編輯 黃宇
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
Harmony
+關注
關注
0文章
75瀏覽量
2973
發布評論請先 登錄
相關推薦
熱點推薦
harmony-utils之ToastUtil,吐司工具類
harmony-utils之ToastUtil,吐司工具類 harmony-utils 簡介與說明 [h
harmony-utils之TypeUtil,類型檢查工具類
harmony-utils之TypeUtil,類型檢查工具類 harmony-utils 簡介與說明 [
harmony-utils之WantUtil,Want工具類
harmony-utils之WantUtil,Want工具類 harmony-utils 簡介與說明 [
harmony-utils之Base64Util,Base64工具類
harmony-utils之Base64Util,Base64工具類 harmony-utils 簡介與說明 [
harmony-utils之AES,AES加解密
harmony-utils之AES,AES加解密 harmony-utils 簡介與說明 [harmony-utils] 一款功能豐富且極易上手的HarmonyOS
harmony-utils之DES,DES加解密
harmony-utils之DES,DES加解密 harmony-utils 簡介與說明 [harmony-utils]一款功能豐富且極易上手的HarmonyOS
harmony-utils之RSA,RSA加解密
harmony-utils之RSA,RSA加解密 harmony-utils 簡介與說明 [harmony-utils] 一款功能豐富且極易上手的HarmonyOS
harmony-utils之SHA,SHA工具類
harmony-utils之SHA,SHA工具類 harmony-utils 簡介與說明 [harmon
harmony-utils之MD5,MD5工具類
harmony-utils之MD5,MD5工具類 harmony-utils 簡介與說明 [harmon
harmony-utils之SM3,SM3工具類
harmony-utils之SM3,SM3工具類 harmony-utils 簡介與說明 harmony
harmony-utils之PickerUtil,拍照、文件選擇和保存,工具類
harmony-utils之PickerUtil,拍照、文件選擇和保存,工具類 harmony-utils 簡介與說明
harmony-utils之AuthUtil,生物認證相關工具類
# harmony-utils之AuthUtil,生物認證相關工具類 ## harmony-utils
harmony-utils之ArrayUtil,集合工具類
# harmony-utils之ArrayUtil,集合工具類 ## harmony-utils 簡介與說明 ------[
評論