如何查詢螢幕/視窗尺寸
在應用開發過程中,為了在不同的裝置上取得更好的顯示效果,開發者可能需要查詢螢幕尺寸或應用顯示視窗尺寸。
- 通過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 |