横竖屏旋转demo有哪些?

横竖屏旋转demo

HarmonyOS
2024-05-26 12:24:38
370浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
QW_MA

使用的核心API

Button('旋转').onClick((event: ClickEvent) => { 
    curState = CurStatus.NO_AUTO; 
    if (lastOrientation == 1 || lastOrientation == 3) { 
      lastOrientation = 4; 
    } else { 
      lastOrientation = 1; 
    } 
    window.getLastWindow(context).then((lastWindow) => { 
      lastWindow.setPreferredOrientation(lastOrientation); 
    }); 
  }) 
} 
.width('100%') 
.height(this.videoHeight) 
.backgroundColor(Color.Black)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

核心代码解释

async setSystemBar(status: boolean) { 
    let windowClass = await window.getLastWindow(context) 
    //设置导航栏,状态栏不可见 
    if (status) { 
      await windowClass.setWindowSystemBarEnable(['status']) 
    } else { 
      await windowClass.setWindowSystemBarEnable([]) 
    } 
  } 
  
  listener: mediaquery.MediaQueryListener = mediaquery.matchMediaSync('(orientation: landscape)'); // 当设备横屏时条件成立 
  @State isLandscape:boolean = false; 
  
  onPortrait(mediaQueryResult: mediaquery.MediaQueryResult) { 
    if (mediaQueryResult.matches) { 
      // 设置窗口布局为沉浸式布局 
      this.setSystemBar(false); 
      this.isLandscape = true; 
      this.videoHeight = '100%' 
    } else { 
      this.isLandscape = false; 
      this.videoHeight = '200vp' 
      this.setSystemBar(true); 
    } 
  } 
  
  aboutToAppear() { 
    this.listener.on('change', (mediaQueryResult: mediaquery.MediaQueryResult) => { 
      this.onPortrait(mediaQueryResult) 
    }); 
  
    try { 
      sensor.on(sensor.SensorId.ACCELEROMETER, (data: sensor.AccelerometerResponse) => { 
        let status = settings.getValueSync(context, settings.general.ACCELEROMETER_ROTATION_STATUS, "0"); 
        if (status == '0') { 
          return; 
        } 
        getCurrentOrientation(data); 
        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); 
        }); 
      }, { interval: 100000000 }); 
      setTimeout(() => { 
        sensor.off(sensor.SensorId.ACCELEROMETER); 
      }, 500); 
    } catch (error) { 
      console.error(`Failed to invoke on. Code: ${error.code}, message: ${error.message}`); 
    } 
  }
  • 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.

适配版本

DevEco Studio Version: 4.1.1.300

SDK:HarmoneyOS 4.0.0.43

分享
微博
QQ
微信
回复
2024-05-27 16:10:12


相关问题
HarmonyOS 怎么监听屏幕横竖旋转
828浏览 • 1回复 待解决
屏幕自动旋转的示例哪些
1037浏览 • 1回复 待解决
监听屏幕旋转的案例哪些
1116浏览 • 1回复 待解决
提供Webview demo哪些
846浏览 • 1回复 待解决
HarmonyOS 横竖切换
1138浏览 • 1回复 待解决
HarmonyOS 设置横竖问题
640浏览 • 1回复 待解决
HarmonyOS 监听横竖切换
965浏览 • 1回复 待解决
HarmonyOS 横竖设置问题
626浏览 • 1回复 待解决
HarmonyOS 横竖切换问题
1165浏览 • 1回复 待解决
HarmonyOS 横竖翻转卡顿
766浏览 • 1回复 待解决
HarmonyOS entry组件的横竖
480浏览 • 1回复 待解决
HarmonyOS 类似骨架的实现demo
841浏览 • 1回复 待解决
求大佬告知如何切换横竖
3067浏览 • 1回复 待解决
HarmonyOS 平板无法正确横竖切换
912浏览 • 1回复 待解决
OpenHarmony 小型系统如何设置横竖
8870浏览 • 1回复 待解决
HarmonyOS 横竖切换实现过于繁琐
1003浏览 • 1回复 待解决