HarmonyOS 如何设置屏幕支持方向为portrait && landscape && landscape_inverted,不需要portrait_inverted

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

HarmonyOS
2024-09-02 09:43:46
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
superinsect

可以通过 @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) 
 } 
}
  • 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.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.

可以设置加快传感器上报频率,默认值为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 });
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

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

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

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

参考:

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

分享
微博
QQ
微信
回复
2024-09-02 17:46:31
相关问题
Swiper切换不需要动画
1428浏览 • 2回复 待解决
如何设置屏幕方向横屏
2477浏览 • 1回复 待解决
HarmonyOS 代码中如何设置屏幕旋转方向
1360浏览 • 1回复 待解决