HarmonyOS 鸿蒙Next折叠屏获取屏幕高宽信息
HarmonyOS 鸿蒙Next折叠屏获取屏幕高宽信息
更多关于HarmonyOS 鸿蒙Next折叠屏获取屏幕高宽信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1、在onWindowStageCreate中补充如下代码,使用windowClass.on监听尺寸变化获取宽口宽度。
```
let windowClass: window.Window | undefined = undefined;
try {
  window.getLastWindow(this.context, (err: BusinessError, data) => {
    const errCode: number = err.code;
    if (errCode) {
      console.error('Failed to obtain the top window. Cause: ’ + JSON.stringify(err));
      return;
    }
    windowClass = data;
    try {
      windowClass.on(‘windowSizeChange’, (data) => {
        AppStorage.setOrCreate(‘width’, px2vp(data.width));
      });
    }
    catch (exception) {
      console.error('Failed to enable the listener for window size changes. Cause: ’ + JSON.stringify(exception));
    }
    console.info('Succeeded in obtaining the top window. Data: ’ + JSON.stringify(data));
  });
}
catch (exception) {
  console.error('Failed to obtain the top window. Cause: ’ + JSON.stringify(exception));
}
</code><button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 8px; right: 8px; font-size: 14px;">复制</button></pre> <p>2、在页面aboutToAppear用getDefaultDisplaySync获取宽度,通过[@StorageLink](/user/StorageLink)('width')获取监听到变化的宽度。</p> <pre style="position: relative;"><code class="language-javascript hljs ">
import display from ‘@ohos.display’;
@Component struct Index {
  @StorageLink(‘width’) width1: number = 0;
  aboutToAppear(): void {
    let displayClass: display.Display | null = null;
    displayClass = display.getDefaultDisplaySync();
    this.width1 = px2vp(displayClass.width);
  }
  build() {
    Column() {
      Text(‘屏幕宽度:’+this.width1.toString())
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
    }
    .height(‘100%’)
    .width(‘100%’)
  }
}
更多关于HarmonyOS 鸿蒙Next折叠屏获取屏幕高宽信息的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next系统中,获取折叠屏手机的屏幕高宽信息,可以通过以下步骤实现:
首先,确保折叠屏手机处于所需的状态(完全展开或完全折叠),然后利用系统提供的API来获取屏幕高宽信息。具体代码如下:
import display from '@ohos.display';
let displayClass = display.getDefaultDisplaySync();
let screenWidth = px2vp(displayClass.width);
let screenHeight = px2vp(displayClass.height);
其中,px2vp函数用于将像素单位转换为鸿蒙系统的适配单位vp,以确保在不同屏幕尺寸和分辨率下的一致性。
请注意,在折叠屏手机中,由于屏幕可以折叠和展开,因此屏幕的高宽信息可能会发生变化。如果需要监听屏幕尺寸的变化,可以使用窗口的on('windowSizeChange')方法,并在尺寸变化时更新屏幕高宽信息。
此外,也可以利用DisplayManager类来获取屏幕信息,但通常上述方法已足够满足大多数需求。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。
        
      
                  
                  
                  
