DEV Community

桃花镇童长老
桃花镇童长老

Posted on

DisplayUtil, screen related tool class

Introduction and description of harmony-utils


harmony-utils A HarmonyOS tool library with rich features and extremely easy to use, with the help of many practical tools, is committed to helping developers quickly build Hongmeng applications. Its encapsulated tools cover APP, device, screen, authorization, notification, inter-thread communication, pop-up frames, toast, biometric authentication, user preferences, taking photos, albums, scanning codes, files, logs, exception capture, characters, strings, numbers, collections, dates, random, base64, encryption, decryption, JSON and other functions, which can meet various development needs.

picker_utils It is a sub-store split by harmony-utils, including PickerUtil, PhotoHelper, and ScanUtil.

Download and install

ohpm i @pura/harmony-utils

ohpm i @pura/picker_utils

  //Global initialization method, initialized in the onCreate method of UIAbility AppUtil.init()
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    AppUtil.init(this.context);
  }
Enter fullscreen mode Exit fullscreen mode

API methods and usage


getDefaultDisplaySync Gets the current default display object
 let display = DisplayUtil.getDefaultDisplaySync();
 let displayStr = JSON.stringify(display, null, 2);
Enter fullscreen mode Exit fullscreen mode
getPrimaryDisplaySync Get home screen information. Devices other than 2in1 obtain the Display object with the device's own screen; when the 2in1 device connects to the external screen, it obtains the Display object with the current home screen; when the 2in1 device does not have an external screen, it obtains the Display object with the screen.
 let display = DisplayUtil.getPrimaryDisplaySync();
 let displayStr = JSON.stringify(display, null, 2);
Enter fullscreen mode Exit fullscreen mode
getAllDisplays Get all current display objects and use Promise asynchronous callback
  let allDisplay = await DisplayUtil.getAllDisplays();
  let displayStr = JSON.stringify(allDisplay, null, 2);
Enter fullscreen mode Exit fullscreen mode
getWidth Gets the screen width of the device in px
 let width = DisplayUtil.getWidth();
 ToastUtil.showToast(`当前屏幕宽度为:${width}px`);
Enter fullscreen mode Exit fullscreen mode
getHeight Get the screen height of the device in px
 let height = DisplayUtil.getHeight();
 ToastUtil.showToast(`当前屏幕宽高度:${height}px`);
Enter fullscreen mode Exit fullscreen mode
getOrientation Gets the current display direction of the device
 let orientation = DisplayUtil.getOrientation();
 ToastUtil.showToast(`设备当前显示的方向:${orientation}`);
Enter fullscreen mode Exit fullscreen mode
getDisplayState Gets the status of the device
 let state = DisplayUtil.getDisplayState();
 ToastUtil.showToast(`当前设备的状态:${state}`);
Enter fullscreen mode Exit fullscreen mode
getCutoutRect Get information on unavailable screen areas such as hole-punch screen, notch screen, and waterfall screen. It is recommended to use layout to avoid this area
 let rect = await DisplayUtil.getCutoutRect();
 let cutoutInfoStr = JSON.stringify(rect, null, 2);
Enter fullscreen mode Exit fullscreen mode
getCutoutHeight Get the height of the unavailable screen area such as hole-punch screen, notch screen, etc., in px
 let h = await DisplayUtil.getCutoutHeight();
 ToastUtil.showToast(`挖孔屏、刘海屏等不可用屏幕区域的高度:${h}`);
Enter fullscreen mode Exit fullscreen mode
isFoldable Check if the device is foldable
 let bl = DisplayUtil.isFoldable();
 ToastUtil.showToast(`设备是否可折叠:${bl}`);
Enter fullscreen mode Exit fullscreen mode
getFoldStatus Gets the current folding status of the foldable device
 let status = DisplayUtil.getFoldStatus();
 ToastUtil.showToast(`折叠设备的当前折叠状态:${status}`);
Enter fullscreen mode Exit fullscreen mode
getFoldDisplayMode Gets the display mode of the foldable device
  let mode = DisplayUtil.getFoldDisplayMode();
  ToastUtil.showToast(`可折叠设备的显示模式:${mode}`);
Enter fullscreen mode Exit fullscreen mode
onFoldStatusChange enables monitoring of folding state changes in folding device
ToastUtil.showToast("开启折叠设备折叠状态变化的监听");
DisplayUtil.onFoldStatusChange((foldStatus: display.FoldStatus) => {
  let foldStatusStr = JSON.stringify(foldStatus, null, 2);
});
Enter fullscreen mode Exit fullscreen mode
offFoldStatusChange Turn off monitoring of folding state changes in folding device
 ToastUtil.showToast("关闭折叠设备折叠状态变化的监听");
 DisplayUtil.offFoldStatusChange();
Enter fullscreen mode Exit fullscreen mode
onFoldAngleChange Turns on monitoring of folding angle changes in folding device. If it is a bifold shaft device, there are two angle values; when the charging port is facing downward, the folding shaft one and folding shaft two are respectively from right to left.
ToastUtil.showToast("开启折叠设备折叠角度变化的监听");
DisplayUtil.onFoldAngleChange((angles: Array<number>) => {
  LogUtil.info(`折叠角度变化的监听:${angles}`);
  ToastUtil.showLong(`折叠角度变化的监听:${angles}`);
});
Enter fullscreen mode Exit fullscreen mode
offFoldAngleChange Turn off monitoring of folding angle changes in folding device
 ToastUtil.showToast("关闭折叠设备折叠角度变化的监听");
 DisplayUtil.offFoldAngleChange();
Enter fullscreen mode Exit fullscreen mode
isCapture Check whether the device is taking screenshots, projecting, and recording screens.
 let isCaptured = DisplayUtil.isCaptured();
 ToastUtil.showToast(`设备是否正在截屏、投屏、录屏:${isCaptured}`);
Enter fullscreen mode Exit fullscreen mode
onCaptureStatusChange enables monitoring of screenshots, projections, and recording status changes
ToastUtil.showToast("开启屏幕截屏、投屏、录屏状态变化的监听");
DisplayUtil.onCaptureStatusChange((captureStatus: boolean) => {
  LogUtil.info(`屏幕截屏、投屏、录屏状态:${captureStatus}`);
  ToastUtil.showLong(`屏幕截屏、投屏、录屏状态:${captureStatus}`);
});
Enter fullscreen mode Exit fullscreen mode
offCaptureStatusChange Close monitoring of changes in screen capture, projection, and recording status
 ToastUtil.showToast("关闭屏幕截屏、投屏、录屏状态变化的监听");
 DisplayUtil.offCaptureStatusChange();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)