0
点赞
收藏
分享

微信扫一扫

OpenHarmony如何查询屏幕/窗口尺寸

如何查询屏幕/窗口尺寸

在应用开发过程中,为了在不同的设备上取得更好的显示效果,开发者可能需要查询屏幕尺寸或应用显示窗口尺寸。

  • 通过display查询显示设备的属性(包括屏幕宽、高和屏幕密度等),详见屏幕属性。

// @ts-nocheck
/*
* Copyright (c) 2021 JianGuo Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import deviceInfo from'@ohos.deviceInfo'
import parameter from '@ohos.systemParameter'
import display from '@ohos.display';

@Entry
@Component
struct GetDeviceTypeSample {
@State deviceType: string = 'deviceType';
@State device: string = 'device';
@State displayInfo: string = 'displayInfo';

aboutToAppear() {
try {
this.deviceType = parameter.getSync("const.build.characteristics");
} catch(e) {
console.log("getSync unexpected error: " + e);
}
}
build() {
Column() {
Text("设备属性").fontSize(36)
//通过js接口查询指定系统参数(const.build.characteristics)进而确定设备类型
Text(this.deviceType).fontSize(28).onClick(() => {
try {
this.deviceType = parameter.getSync("const.build.characteristics");
console.log("坚果"
+
this.deviceType);
} catch (e) {
console.log("getSync unexpected error: " + e);
}

})
//通过deviceInfo查询设备类型
Text( this.device).fontSize(28).onClick(() => {
this.device= deviceInfo.deviceType;
})
//通过display查询显示设备的属性(包括屏幕宽、高和屏幕密度等)
Text(this.displayInfo).fontSize(28).onClick(() => {
display.getDefaultDisplay()
.then((displayInfo) => {
console.info('Display width: ' + displayInfo.width);
console.info('Display height: ' + displayInfo.height);
console.info('Display density: ' + displayInfo.densityDPI);
this.displayInfo=JSON.stringify(displayInfo);
console.info('Display density: ' + JSON.stringify(displayInfo));

})
.catch((error) => {
console.error('Failed to obtain the default display size. Cause: ' + JSON.stringify(error));
})

})
}
.width('100%')
.height('100%').justifyContent(FlexAlign.Center).alignItems(HorizontalAlign.Center)
}
}

运行之后,在控制台打印

{"alive":true,"densityDPI":560,"densityPixels":3.5,"height":2560,"id":0,"name":"内置屏幕","refreshRate":60.000004,"rotation":0,"scaledDensity":3.5,"state":2,"width":1440,"xDPI":560,"yDPI":560}

下面是参数描述

Display

描述display对象的属性。

系统能力: 以下各项对应的系统能力均为 SystemCapability.WindowManager.WindowManager.Core。

名称

参数类型

可读

可写

说明

id

number



显示设备的id号。

name

string



显示设备的名称。

alive

boolean



显示设备是否启用。

state

​​DisplayState​​



显示设备的状态。

refreshRate

number



显示设备的刷新率。

rotation

number



显示设备的屏幕旋转角度。

width

number



显示设备的宽度,单位为像素。

height

number



显示设备的高度,单位为像素。

densityDPI

number



显示设备的屏幕密度,单位为DPI。

densityPixels

number



显示设备的屏幕密度,单位为像素。

scaledDensity

number



显示设备的显示字体的缩放因子。

xDPI

number



x方向中每英寸屏幕的确切物理像素值。

yDPI

number



y方向中每英寸屏幕的确切物理像素值。

举报

相关推荐

0 条评论