可以使用Screen中的接口:setOrientation(screen.Orientation.VERTICAL, (err: BusinessError) => {}来设置系统屏幕旋转的方向,

其中Screen中的接口都需先使用getAllScreens()createVirtualScreen()中的任一方法获取到Screen实例,再通过此实例调用对应方法。

 

具体实例如下:

    Column() {
      Text('旋转1 垂直 VERTICAL')
        .fontSize(25)
        .fontWeight(FontWeight.Bold)
        .margin({ left: 100 })
        .onClick(() => {
          screen.getAllScreens((err, src) => {
            console.log("VERTICAL success")
            src[0].setOrientation(screen.Orientation.VERTICAL, (err: BusinessError) => {
              const errCode: number = err.code;
              if (errCode) {
                console.info(`Failed to set the VERTICAL orientation. Code:${err.code},message is ${err.message}`);
                return;
              }
              console.info('Succeeded in setting the VERTICAL orientation.');
            });
          })
        })
        .backgroundColor(Color.Green)
        .width('100%')
        .height('25%')

      Text('旋转2 水平 HORIZONTAL')
        .fontSize(25)
        .fontWeight(FontWeight.Bold)
        .margin({ left: 100 })
        .onClick(() => {
          screen.getAllScreens((err, src) => {
            console.log("HORIZONTAL success")
            src[0].setOrientation(screen.Orientation.HORIZONTAL, (err: BusinessError) => {
              const errCode: number = err.code;
              if (errCode) {
                console.info(`Failed to set the HORIZONTAL orientation. Code:${err.code},message is ${err.message}`);
                return;
              }
              console.info('Succeeded in setting the HORIZONTAL orientation.');
            });
          })
        })
        .backgroundColor(Color.Yellow)
        .width('100%')
        .height('25%')

      Text('旋转3 反向垂直 REVERSE_HORIZONTAL')
        .fontSize(25)
        .fontWeight(FontWeight.Bold)
        .margin({ left: 100 })
        .onClick(() => {
          screen.getAllScreens((err, src) => {
            console.log("REVERSE_HORIZONTAL success")
            src[0].setOrientation(screen.Orientation.REVERSE_HORIZONTAL, (err: BusinessError) => {
              const errCode: number = err.code;
              if (errCode) {
                console.info(`Failed to set the REVERSE_HORIZONTAL orientation. Code:${err.code},message is ${err.message}`);
                return;
              }
              console.info('Succeeded in setting the REVERSE_HORIZONTAL orientation.');
            });
          })
        })
        .backgroundColor(Color.Blue)
        .width('100%')
        .height('25%')

      Text('旋转4 反向水平 REVERSE_VERTICAL')
        .fontSize(25)
        .fontWeight(FontWeight.Bold)
        .margin({ left: 100 })
        .onClick(() => {
          screen.getAllScreens((err, src) => {
            console.log("REVERSE_VERTICAL success")
            src[0].setOrientation(screen.Orientation.REVERSE_VERTICAL, (err: BusinessError) => {
              const errCode: number = err.code;
              if (errCode) {
                console.info(`Failed to set the REVERSE_VERTICAL orientation. Code:${err.code},message is ${err.message}`);
                return;
              }
              console.info('Succeeded in setting the REVERSE_VERTICAL orientation.');
            });
          })
        })
        .backgroundColor(Color.Red)
        .width('100%')
        .height('25%')
    }

 

代码工程及调试验证hap如下:

https://gitee.com/wanfan_yyds/screenrotation

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐