HarmonyOS 如何设置屏幕支持方向为portrait && landscape && landscape\_inverted,不需要portrait\_inverted?

通过项目配置或者代码,设置只支持竖屏 + 横屏 + 反向横屏,不支持反向竖屏。 比如像Xcode 。

阅读 759
1 个回答

可以通过 @ohos.window 的 setPreferredOrientation属性设置窗口的显示方向,具体使用参数参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5\#setpreferredorientation9

请您验证下以下demo能否解决您的问题:

import window from '@ohos.window';
import sensor from '@ohos.sensor';
import common from '@ohos.app.ability.common';
import settings from '@ohos.settings';
import mediaquery from '@ohos.mediaquery'

enum CurStatus {
 AUTO,
 NO_AUTO
}

const TAG = 'wth';
let context = getContext(this) as common.UIAbilityContext;
let lastOrientation: number = -1;
let curOrientation = -1;
let curState = CurStatus.AUTO;
let portraitFunc = null

/**
 * 横竖屏旋转
 */
@Entry
@Component
struct SampleVideoListPage {
 context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext

 async setSystemBar(status: boolean) {
  let windowClass = await window.getLastWindow(context)
  //设置导航栏,状态栏不可见
  if (status) {
   await windowClass.setWindowSystemBarEnable(['status'])
  } else {
   await windowClass.setWindowSystemBarEnable([])
  }
 }

 listener = mediaquery.matchMediaSync('(orientation: landscape)') // 当设备横屏时条件成立

 onPortrait(mediaQueryResult) {
  if (mediaQueryResult.matches) {
   // 设置窗口布局为沉浸式布局
   this.setSystemBar(false);
  } else {
   this.setSystemBar(true);
  }
 }
 aboutToAppear() {
  portraitFunc = this.onPortrait.bind(this) // 绑定当前应用实例
  this.listener.on('change', portraitFunc)

  try {
   sensor.on(sensor.SensorId.ACCELEROMETER, function (event) {  // 需要增加对应权限
    let status = settings.getValueSync(context, settings.general.ACCELEROMETER_ROTATION_STATUS, "0");

    console.log(TAG, status);
    if(status == '0'){
     return;
    }
    getCurrentOrientation(event);
    if (curState == CurStatus.NO_AUTO) {
     if (lastOrientation == 1 && (curOrientation < 20 || curOrientation > 340)) {
      curState = CurStatus.AUTO;
     }
     if (lastOrientation == 4 && (curOrientation < 280 && curOrientation > 260)) {
      curState = CurStatus.AUTO;
     }
     return;
    }
    if (curOrientation < 20 || curOrientation > 340) {
     lastOrientation = window.Orientation.PORTRAIT; // 1
    } else if (curOrientation < 200 && curOrientation > 160) {
     lastOrientation = window.Orientation.PORTRAIT_INVERTED; // 3
    }
    if (curOrientation < 100 && curOrientation > 80) { //90度 右横屏
     lastOrientation = window.Orientation.LANDSCAPE; // 2
    }
    if (curOrientation < 280 && curOrientation > 260) { //270度 左横屏
     lastOrientation = window.Orientation.LANDSCAPE_INVERTED; // 4
    }

    window.getLastWindow(context).then((lastWindow) => {
     lastWindow.setPreferredOrientation(lastOrientation);
    });


   },);
  } catch (error) {
   console.error(`Failed to invoke on. Code: ${error.code}, message: ${error.message}`);
  }
 }
 build() {
  Column() {
   Text("横竖屏旋转案例")
    .fontSize(26)
    .fontColor(Color.White)
    .margin(10)
    .fontWeight(FontWeight.Bold)
    .backgroundColor(Color.Black)
   Button("横竖屏切换按钮")
    .margin(10)
    .padding(10)
    .onClick(() => {
     curState = CurStatus.NO_AUTO;
     if (lastOrientation == 1 || lastOrientation == 3) {
      lastOrientation = 4;
     } else {
      lastOrientation = 1;
     }
     window.getLastWindow(context).then((lastWindow) => {
      lastWindow.setPreferredOrientation(lastOrientation);
     });
    })
   Image($r('app.media.ic_low'))
  }.width('100%').height('100%').alignItems(HorizontalAlign.Center)
 }
}

可以设置加快传感器上报频率,默认值为200000000ns。

参考文档:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-sensor-V5\#accelerometer9

例如:设置上报间隔为50000000 ns

sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => {
 console.info('Succeeded in invoking on. X-coordinate component: ' + data.x);
 console.info('Succeeded in invoking on. Y-coordinate component: ' + data.y);
 console.info('Succeeded in invoking on. Z-coordinate component: ' + data.z);
}, { interval: 50000000 });

调整传感器的上报频率确实会对性能产生消耗。

例如,如果您在使用加速度传感器时设置了较高频率,这将导致更频繁的传感器数据变化通知,从而增加设备的处理负担,可能会影响设备的电池续航时间。

因此,在进行传感器频率调整时,需要根据具体的应用场景和设备的能耗情况进行权衡和优化。您可以在不需要的横竖屏切换的场景下,取消传感器订阅;

参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-sensor-V5\#accelerometer9-1

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进