1# 图形图像开发常见问题 2 3## 如何获取设备的dpi值 4 5适用于:OpenHarmony 3.2 Beta5,API 9 Stage模型 6 7**解决措施** 8 9导入@ohos.display包,通过getDefaultDisplaySync\(\)方法获取。 10 11**代码示例** 12 13``` 14import display from '@ohos.display'; 15let displayClass = null; 16try { 17 displayClass = display.getDefaultDisplaySync(); 18 console.info('Test densityDPI:' + JSON.stringify(displayClass.densityDPI)); 19} catch (exception) { 20 console.error('Failed to obtain the default display object. Code: ' + JSON.stringify(exception)); 21} 22``` 23 24## 如何隐藏状态栏实现沉浸式效果 25 26适用于:OpenHarmony 3.2 Beta5,API 9 Stage模型 27 28**解决措施** 29 301. 可以在onWindowStageCreate方法获取windowClass对象。 31 32 ``` 33 onWindowStageCreate(windowStage) { 34 // Main window is created, set main page for this ability 35 console.log("[Demo] MainAbility onWindowStageCreate") 36 windowStage.getMainWindow((err, data) => { 37 if (err.code) { 38 console.error('Failed to obtain the main window.') 39 return; 40 } 41 // 获取到窗口对象 42 globalThis.windowClass = data; 43 }) 44 } 45 ``` 46 472. 设置窗口全屏,隐藏状态栏。 48 49 ``` 50 globalThis.windowClass.setFullScreen(isFullScreen, (err, data) => { 51 if (err.code) { 52 console.error('Failed to enable the full-screen mode. Cause:' + JSON.stringify(err)); 53 return; 54 } 55 console.info('Succeeded in enabling the full-screen mode. Data: ' + JSON.stringify(data)); 56 }); 57 ``` 58 59 60## 如何获取窗口的宽高信息 61 62适用于:OpenHarmony SDK 3.2 Beta5,API 9 Stage模型 63 64**解决措施** 65 66引入窗口模块@ohos.window,获取指定窗口对象Window后,在该对象上使用getWindowProperties\(\)获取窗口各个属性,在属性windowRect中获取窗口宽高信息。 67 68**代码示例** 69 70``` 71import window from '@ohos.window'; 72let windowClass = null; 73try { 74 let promise = window.getLastWindow(this.context); 75 promise.then((data)=> { 76 //获取窗口对象 77 windowClass = data; 78 try { 79 //获取窗口属性 80 let properties = windowClass.getWindowProperties(); 81 let rect = properties.windowRect; 82 //rect.width: 窗口宽度;rect.height: 窗口高度 83 } catch (exception) { 84 console.error('Failed to obtain the window properties. Cause: ' + JSON.stringify(exception)); 85 } 86 console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); 87 }).catch((err)=>{ 88 console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err)); 89 });} catch (exception) { 90 console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception)); 91} 92``` 93 94