HarmonyOS 代码中如何设置屏幕旋转方向

应用需要适配ipad,并支持屏幕旋转,希望设备是手机类型时,不支持旋转,类型是平板的,可以旋转,我参考了这些文档,没找到相关设置:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-rotation-transition-animation-V5

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/module-configuration-file-V5

HarmonyOS
2024-12-25 11:34:18
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

通过deviceInfo.deviceType判断机型是平板还是手机,通过 let orientation = window.Orientation.AUTO_ROTATION; //设置窗口方向为传感器自动旋转模式。

示例代码如下:

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


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

@Entry
@Component
struct ScreenTest {
  @State rotation: number = 0
  @State message: string = ORIENTATION[this.rotation]

  aboutToAppear() {
    if(deviceInfo.deviceType == 'phone'){

    }else {
      this.setOrientation()
    }

    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() {
    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 = window.Orientation.AUTO_ROTATION; //设置窗口方向为传感器自动旋转模式。
        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));
    }
    ;
  }

  build() {
    Row() {
      Column() {
        Text(`${this.rotation}`).fontSize(25)
        Text(`${this.message}`).fontSize(25)
      }
      .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.
分享
微博
QQ
微信
回复
2024-12-25 15:02:05
相关问题
HarmonyOS如何设置应用跟随屏幕旋转
1040浏览 • 1回复 待解决
如何设置屏幕方向为横屏
2500浏览 • 1回复 待解决
HarmonyOS 组件旋转后,平移方向出错
592浏览 • 1回复 待解决
HarmonyOS 如何监听屏幕旋转事件?
642浏览 • 1回复 待解决
HarmonyOS 旋转设备获取设备方向问题
896浏览 • 1回复 待解决
HarmonyOS 如何获取屏幕旋转角度
786浏览 • 1回复 待解决
window获取屏幕方向配置
1419浏览 • 1回复 待解决
HarmonyOS 视频播放器如何旋转屏幕
990浏览 • 1回复 待解决
如何实现应用的屏幕自动旋转
3113浏览 • 1回复 待解决
HarmonyOS 传感器判断屏幕方向
842浏览 • 1回复 待解决
获取当前设备的屏幕方向
1054浏览 • 1回复 待解决
有谁知道如何监听屏幕旋转
2818浏览 • 1回复 待解决
HarmonyOS 怎么监听屏幕横竖屏旋转
845浏览 • 1回复 待解决
HarmonyOS Text组件如何设置文字方向
676浏览 • 1回复 待解决