HarmonyOS 设备自动旋转

HarmonyOS设备支持屏幕自动旋转吗?如何监听设备旋转控制页面横竖屏展示

HarmonyOS
2024-12-18 16:27:14
3905浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

应用设置屏幕自动旋转:在模块配置文件module.json5中给EntryAbility设置"orientation": “auto_rotation_restricted”,再打开手机自动旋转即可。应用中通过window.getLastWindow 获取window实例–>用setPreferredOrientation设置窗口显示方向的属性。通过diplay.on可以监听屏幕状态改变。点击设置了具体方向后,再加上传感器模式判断屏幕方向。demo如下:

import window from '@ohos.window';
import display from '@ohos.display';

const TAG = 'foo'
const ORIENTATION: Array<string> = ['垂直','平', '反向垂直', '反向水平']

@Entry
@Component
struct Index {

  @State rotation: number = 0
  @State message: string = ORIENTATION[this.rotation]
  @Watch('setWindowLayOut') @State isLandscape: boolean = false;// 是否横屏状态

  aboutToAppear() {

    this.setOrientation(1)
    let callback = async () => {
      let d = await display.getDefaultDisplaySync()
      this.rotation = d.rotation
      this.message = ORIENTATION[this.rotation]
      console.info(TAG, JSON.stringify(d))
    }
    try {
      display.on("change", callback);//监听屏幕状态改变
    } catch (exception) {
      console.error(TAG, 'Failed to register callback. Code: ' + JSON.stringify(exception));
    }
  }

  setOrientation(type : number) {
    try {
      window.getLastWindow(getContext(this), (err, data) => { //获取window实例
        if (err.code) {
          console.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(err));
          return;
        }
        let windowClass = data;
        console.info(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) {
              console.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(err));
              return;
            }
            console.info(TAG, 'Succeeded in setting window orientation.');
          });
        } catch (exception) {
          console.error(TAG, 'Failed to set window orientation. Cause: ' + JSON.stringify(exception));
        }
        ;
      });
    } catch (exception) {
      console.error(TAG, 'Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
    }
    ;
  }

  setWindowLayOut() {
    // 调用该接口手动改变设备横竖屏状态(设置全屏模式,先强制横屏,再加上传感器模式)
    window.getLastWindow(getContext(this)).then((windowClass) => {
      if (this.isLandscape) {
        console.log('设置屏幕横屏')
        windowClass.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE);

      } else {
        console.log('设置屏幕竖屏')
        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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
分享
微博
QQ
微信
回复
2024-12-18 18:37:30


相关问题
HarmonyOS 旋转设备获取设备方向问题
853浏览 • 1回复 待解决
屏幕自动旋转的示例有哪些?
1037浏览 • 1回复 待解决
如何实现应用的屏幕自动旋转
3101浏览 • 1回复 待解决
ArkTS对象怎么自动同步到其他设备
251浏览 • 0回复 待解决
HarmonyOS 组件旋转
630浏览 • 1回复 待解决
HarmonyOS pixelMap旋转
590浏览 • 1回复 待解决
HarmonyOS 拍摄视频旋转问题
484浏览 • 1回复 待解决
HarmonyOS 旋转动画效果
743浏览 • 1回复 待解决
HarmonyOS 如何监听屏幕旋转事件?
633浏览 • 1回复 待解决
HarmonyOS 如何实现Y轴旋转
681浏览 • 1回复 待解决
HarmonyOS 图片旋转角度问题
544浏览 • 1回复 待解决
HarmonyOS 如何实现旋转动画
1165浏览 • 1回复 待解决
HarmonyOS如何设置应用跟随屏幕旋转
1029浏览 • 1回复 待解决
HarmonyOS 如何获取屏幕旋转角度
769浏览 • 1回复 待解决