DisplayUtils.ets 4.42 KB
import display from '@ohos.display';
import ConfigurationConstant from '@ohos.app.ability.ConfigurationConstant';
import {Logger} from './Logger';

const TAG = 'DisplayUtils';

/**
 * 设备显示器/Display属性:
 *
 手机(HUAWEI Mate 40 Pro & NOH-AN00)-竖屏:
 {
 "id": 0,
 "name": "UNKNOW",
 "alive": true,
 "state": 0,
 "refreshRate": 90,
 "rotation": 0,
 "width": 1344,  // 屏幕显示的实际宽度/(像素个数/以像素px为单位)
 "height": 2772, // 屏幕显示的实际高度/(像素个数/以像素px为单位)
 "densityDPI": 560, // 实际像素密度(每英寸像素:120/160/240/320/560)PPI,是一个具体的数值
 "orientation": 0,
 "densityPixels": 3.5, // 屏幕密度(当像素密度为160时屏幕密度为1.0, 像素比例0.75/1.0/1.5/2.0/3.5) | 缩放因子density = 屏幕像素密度/160, 可用于vp和px转化
 "scaledDensity": 3.5,  // 可用于fp和px转化
 "xDPI": 461.3188781738281,
 "yDPI": 457.1999816894531
 }

 手机(HUAWEI Mate 40 Pro & NOH-AN00)-横屏:
 {
 "id": 0,
 "name": "UNKNOW",
 "alive": true,
 "state": 0,
 "refreshRate": 90,
 "rotation": 1,
 "width": 2772,
 "height": 1344,
 "densityDPI": 560,
 "orientation": 1,
 "densityPixels": 3.5,
 "scaledDensity": 3.5,
 "xDPI": 951.47021484375,
 "yDPI": 221.67271423339844
 }

 Fold手机(HUAWEI Mate X5 & ALT-AL10)-折叠状态-竖屏:
 {
 "id": 0,
 "name": "UNKNOW",
 "alive": true,
 "state": 0,
 "refreshRate": 90,
 "rotation": 0,
 "width": 1080,
 "height": 2504,
 "densityDPI": 500,
 "orientation": 1,
 "densityPixels": 3.125,
 "scaledDensity": 3.125,
 "xDPI": 173.62025451660156,
 "yDPI": 451.0751647949219
 }

 Fold手机(HUAWEI Mate X5 & ALT-AL10)-折叠状态-横屏:
 {
 "id": 0,
 "name": "UNKNOW",
 "alive": true,
 "state": 0,
 "refreshRate": 90,
 "rotation": 3,
 "width": 2504,
 "height": 1080,
 "densityDPI": 500,
 "orientation": 2,
 "densityPixels": 3.125,
 "scaledDensity": 3.125,
 "xDPI": 402.541748046875,
 "yDPI": 194.55319213867188
 }

 Fold手机(HUAWEI Mate X5 & ALT-AL10)-完全展开状态-竖屏:
 {
 "id": 0,
 "name": "UNKNOW",
 "alive": true,
 "state": 0,
 "refreshRate": 90,
 "rotation": 0,
 "width": 2224,
 "height": 2496,
 "densityDPI": 500,
 "orientation": 1,
 "densityPixels": 3.125,
 "scaledDensity": 3.125,
 "xDPI": 357.52911376953125,
 "yDPI": 449.634033203125
 }

 平板电脑(HUAWEI MatePad Pro & WGR-W09)(2in1设备)-横屏:
 {
 "id": 0,
 "name": "UNKNOW",
 "alive": true,
 "state": 0,
 "refreshRate": 60,
 "rotation": 0,
 "width": 2560,
 "height": 1600,
 "densityDPI": 240,
 "orientation": 1,
 "densityPixels": 1.5,
 "scaledDensity": 1.5,
 "xDPI": 239.05882263183594,
 "yDPI": 239.05882263183594
 }
 */
/**
 * 与显示器/display/screen(硬件)设备相关(不可改变或不可升级)属性或操作(应该读取窗口Window的宽高,而不是显示器display的宽高,因为电脑设备的窗口可以动态调整Window窗口大小)
 * 屏幕密度是160时,1vp=1px
 */
export class DisplayUtils {
  /**
   * Get 屏幕显示/display/screen Width(单位vp)
   * 手机(HUAWEI Mate 40 Pro & NOH-AN00)-竖屏:width为1344*3.5=384vp
   * 手机(HUAWEI Mate 40 Pro & NOH-AN00)-横屏:width为2772*3.5=792vp
   * @returns screen width.
   */
  public static getDeviceWidth(): number {
    let displayObject: display.Display = display.getDefaultDisplaySync();
    // Logger.info(TAG, `getDeviceWidth, displayObject: ${JSON.stringify(displayObject)}`);
    let screenWidth = displayObject.width;
    let screenDensityDPI = displayObject.densityDPI;
    // 像素密度(Pixel Density)
    let densityPixels = screenDensityDPI / ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_MDPI
    // let densityPixels = displayObject.densityPixels
    let deviceWidth = screenWidth / densityPixels;
    // Logger.info(TAG, `getDeviceWidth, deviceWidth: ${deviceWidth}`);
    return deviceWidth
  }

  /**
   * Get the 显示/display/screen height(单位vp).
   *
   * @returns screen height.
   */
  public static getDeviceHeight(): number {
    let displayObject: display.Display = display.getDefaultDisplaySync();
    let screenHeight = displayObject.height;
    let screenDensityDPI = displayObject.densityDPI;
    // 像素密度(Pixel Density)
    let densityPixels = screenDensityDPI / ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_MDPI
    // let densityPixels = displayObject.densityPixels
    let deviceHeight = screenHeight / densityPixels
    // Logger.info(TAG, `getDeviceHeight, deviceHeight: ${deviceHeight}`); // 345.6 // 711.68
    return deviceHeight
  }
}