鸿蒙Next开发日记 Day03 - 屏幕翻转与全屏化

Screenshot_2024-08-06T122646.png

Screenshot_2024-08-06T122626.png


程序开发中,会经常用到屏幕的横竖屏切换,比如在全屏播放视频的时候。

下面是一个切换的工具方法。

/**
 * 切换窗口显示模式
 * @param context 上下文
 * @param windowMode 显示模式 0竖屏普通模式 1竖屏沉浸式 2横屏沉浸式
 */
const changeOrientation = (context: Context, windowMode: number) => {
  // 调用该接口手动改变设备横竖屏状态(设置全屏模式,先强制横屏,再加上传感器模式)
  window.getLastWindow(context).then((lastWindow) => {
    if (windowMode === 2) {
      // 设置窗口的布局是否为沉浸式布局
      lastWindow.setWindowLayoutFullScreen(true, () => {
        // 设置窗口全屏模式时导航栏、状态栏的可见模式
        lastWindow.setWindowSystemBarEnable([]);
        // 设置窗口的显示方向属性,AUTO_ROTATION_LANDSCAPE表示传感器自动横向旋转模式
        lastWindow.setPreferredOrientation(window.Orientation.AUTO_ROTATION_LANDSCAPE);
      });
    } else if(windowMode === 1) {
      // 设置窗口的布局是否为沉浸式布局
      lastWindow.setWindowLayoutFullScreen(true, () => {
        // 设置窗口全屏模式时导航栏、状态栏的可见模式
        lastWindow.setWindowSystemBarEnable([]);
        // 设置窗口的显示方向属性,AUTO_ROTATION_LANDSCAPE表示传感器自动横向旋转模式
        lastWindow.setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT);
      });
    } else {
      // 设置窗口的布局是否为沉浸式布局
      lastWindow.setWindowLayoutFullScreen(false, () => {
        // 设置窗口全屏模式时导航栏、状态栏的可见模式
        lastWindow.setWindowSystemBarEnable(["status", "navigation"]);
        // 设置窗口的显示方向属性,AUTO_ROTATION_LANDSCAPE表示传感器自动横向旋转模式
        lastWindow.setPreferredOrientation(window.Orientation.AUTO_ROTATION_PORTRAIT);
      });
    }
  });
}


关键词:鸿蒙NEXT

网友留言(0 条)

发表评论

验证码