7

如何在OpenHarmony应用侧获取屏幕DPI

 11 months ago
source link: https://www.51cto.com/article/768978.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何在OpenHarmony应用侧获取屏幕DPI

作者:离北况归 2023-10-08 15:23:09
笔者之前分享过如何在设备侧修改屏幕 DPI,本文则介绍在OpenHarmony应用侧获取屏幕DPI。
4519c1461aaf6c620549646b3de26ed6165a33.png

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

1、应用侧获取屏幕DPI 实现思路

如图这是笔者的两台OpenHarmony设备,左边汇思博SEEK100 sl8541e的DPI为213,右边润开鸿 DAYU200的DPI为240。

resize,w_820,h_1280

通过导入@ohos.display包来使用getDefaultDisplaySync()方法以获取屏幕 DPI值的方法。

import display from '@ohos.display';

@Entry
@Component
struct Index {
  @State displayClass: display.Display | null = null

  aboutToAppear(){
    try {
      this.displayClass = display.getDefaultDisplaySync();
      console.info('Test densityDPI:' + JSON.stringify(this.displayClass.densityDPI));
    } catch (exception) {
      console.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception));
    }
  }

  build() {
    Row() {
      Column() {
        Text('屏幕DPI是'+JSON.stringify(this.displayClass.densityDPI))
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

2、更改开发板的屏幕DPI(以DAYU200为例)

(1)方法一:更改源码编译烧录固件

在源码foundation/window/window_manager/resources/config/rk3568/display_manager_config.xml文件中修改dpi,编译后烧录固件。

<!--Window display dpi, valid range is 80~640, use 0 if no configuration is requeired-->
    <dpi>240</dpi>

(2)方法二:发送display_manager_config.xml文件至设备etc/window/resources

OpenHarmony固件编译烧录进入设备后,display_manager_config.xml文件会被预编译打包至设备端etc/window/resources目录下,什么,为什么只有window/resources字段,因为ohos_prebuilt_etc就是把文件放进设备的etc文件夹。

foundation/window/window_manager/resources/config/BULID.gn文件如下,
下面代码的意思是源码foundation/window/window_manager/resources/config/rk3568/display_manager_config.xml会真正安装在设备中的etc/window/resources文件位置,

ohos_prebuilt_etc("display_manager_config") {
  if (device_name == "rk3568") {
    source = "//foundation/window/window_manager/resources/config/rk3568/display_manager_config.xml"
    install_enable = true
···
···
  relative_install_dir = "window/resources"
}

步骤如下:

# 进入sdk\版本号\toolchain输入cmd打开命令行,重新加载系统为可读写
hdc_std shell mount -o remount,rw /

# 替换/etc/window/resources中的display_manager_config.xml文件
hdc_std file send C:\Users\jjh\display_manager_config.xml /etc/window/resources

# 然后重启开发板让设置生效
hdc reboot

3、参考资料

  • https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-display.md。
  • https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/faqs/faqs-graphics.md。

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com

责任编辑:jianghua 来源: 51CTO 开源基础软件社区

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK