当前位置: 首页 > news >正文

【HarmonyOS】鸿蒙应用如何进行页面横竖屏切换以及注意事项,自动切换横竖屏,监听横竖屏

【HarmonyOS】鸿蒙应用如何进行页面横竖屏切换以及注意事项,自动切换横竖屏,监听横竖屏

一、鸿蒙应用如何进行页面横竖屏调用API手动切换

1.首先要在EntryAbility 中获取主窗口对象
EntryAbility.ets

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';export default class EntryAbility extends UIAbility {onWindowStageCreate(windowStage: window.WindowStage): void {// 挂载globalThis上,可以当全局对象使用。当然此处实现方式因人而异,你可以放在单例里,或者localstore中等等globalThis.windowClass = windowStage.getMainWindowSync();windowStage.loadContent('pages/RotationTestPage', (err) => {if (err.code) {return;}});}
}

之后在需要调用横竖屏切换的页面或者逻辑中调用,我这里用按钮触发举例:
RotationTestPage.ets

import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';

struct RotationTestPage {private TAG: string = "RotationTestPage";onClickRotation = ()=>{// 设置横竖屏状态let orientation = window.Orientation.LANDSCAPE;try{globalThis.windowClass.setPreferredOrientation(orientation, (err: BusinessError) => {if(err.code){console.error(this.TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err));return;}console.info(this.TAG,'Succeeded in setting window orientation.');});}catch (exception) {console.error(this.TAG,'Failed to set window orientation. Cause: ' + JSON.stringify(exception));}}build() {RelativeContainer() {Text("点击切换为横屏").id('RotationTestPageHelloWorld').fontSize(50).fontWeight(FontWeight.Bold).alignRules({center: { anchor: '__container__', align: VerticalAlign.Center },middle: { anchor: '__container__', align: HorizontalAlign.Center }}).onClick(this.onClickRotation)}.height('100%').width('100%')}
}

window.Orientation具体数值,参见以下网址

注意:
设置主窗口的显示方向属性。仅在支持跟随sensor旋转的设备上生效,子窗口调用后不生效。

二、如何实现应用的屏幕自动旋转

在module.json5添加属性"orientation": “auto_rotation”。

"abilities": [ { "name": "EntryAbility", "srcEntry": "./ets/entryability/EntryAbility.ets", "description": "$string:EntryAbility_desc", "icon": "$media:icon", "label": "$string:EntryAbility_label", "startWindowIcon": "$media:startIcon", "startWindowBackground": "$color:start_window_background", "exported": true, "skills": [ { "entities": [ "entity.system.home" ], "actions": [ "action.system.home" ] } ], "orientation": "auto_rotation", // 随传感器旋转 } 
]

注意:
auto_rotation随传感器旋转 需要在系统下滑菜单中,放开自动锁定状态才可生效。

三、如何监听屏幕旋转

使用媒体查询接口监听屏幕旋转

import { mediaquery } from '@kit.ArkUI'; 
let listener = mediaquery.matchMediaSync('(orientation: landscape)'); // 监听横屏事件 
function onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { if (mediaQueryResult.matches) { // do something here } else { // do something here } 
} 
listener.on('change', onPortrait) // 注册回调 
listener.off('change', onPortrait) // 去注册回调
http://www.lryc.cn/news/515340.html

相关文章:

  • 编译 C++ 程序:分离与保留调试信息以支持 GDB 对 Core 文件的调试
  • 009:传统计算机视觉之边缘检测
  • JAVA创建绘图板JAVA构建主窗口鼠标拖动来绘制线条
  • 机器人对物体重定向操作的发展简述
  • 自动驾驶三维重建
  • 30分钟学会css
  • vue路由模式面试题
  • Python 开发框架搭建简单博客系统:代码实践与应用
  • 如何在 VSCode 中配置 C++ 开发环境:详细教程
  • 三甲医院等级评审八维数据分析应用(一)--组织、制度、管理可视化篇
  • 2024 年度总结|勇敢去探索~
  • 2024年, Milvus 社区的那些事
  • vue代理问题
  • Git快速入门(三)·远程仓库GitHub以及Gitee的使用
  • [开源]C++代码分享
  • CSS3——3. 书写格式二
  • PHP语言的计算机基础
  • 第 23 章 JSON
  • Java 正则表达式入门与应用(详细版)
  • 洛谷:P1540 [NOIP2010 提高组] 机器翻译
  • 基于AT89C51单片机的可暂停八路抢答器设计
  • 面试题解,Java中的“对象”剖析
  • 行为模式3.迭代器模式
  • 第8章 DMA控制器
  • 后端java开发路由接口并部署服务器(四)
  • 检索增强生成 和思维链 结合: 如何创建检索增强思维链 (RAT)?
  • 在 SQL 中,区分 聚合列 和 非聚合列(nonaggregated column)
  • 单元测试3.0+ @RunWith(JMockit.class)+mock+injectable+Expectations
  • STM32第十一课:STM32-基于标准库的42步进电机的简单IO控制(附电机教程,看到即赚到)
  • MotionCtrl: A Unified and Flexible Motion Controller for Video Generation 论文解读