获取屏幕宽高尺寸 - 鸿蒙 HarmonyOS Next

基于 ArkUI 中 display 的 getDefaultDisplaySync 方法实现获取屏幕宽高尺寸;

除此之外 api 中提供一些分辨率之类的参数,具体可参考官网文档按需增配;

具体实现如下:

import {display} from '@kit.ArkUI'

export enum ScreenType { // 屏幕类型
  Width = 0,
  Height = 1,
}

/*公共方法类*/
export class PublicUtils {
  /**
   * 获取屏幕尺寸
   * @param type 宽 & 高
   * @returns 
   */
  static getScreenSize(type: ScreenType): number {
    let displayClass: display.Display | null = null;
    try {
      displayClass = display.getDefaultDisplaySync()
      if (type === ScreenType.Width) {
        let width = displayClass.width
        return width
      } else {
        let height = displayClass.height
        return height
      }
    } catch (exception) {
      console.log('[公共方法] - 获取屏幕尺寸')
      console.error('[异常]' + JSON.stringify(exception))
      return 0
    }
  }
}

以上便是此次分享的全部内容,希望能对大家有所帮助!

猜你喜欢

转载自blog.csdn.net/survivorsfyh/article/details/139675511
今日推荐