关于openharmony:OpenHarmony如何切换横竖屏

前言

在日常开发中,大多APP可能依据理论状况间接将APP的界面方向固定,或竖屏或横屏。但在应用过程中,咱们还是会遇到横竖屏切换的性能需要,可能是通过物理重力感应触发,也有可能是用户手动触发。所以本文次要带大家理解在OpenAtom OpenHarmony(以下简称“OpenHarmony”)利用开发的过程中,如何在Stage模型和FA模型下应用对应的接口去实现横竖屏的切换。

本文中OpenHarmony版本为3.2 Beta4,API版本为9。开发板为DAYU200。

FA模型

FA模型下,setDisplayOrientation和setDisplayOrientation是切换横竖屏的接口。

文档:https://gitee.com/openharmony…

context.setDisplayOrientation
setDisplayOrientation(orientation:bundle.DisplayOrientation, callback: AsyncCallback<void>): void

设置以后能力的显示方向(callback模式)。

零碎能力:SystemCapability.Ability.AbilityRuntime.Core

参数:

示例:

import featureAbility from '@ohos.ability.featureAbility';
import bundle from '@ohos.bundle';
//FA模型下获取context
var context = featureAbility.getContext();
var orientation = bundle.DisplayOrientation.UNSPECIFIED;
context.setDisplayOrientation(orientation, (err) => {
    console.info("setDisplayOrientation err: " + JSON.stringify(err));
});

残缺代码

import bundle from '@ohos.bundle';
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = '横竖屏切换 '
  @State portrait: boolean = true

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
          var context = featureAbility.getContext();
          if (this.portrait) {

            // 横屏
            var orientation = bundle.DisplayOrientation.LANDSCAPE;
            context.setDisplayOrientation(orientation, (err) => {
              this.portrait = !this.portrait
              console.info("setDisplayOrientation err: " + JSON.stringify(err));
            });
          } else {
            //竖屏
            var orientation = bundle.DisplayOrientation.PORTRAIT;
            context.setDisplayOrientation(orientation, (err) => {
              this.portrait = !this.portrait
              console.info("setDisplayOrientation err: " + JSON.stringify(err));
            });      
          }
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Stage模型

从API 9开始,能够应用setPreferredOrientation来切换横竖屏。

文档:https://gitee.com/openharmony…

在Stage模型中,应用到的次要是Window(窗口)。在设置横竖屏切换的时候,须要先应用getLastWindow()、createWindow()、findWindow()中的任一办法获取到Window实例,再通过此实例调用对应的办法,本文应用的是getLastWindow。

Window.getLastWindow
getLastWindow(ctx: BaseContext): Promise<Window>
获取以后利用内最初显示的窗口,应用Promise异步回调。
零碎能力:SystemCapability.WindowManager.WindowManager.Core

参数:

返回值:

错误码:以下错误码的具体介绍请参见窗口错误码。

let windowClass = null;
try {
    let promise = window.getLastWindow(this.context);
    promise.then((data)=> {
        windowClass = data;
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
    }).catch((err)=>{
        console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
    });
} catch (exception) {
    console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}

而后就能够应用setPreferredOrientation属性。

setPreferredOrientation

setPreferredOrientation(orientation: Orientation): Promise<void>

设置窗口的显示方向属性,应用Promise异步回调。

零碎能力:SystemCapability.WindowManager.WindowManager.Core

参数:

返回值:

错误码:以下错误码的具体介绍请参见窗口错误码。

let orientation = window.Orientation.AUTO_ROTATION;
try {
    let promise = windowClass.setPreferredOrientation(orientation);
    promise.then(()=> {
        console.info('Succeeded in setting the window orientation.');
    }).catch((err)=>{
        console.error('Failed to set the window orientation. Cause: ' + JSON.stringify(err));
    });
} catch (exception) {
    console.error('Failed to set window orientation. Cause: ' + JSON.stringify(exception));
}

总结

本文带大家应用对应的接口,在Stage模型和FA模型下实现了横竖屏的切换。其中还波及到了上下文的获取:Stage模型用(getContext(this) as any),FA模型(featureAbility.getContext()),大家能够在此基础上利用生命周期的回调,在适合的中央实现对应的操作。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理