0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

OpenHarmony如何切换横竖屏?

OpenAtom OpenHarmony 来源:未知 2023-01-18 02:25 次阅读

开源项目 OpenHarmony是每个人的 OpenHarmony 9579cde2-9692-11ed-bfe3-dac502259ad0.png

徐建国(坚果)

江苏润开鸿数字科技有限公司 生态技术专家

前言

在日常开发中,大多APP可能根据实际情况直接将APP的界面方向固定,或竖屏或横屏。但在使用过程中,我们还是会遇到横竖屏切换的功能需求,可能是通过物理重力感应触发,也有可能是用户手动触发。所以本文主要带大家了解在OpenAtom OpenHarmony(以下简称“OpenHarmony”)应用开发的过程中,如何在Stage模型和FA模型下使用对应的接口去完成横竖屏的切换。 本文中OpenHarmony版本为3.2 Beta4,API版本为9。开发板为DAYU200。

FA模型

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

文档:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-inner-app-context.md#contextsetdisplayorientation7

context.setDisplayOrientation setDisplayOrientation(orientation: bundle.DisplayOrientation, callback: AsyncCallback): void 设置当前能力的显示方向(callback形式)。 系统能力: SystemCapability.Ability.AbilityRuntime.Core 参数95a190e8-9692-11ed-bfe3-dac502259ad0.png   示例:
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%')
  }
}
上面这样写太乱了,我们可以封装一下:
import bundle from '@ohos.bundle';
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = '横竖屏切换 '
  @State portrait: boolean = true


  private changePortrait() {
    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));
      });


    }


  }


  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
this.changePortrait()
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Stage模型

从API 9开始,可以使用setPreferredOrientation来切换横竖屏。

文档:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-window.md#setpreferredorientation9

在Stage模型中,使用到的主要是Window(窗口)。在设置横竖屏切换的时候,需要先使用getLastWindow()、createWindow()、findWindow()中的任一方法获取到Window实例,再通过此实例调用对应的方法,本文使用的是getLastWindow。 Window.getLastWindow getLastWindow(ctx: BaseContext): Promise获取当前应用内最后显示的窗口,使用Promise异步回调。 系统能力: SystemCapability.WindowManager.WindowManager.Core 参数:95cba716-9692-11ed-bfe3-dac502259ad0.png   返回值:95d47fa8-9692-11ed-bfe3-dac502259ad0.png   错误码: 以下错误码的详细介绍请参见窗口错误码。95fa15f6-9692-11ed-bfe3-dac502259ad0.png
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 设置窗口的显示方向属性,使用Promise异步回调。 系统能力: SystemCapability.WindowManager.WindowManager.Core 参数:9610352a-9692-11ed-bfe3-dac502259ad0.png   返回值:963309f6-9692-11ed-bfe3-dac502259ad0.png   错误码: 以下错误码的详细介绍请参见窗口错误码。96482ffc-9692-11ed-bfe3-dac502259ad0.png
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));
}
完整代码
importWindowfrom'@ohos.window'
import common from '@ohos.app.ability.common';
@Entry
@Component
struct ArkUIClubTest {
  private portrait: boolean = true
  build() {
    Stack() {
      Button("横竖屏切换")
        .onClick(() => {
          this.changeOrientation()
        })
    }
    .width('100%')
    .height('100%')
  }
  private changeOrientation() {
    let windowClass = null;
    //获取上下文
    //var context = getContext(this) as any
    // 获取上下文,使用common模块
     var context =   getContext(this) as common.UIAbilityContext;
    let promise = Window.getLastWindow(context);
    promise.then((data) => {
      windowClass = data;
      if (this.portrait) {
        //切换成横屏
        let orientation = Window.Orientation.LANDSCAPE;
        windowClass.setPreferredOrientation(orientation, (err) => {
       });
        this.portrait = !this.portrait
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
      }
      else {
        //切换成竖屏
        let orientation = Window.Orientation.PORTRAIT;
        windowClass.setPreferredOrientation(orientation, (err) => {
        });
        this.portrait = !this.portrait
        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));
    });
  }
}

总结

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


原文标题:OpenHarmony如何切换横竖屏?

文章出处:【微信公众号:OpenAtom OpenHarmony】欢迎添加关注!文章转载请注明出处。


声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 鸿蒙
    +关注

    关注

    55

    文章

    2123

    浏览量

    42274
  • OpenHarmony
    +关注

    关注

    24

    文章

    3480

    浏览量

    15425

原文标题:OpenHarmony如何切换横竖屏?

文章出处:【微信号:gh_e4f28cfa3159,微信公众号:OpenAtom OpenHarmony】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    求助,关于STM32F429I-EVAL评估板横切换问题求解

    最近使用的STM32429I-EVAL评估板做个项目调研,系统4.3的显示可以由横切换。结果试验了两天也没有解决?通过宏定义可以实现X和y镜像。但是Xy交换不了。直接没有显示。
    发表于 05-10 06:00

    stm32f429 emwin切换窗口闪的原因?

    我这里GUI初始化的时候全屏都初始化了,但是用的时候只用了右半边,左半边显示的是摄像头的图像,现在是只要切换右半的页面摄像头这边就会闪,不知道是什么问题。
    发表于 04-18 06:51

    浅谈兼容 OpenHarmony 的 Flutter

    OpenHarmony SIG 组织在 Gitee 开源了兼容 OpenHarmony 的 Flutter。该组织主要用于孵化 OpenHarmony 相关的开源生态项目。     ▲ 仓库地址
    的头像 发表于 02-02 15:22 347次阅读
    浅谈兼容 <b class='flag-5'>OpenHarmony</b> 的 Flutter

    OpenHarmony C++公共基础类库应用案例:HelloWorld

    1、程序简介该程序是基于OpenHarmony的C++公共基础类库的简单案例:HelloWorld。该应用案例已在OpenHarmony凌蒙派-RK3568开发板
    的头像 发表于 11-23 08:22 390次阅读
    <b class='flag-5'>OpenHarmony</b> C++公共基础类库应用案例:HelloWorld

    鸿蒙原生应用开发-DevEco Studio中HarmonyOS与OpenHarmony项目的切换

    一、找到该目录 二、修改操作系统类型 三、分别进行开发,一些常规的应用功能实现后,相互切换后都可以正常运行的。前期OpenHarmony项目如果连接开发板比较困难的化,开发完成后,切换成为HarmonyOS后就可以比较详细地看
    发表于 11-13 09:47

    OpenHarmony技术大会 | OpenHarmony技术俱乐部分论坛嘉宾金句

    点击蓝字 ╳ 关注我们 开源项目 OpenHarmony 是每个人的 OpenHarmony 原文标题:OpenHarmony技术大会 | OpenHarmony技术俱乐部分论坛嘉宾金
    的头像 发表于 11-10 20:25 303次阅读

    Unity中国、Cocos为OpenHarmony游戏生态插上腾飞的翅膀

    等关键能力。此外,OpenHarmony的底座技术能力也不断增强,现已拥有16,000 多个API,并在应用开发、多媒体、通信等能力上进行了完善和丰富,将全面支持手机、平板、大、车机等复杂带设备
    发表于 10-23 16:15

    开鸿智谷在鸿OS设备开发实验箱通过OpenHarmony兼容性测评

    、智慧农业、智慧城市等8大行业的综合智能场景,触控开关方式可在不同开发板之间切换烧录调试接口。基于OpenHarmony图形化引擎实现的智慧中控,可实现本地控制各个案例设备,实时查看各个设备及传感器
    发表于 10-19 10:14

    开鸿智谷亮相OpenHarmony Meetup深圳站!

    开源项目OpenHarmony是每个人的OpenHarmonyOpenHarmony正当时”OpenHarmonyMeetup2023城市巡回活动,旨在通过meetup线下交流形式,
    的头像 发表于 10-18 08:30 485次阅读
    开鸿智谷亮相<b class='flag-5'>OpenHarmony</b> Meetup深圳站!

    OpenHarmony竞赛训练营正式启动

    点击蓝字 ╳ 关注我们 开源项目 OpenHarmony 是每个人的 OpenHarmony OpenAtom OpenHarmony(简称“OpenHarmony”)竞赛训练营正式开
    的头像 发表于 10-07 21:10 421次阅读

    OpenHarmony应用开发涉及的主要因素与UX设计规范

    、内容宽度、横向组件数量的调整,避免出现因横竖切换、窗口尺寸变化造成的界面元素不合理空白、模糊、变形、截断的问题。 兼容性,在硬件能力、交互方式、使用场景差异较大的设备上,除了考虑布局位置、内容宽度
    发表于 09-25 15:03

    Qt For OpenHarmony

    OpenHarmony本身触的输入、鼠标/键盘的输入怎么能够把它映射到Qt本身的应用程序框架里去。需要了解Qt的QPA实现及系统接口调用的相关逻辑,就能实现Qt应用程序在OpenHarmony上的运行。 3.1
    发表于 09-15 15:39

    OpenHarmony设备截的5种方式

    本文转载自《OpenHarmony设备截的5种方式 》,作者westinyang ​​ 方式1:系统控制中心 ● 顶部下滑在控制中心里点击截 ● 这种方式最普遍,截图后可到相册查看,不过要注意
    发表于 08-29 14:49

    6步玩转OpenHarmony标准芯片适配

    前言 本文是OpenHarmony标准芯片的适配指南,希望能帮助大家更清晰认识到OpenHarmony芯片的适配过程。本文描述的过程范围,从头从零开始——芯片没有在OpenHarmony适配过,到
    发表于 08-22 09:10

    OpenHarmony轻量系统书籍推荐《OpenHarmony轻量设备开发理论与实战》

    最近大家问的智能家居套件方面有没有可以参考的资料,这里给大家统一回复一下 推荐大家可以看这本书 《OpenHarmony轻量设备开发理论与实战》 本书系统地讲授OpenHarmony 轻量系统 设备
    的头像 发表于 07-20 12:43 830次阅读