diff --git a/ArkUI/orientationDevelopment/README.md b/ArkUI/orientationDevelopment/README.md index f58c9803656078eec5924b8d9cda3a1b59b0ae7f..8d815134f3734a2b17a403c64bb884cd04e797dc 100644 --- a/ArkUI/orientationDevelopment/README.md +++ b/ArkUI/orientationDevelopment/README.md @@ -1,8 +1,8 @@ -# 多设备功能开发同源代码工程 +# 多设备同源代码工程 ## 介绍 -本示例为多设备功能开发同源代码工程,包含最佳实践文档中包含的推荐使用方法的样例代码。工程本身不具备实际功能,开发者请直接阅读文档结合源码来理解多设备功能开发。 +本示例为窗口方向与多设备功能开发同源代码工程,包含最佳实践文档中包含的推荐使用方法的样例代码。工程本身不具备实际功能,开发者请直接阅读文档结合源码来理解多设备功能开发。 ## 效果预览 @@ -11,14 +11,12 @@ ## 工程目录 ``` -entry/src/main/ets -│ ├──entryability -│ │ └──entryability // 应用入口类 -│ ├──entrybackupability -│ │ └──EntryBackupAbility.ets // 应用备份能力类 -│ └──pages -│ └──Index.ets // 首页 -└────entry/src/main/resources +entry/src/main/ +├──configautorotation // 窗口自动旋转 +├──configlandscapeauto // 窗口横屏 +├──configportrait // 窗口竖屏 +├──ets // 主目录 +└──resources // 资源目录 ``` ## 具体实现 diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..508880af8c33aa838016d1cd4b2c68be2f447540 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e4de99282050bad799ac892eb85ac5449364a51 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e2d24ad42693fc877d51bb7820f0a9da68fa135 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/ets/pages/Index.ets @@ -0,0 +1,23 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..01b6011d32a16efa9066ff41154ba5b688b2efa3 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configautorotation/module.json5 @@ -0,0 +1,64 @@ +// [Start module] +{ + "module": { + // [StartExclude module] + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1", + "wearable" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + // [EndExclude module] + "abilities": [ + { + "name": "EntryAbility", + // [StartExclude module] + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + // [EndExclude module] + "orientation": "auto_rotation", + // [StartExclude module] + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + // [EndExclude module] + } + ], + // [StartExclude module] + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + // [EndExclude module] + } +} +// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..508880af8c33aa838016d1cd4b2c68be2f447540 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e4de99282050bad799ac892eb85ac5449364a51 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e2d24ad42693fc877d51bb7820f0a9da68fa135 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/ets/pages/Index.ets @@ -0,0 +1,23 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..f45f9d029f91c61f81f22b479b62672587a1db69 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configlandscapeauto/module.json5 @@ -0,0 +1,64 @@ +// [Start module] +{ + "module": { + // [StartExclude module] + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1", + "wearable" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + // [EndExclude module] + "abilities": [ + { + "name": "EntryAbility", + // [StartExclude module] + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + // [EndExclude module] + "preferMultiWindowOrientation": "landscape_auto", + // [StartExclude module] + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + // [EndExclude module] + } + ], + // [StartExclude module] + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + // [EndExclude module] + } +} +// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..508880af8c33aa838016d1cd4b2c68be2f447540 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entryability/EntryAbility.ets @@ -0,0 +1,44 @@ +import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { window } from '@kit.ArkUI'; + +const DOMAIN = 0x0000; + +export default class EntryAbility extends UIAbility { + onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { + this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + } + + onDestroy(): void { + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); + } + + onWindowStageCreate(windowStage: window.WindowStage): void { + // Main window is created, set main page for this ability + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); + + windowStage.loadContent('pages/Index', (err) => { + if (err.code) { + hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); + return; + } + hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); + }); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); + } + + onForeground(): void { + // Ability has brought to foreground + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); + } + + onBackground(): void { + // Ability has back to background + hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e4de99282050bad799ac892eb85ac5449364a51 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/entrybackupability/EntryBackupAbility.ets @@ -0,0 +1,16 @@ +import { hilog } from '@kit.PerformanceAnalysisKit'; +import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit'; + +const DOMAIN = 0x0000; + +export default class EntryBackupAbility extends BackupExtensionAbility { + async onBackup() { + hilog.info(DOMAIN, 'testTag', 'onBackup ok'); + await Promise.resolve(); + } + + async onRestore(bundleVersion: BundleVersion) { + hilog.info(DOMAIN, 'testTag', 'onRestore ok %{public}s', JSON.stringify(bundleVersion)); + await Promise.resolve(); + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets new file mode 100644 index 0000000000000000000000000000000000000000..8e2d24ad42693fc877d51bb7820f0a9da68fa135 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/ets/pages/Index.ets @@ -0,0 +1,23 @@ +@Entry +@Component +struct Index { + @State message: string = 'Hello World'; + + build() { + RelativeContainer() { + Text(this.message) + .id('HelloWorld') + .fontSize($r('app.float.page_text_font_size')) + .fontWeight(FontWeight.Bold) + .alignRules({ + center: { anchor: '__container__', align: VerticalAlign.Center }, + middle: { anchor: '__container__', align: HorizontalAlign.Center } + }) + .onClick(() => { + this.message = 'Welcome'; + }) + } + .height('100%') + .width('100%') + } +} \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 b/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 new file mode 100644 index 0000000000000000000000000000000000000000..e53c330271e45c24bcf12f1209675100aa3a2ebc --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/configportrait/module.json5 @@ -0,0 +1,64 @@ +// [Start module] +{ + "module": { + // [StartExclude module] + "name": "entry", + "type": "entry", + "description": "$string:module_desc", + "mainElement": "EntryAbility", + "deviceTypes": [ + "phone", + "tablet", + "2in1", + "wearable" + ], + "deliveryWithInstall": true, + "installationFree": false, + "pages": "$profile:main_pages", + // [EndExclude module] + "abilities": [ + { + "name": "EntryAbility", + // [StartExclude module] + "srcEntry": "./ets/entryability/EntryAbility.ets", + "description": "$string:EntryAbility_desc", + "icon": "$media:layered_image", + "label": "$string:EntryAbility_label", + "startWindowIcon": "$media:startIcon", + // [EndExclude module] + "orientation": "portrait", + // [StartExclude module] + "startWindowBackground": "$color:start_window_background", + "exported": true, + "skills": [ + { + "entities": [ + "entity.system.home" + ], + "actions": [ + "action.system.home" + ] + } + ], + // [EndExclude module] + } + ], + // [StartExclude module] + "extensionAbilities": [ + { + "name": "EntryBackupAbility", + "srcEntry": "./ets/entrybackupability/EntryBackupAbility.ets", + "type": "backup", + "exported": false, + "metadata": [ + { + "name": "ohos.extension.backup", + "resource": "$profile:backup_config" + } + ], + } + ], + // [EndExclude module] + } +} +// [End module] \ No newline at end of file diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..c71a990e05a7a705c53851282dd948e83fc9c398 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/MediaQueryPage.ets @@ -0,0 +1,71 @@ +import { mediaquery, window } from '@kit.ArkUI'; +import { common } from '@kit.AbilityKit'; + +// [Start Media] +@Entry +@Component +struct MediaQueryPage { + @State color: string = '#DB7093'; + @State text: string = 'Portrait'; + // The condition is true when the device is in landscape orientation + listener: mediaquery.MediaQueryListener = + this.getUIContext().getMediaQuery().matchMediaSync('(orientation: landscape)'); + // When the media query conditions are met, a callback is triggered + onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { + if (mediaQueryResult.matches as boolean) { + // If the device is in landscape orientation, change the corresponding page layout + this.color = '#FFD700'; + this.text = 'Landscape'; + } else { + this.color = '#DB7093'; + this.text = 'Portrait'; + } + } + aboutToAppear() { + // Bind the current application instance + // Bind a callback function + this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => { + this.onPortrait(mediaQueryResult) + }); + } + aboutToDisappear() { + // Unbind the callback function registered in the listener + this.listener.off('change'); + } + + // Change the horizontal and vertical screen status functions of the device + private changeOrientation(isLandscape: boolean) { + // Obtain the context of the UIAbility instance + let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext; + // Call this API to manually change the horizontal and vertical screen status of the device + window.getLastWindow(context).then((lastWindow) => { + lastWindow.setPreferredOrientation(isLandscape ? window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT) + }); + } + + build() { + Column({ space: 50 }) { + Text(this.text) + .fontSize(50) + .fontColor(this.color) + Text('Landscape') + .fontSize(50) + .fontColor(this.color) + .backgroundColor(Color.Orange) + .onClick(() => { + this.changeOrientation(true); + }) + Text('Portrait') + .fontSize(50) + .fontColor(this.color) + .backgroundColor(Color.Orange) + .onClick(() => { + this.changeOrientation(false); + }) + } + .width('100%') + .height('100%') + } +} + +// [End Media] diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets new file mode 100644 index 0000000000000000000000000000000000000000..72e0ed10f9749da076c654256e9fcd7aa442986e --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/OrientationPage.ets @@ -0,0 +1,91 @@ +// [Start orientation] +import { display, window } from '@kit.ArkUI'; + +const TAG = 'foo'; +const ORIENTATION: Array = ['垂直', '平', '反向垂直', '反向水平']; + +@Entry +@Component +struct OrientationPage { + @State rotation: number = 0; + @State message: string = ORIENTATION[this.rotation]; + @Watch('setWindowLayOut') @State isLandscape: boolean = false; + + aboutToAppear() { + this.setOrientation(1); + let callback = async () => { + let getDefaultDisplay = display.getDefaultDisplaySync(); + this.rotation = getDefaultDisplay.rotation; + this.message = ORIENTATION[this.rotation]; + }; + try { + display.on('change', callback); + } catch (exception) { + hilog.error(0x0000, TAG, 'Failed to register callback. Code: ' + JSON.stringify(exception)); + } + } + + setOrientation(type: number) { + try { + window.getLastWindow(getContext(this), (err, data) => { + //获取window实例 + if (err.code) { + hilog.error(0x0000, TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err)); + return; + } + let windowClass = data; + hilog.info(0x0000, TAG, 'Succeeded in obtaining the top window. Data: ' + JSON.stringify(data)); + let orientation: number; + if (type === 1) { + orientation = window.Orientation.AUTO_ROTATION; + } else { + orientation = window.Orientation.UNSPECIFIED; + } + try { + windowClass.setPreferredOrientation(orientation, (err) => { + if (err.code) { + hilog.error(0x0000, TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err)); + return; + } + hilog.info(0x0000, 'Succeeded in setting window orientation.'); + }); + } catch (exception) { + hilog.error(0x0000, TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception)); + } + }); + } catch (exception) { + hilog.error(0x0000, TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception)); + } + } + + setWindowLayOut() { + window.getLastWindow(getContext(this)).then((windowClass) => { + if (this.isLandscape) { + hilog.info(0x0000, 'OrientationPage', '设置屏幕横屏'); + windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE); + } else { + hilog.info(0x0000, 'OrientationPage', '设置屏幕竖屏'); + windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT); + } + }); + } + + build() { + Row() { + Column() { + Text(`${this.rotation}`) + .fontSize(25) + Text(`${this.message}`) + .fontSize(25) + Button('全屏') + .width(140) + .onClick(() => { + this.isLandscape = !this.isLandscape; + }) + } + .width('100%') + } + .height('100%') + } +} +// [End orientation] diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets new file mode 100644 index 0000000000000000000000000000000000000000..6d7948a9fef7441c53a005affe62aa8330bb79b3 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationA.ets @@ -0,0 +1,25 @@ +import { window } from '@kit.ArkUI'; + +@Component +struct ScreenRotationA { + // [Start orientation] + @StorageLink('mainWindow') mainWindow?: window.Window = undefined; + public lastOrientation?: window.Orientation; + + aboutToAppear(): void { + if (this.mainWindow === undefined) { + return; + } + this.lastOrientation = this.mainWindow!.getPreferredOrientation(); + this.mainWindow!.setPreferredOrientation(window.Orientation.LANDSCAPE); + } + + aboutToDisappear(): void { + this.mainWindow!.setPreferredOrientation(this.lastOrientation) + } + // [End orientation] + + build() { + Text('你好') + } +} diff --git a/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets new file mode 100644 index 0000000000000000000000000000000000000000..80b24fa49d12fc2f810b0994086cfb12253423d8 --- /dev/null +++ b/ArkUI/orientationDevelopment/entry/src/main/ets/pages/ScreenRotationB.ets @@ -0,0 +1,20 @@ +import { common } from '@kit.AbilityKit'; + +@Component +struct ScreenRotationB { + // [Start window] + private windowClass = (this.getUIContext().getHostContext() as common.UIAbilityContext).windowStage.getMainWindowSync() + + aboutToAppear(): void { + this.windowClass.enableLandscapeMultiWindow(); + } + + aboutToDisappear(): void { + this.windowClass.disableLandscapeMultiWindow(); + } + // [End window] + + build() { + Text('你好') + } +}