OpenHarmony开发者论坛
标题:
如何在OpenHarmony应用侧获取屏幕DPI
[打印本页]
作者:
润开鸿_贾佳豪
时间:
2023-11-27 11:36
标题:
如何在OpenHarmony应用侧获取屏幕DPI
[md]笔者之前分享过[如何在设备侧修改屏幕 DPI](
https://ost.51cto.com/posts/21367
),本文则介绍在OpenHarmony应用侧获取`屏幕DPI`。
---
## 1.应用侧获取屏幕DPI 实现思路
- 如图这是笔者的两台OpenHarmony设备,左边[汇思博SEEK100 sl8541e](
https://gitee.com/wisbokit/resources/tree/master
)的DPI为213,右边[润开鸿 DAYU200](
https://gitee.com/hihope_iot/docs/tree/master/HiHope_DAYU200
)的DPI为240
![af864212b74a9859f4bad5cf1aec8e7.jpg](
https://dl-harmonyos.51cto.com/i ... rocess=image/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为例)
### 2.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.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/do ... /js-apis-display.md
-
https://gitee.com/openharmony/do ... qs/faqs-graphics.md
[/md]
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5