DisplayUtils.ets
4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
}
}