在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

0
  • 聊天消息
  • 系統消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發帖/加入社區
會員中心
創作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

OpenHarmony如何切換橫豎屏?

OpenAtom OpenHarmony ? 來源:未知 ? 2023-01-18 02:25 ? 次閱讀

開源項目 OpenHarmony是每個人的 OpenHarmony 9579cde2-9692-11ed-bfe3-dac502259ad0.png

徐建國(堅果)

江蘇潤開鴻數字科技有限公司 生態技術專家

前言

在日常開發中,大多APP可能根據實際情況直接將APP的界面方向固定,或豎屏或橫屏。但在使用過程中,我們還是會遇到橫豎屏切換的功能需求,可能是通過物理重力感應觸發,也有可能是用戶手動觸發。所以本文主要帶大家了解在OpenAtom OpenHarmony(以下簡稱“OpenHarmony”)應用開發的過程中,如何在Stage模型和FA模型下使用對應的接口去完成橫豎屏的切換。 本文中OpenHarmony版本為3.2 Beta4,API版本為9。開發板為DAYU200。

FA模型

FA模型下,setDisplayOrientation和setDisplayOrientation是切換橫豎屏的接口。

文檔:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-inner-app-context.md#contextsetdisplayorientation7

context.setDisplayOrientation setDisplayOrientation(orientation: bundle.DisplayOrientation, callback: AsyncCallback): void 設置當前能力的顯示方向(callback形式)。 系統能力: SystemCapability.Ability.AbilityRuntime.Core 參數95a190e8-9692-11ed-bfe3-dac502259ad0.png ? 示例:
import featureAbility from '@ohos.ability.featureAbility';
import bundle from '@ohos.bundle';
//FA模型下獲取context
var context = featureAbility.getContext();
var orientation = bundle.DisplayOrientation.UNSPECIFIED;
context.setDisplayOrientation(orientation, (err) => {
    console.info("setDisplayOrientation err: " + JSON.stringify(err));
});
完整代碼
import bundle from '@ohos.bundle';
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = '橫豎屏切換 '
  @State portrait: boolean = true


  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
          var context = featureAbility.getContext();
          if (this.portrait) {


            // 橫屏
            var orientation = bundle.DisplayOrientation.LANDSCAPE;
            context.setDisplayOrientation(orientation, (err) => {
              this.portrait = !this.portrait
              console.info("setDisplayOrientation err: " + JSON.stringify(err));
            });
          } else {
            //豎屏
            var orientation = bundle.DisplayOrientation.PORTRAIT;
            context.setDisplayOrientation(orientation, (err) => {
              this.portrait = !this.portrait
              console.info("setDisplayOrientation err: " + JSON.stringify(err));
            });      
          }
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}
上面這樣寫太亂了,我們可以封裝一下:
import bundle from '@ohos.bundle';
import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = '橫豎屏切換 '
  @State portrait: boolean = true


  private changePortrait() {
    var context = featureAbility.getContext();
    if (this.portrait) {
      // 橫屏
      var orientation = bundle.DisplayOrientation.LANDSCAPE;
      context.setDisplayOrientation(orientation, (err) => {
        this.portrait = !this.portrait
        console.info("setDisplayOrientation err: " + JSON.stringify(err));
      });
    } else {
      //豎屏
      var orientation = bundle.DisplayOrientation.PORTRAIT;
      context.setDisplayOrientation(orientation, (err) => {
        this.portrait = !this.portrait
        console.info("setDisplayOrientation err: " + JSON.stringify(err));
      });


    }


  }


  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(30)
          .fontWeight(FontWeight.Bold).onClick(() => {
this.changePortrait()
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Stage模型

從API 9開始,可以使用setPreferredOrientation來切換橫豎屏。

文檔:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-window.md#setpreferredorientation9

在Stage模型中,使用到的主要是Window(窗口)。在設置橫豎屏切換的時候,需要先使用getLastWindow()、createWindow()、findWindow()中的任一方法獲取到Window實例,再通過此實例調用對應的方法,本文使用的是getLastWindow。 Window.getLastWindow getLastWindow(ctx: BaseContext): Promise獲取當前應用內最后顯示的窗口,使用Promise異步回調。 系統能力: SystemCapability.WindowManager.WindowManager.Core 參數:95cba716-9692-11ed-bfe3-dac502259ad0.png ? 返回值:95d47fa8-9692-11ed-bfe3-dac502259ad0.png ? 錯誤碼: 以下錯誤碼的詳細介紹請參見窗口錯誤碼。95fa15f6-9692-11ed-bfe3-dac502259ad0.png
let windowClass = null;
try {
    let promise = window.getLastWindow(this.context);
    promise.then((data)=> {
        windowClass = data;
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
    }).catch((err)=>{
        console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
    });
} catch (exception) {
    console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}
然后就可以使用setPreferredOrientation屬性。 setPreferredOrientation setPreferredOrientation(orientation: Orientation): Promise 設置窗口的顯示方向屬性,使用Promise異步回調。 系統能力: SystemCapability.WindowManager.WindowManager.Core 參數:9610352a-9692-11ed-bfe3-dac502259ad0.png ? 返回值:963309f6-9692-11ed-bfe3-dac502259ad0.png ? 錯誤碼: 以下錯誤碼的詳細介紹請參見窗口錯誤碼。96482ffc-9692-11ed-bfe3-dac502259ad0.png
let orientation = window.Orientation.AUTO_ROTATION;
try {
    let promise = windowClass.setPreferredOrientation(orientation);
    promise.then(()=> {
        console.info('Succeeded in setting the window orientation.');
    }).catch((err)=>{
        console.error('Failed to set the window orientation. Cause: ' + JSON.stringify(err));
    });
} catch (exception) {
    console.error('Failed to set window orientation. Cause: ' + JSON.stringify(exception));
}
完整代碼
importWindowfrom'@ohos.window'
import common from '@ohos.app.ability.common';
@Entry
@Component
struct ArkUIClubTest {
  private portrait: boolean = true
  build() {
    Stack() {
      Button("橫豎屏切換")
        .onClick(() => {
          this.changeOrientation()
        })
    }
    .width('100%')
    .height('100%')
  }
  private changeOrientation() {
    let windowClass = null;
    //獲取上下文
    //var context = getContext(this) as any
    // 獲取上下文,使用common模塊
     var context =   getContext(this) as common.UIAbilityContext;
    let promise = Window.getLastWindow(context);
    promise.then((data) => {
      windowClass = data;
      if (this.portrait) {
        //切換成橫屏
        let orientation = Window.Orientation.LANDSCAPE;
        windowClass.setPreferredOrientation(orientation, (err) => {
       });
        this.portrait = !this.portrait
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
      }
      else {
        //切換成豎屏
        let orientation = Window.Orientation.PORTRAIT;
        windowClass.setPreferredOrientation(orientation, (err) => {
        });
        this.portrait = !this.portrait
        console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
      }
    }).catch((err) => {
      console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
    });
  }
}

總結

本文帶大家使用對應的接口,在Stage模型和FA模型下完成了橫豎屏的切換。其中還涉及到了上下文的獲取:Stage模型用(getContext(this) as any),FA模型(featureAbility.getContext()),大家可以在此基礎上利用生命周期的回調,在合適的地方完成對應的操作。


原文標題:OpenHarmony如何切換橫豎屏?

文章出處:【微信公眾號:OpenAtom OpenHarmony】歡迎添加關注!文章轉載請注明出處。


聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。 舉報投訴
  • 鴻蒙
    +關注

    關注

    57

    文章

    2401

    瀏覽量

    43177
  • OpenHarmony
    +關注

    關注

    25

    文章

    3757

    瀏覽量

    16795

原文標題:OpenHarmony如何切換橫豎屏?

文章出處:【微信號:gh_e4f28cfa3159,微信公眾號:OpenAtom OpenHarmony】歡迎添加關注!文章轉載請注明出處。

收藏 人收藏

    評論

    相關推薦

    OpenHarmony默認30秒熄太麻煩?觸覺智能鴻蒙開發板教你輕松取消

    OpenHarmony系統開機后 30 秒會自動息,教大家兩招輕松取消自動息,觸覺智能Purple Pi OH鴻蒙開發板演示,已適配全新OpenHarmony5.0 Release
    的頭像 發表于 12-09 11:45 ?328次閱讀
    <b class='flag-5'>OpenHarmony</b>默認30秒熄<b class='flag-5'>屏</b>太麻煩?觸覺智能鴻蒙開發板教你輕松取消

    貝啟科技亮相OpenHarmony人才生態大會2024

    ,超高清多路拼接顯示,工業控制等,吸引了眾多參展者的目光。與此同時,在貝啟科技的展臺前,前來了解產品詳情、尋求合作機會的觀眾絡繹不絕。此次大會聚焦技術交流與生態發展,貝啟科技作為OpenHarmony生態的重要成員,全程參與并貢獻力量。
    的頭像 發表于 11-29 16:00 ?250次閱讀

    第三屆OpenHarmony技術大會星光璀璨、致謝OpenHarmony社區貢獻者

    10月12日,在上海舉辦的第三屆OpenHarmony技術大會上,32家高校OpenHarmony技術俱樂部璀璨亮相,30家高校OpenHarmony開發者協會盛大啟幕。還分別致謝了年度星光TSG
    的頭像 發表于 10-21 14:10 ?278次閱讀

    OpenHarmony年度技術俱樂部、個人及活動評選結果公示

    2024年度技術俱樂部評選活動已經圓滿結束。在此,OpenHarmony項目群技術指導委員會(TSC)對所有參與者的積極參與和辛勤付出表示感謝。經過嚴格的評選和審核,現將名單予以公示: 評選
    的頭像 發表于 10-05 08:07 ?336次閱讀

    基于ArkTS語言的OpenHarmony APP應用開發:HelloOpenharmony

    1、程序簡介該程序是基于OpenHarmony標準系統編寫的UI應用類:HelloOpenHarmony。本案例是基于API9接口開發。本案例已在OpenHarmony凌蒙派-RK3568開發
    的頭像 發表于 09-15 08:09 ?535次閱讀
    基于ArkTS語言的<b class='flag-5'>OpenHarmony</b> APP應用開發:Hello<b class='flag-5'>Openharmony</b>

    基于ArkTS語言的OpenHarmony APP應用開發:HelloOpenharmony

    Studio提供遠程模擬器和本地模擬器。 6.1、本地模擬器 點擊右側欄中的\"Previewer\",可以查看ArkUI運行結果。 程序運行結果如下所示: 注意:如果發現界面是橫豎不對
    發表于 09-14 12:47

    鴻蒙開發系統基礎能力:ohos.screenLock 鎖管理

    管理服務是OpenHarmony中系統服務,為鎖應用提供注冊亮、滅、開啟屏幕、結束休眠、退出動畫、請求解鎖結果監聽,并提供回調結果
    的頭像 發表于 06-27 11:41 ?924次閱讀
    鴻蒙開發系統基礎能力:ohos.screenLock 鎖<b class='flag-5'>屏</b>管理

    鴻蒙開發接口資源管理:【@ohos.resourceManager (資源管理)】

    資源管理模塊,根據當前configuration(語言,區域,橫豎,mccmnc)和device capability(設備類型,分辨率)提供獲取應用資源信息讀取接口。
    的頭像 發表于 06-03 15:10 ?1283次閱讀
    鴻蒙開發接口資源管理:【@ohos.resourceManager (資源管理)】

    求助,關于STM32F429I-EVAL評估板橫切換問題求解

    最近使用的STM32429I-EVAL評估板做個項目調研,系統4.3的顯示可以由橫切換。結果試驗了兩天也沒有解決?通過宏定義可以實現X和y鏡像。但是Xy交換不了。直接沒有顯示。
    發表于 05-10 06:00

    鴻蒙OpenHarmony【創建工程并獲取源碼】

    在通過DevEco Device Tool創建OpenHarmony工程時,可自動下載相應版本的OpenHarmony源碼。
    的頭像 發表于 04-19 21:40 ?464次閱讀
    鴻蒙<b class='flag-5'>OpenHarmony</b>【創建工程并獲取源碼】

    九聯開鴻加入開放原子開源基金會OpenHarmony醫療健康專委會

    近日,九聯開鴻加入開放原子開源基金會OpenHarmony醫療健康專委會,將與醫療行業伙伴合作開發基于OpenHarmony系統的智慧醫療產品以及智慧病房解決方案,完成包括床頭、走廊
    的頭像 發表于 04-18 09:46 ?518次閱讀
    九聯開鴻加入開放原子開源基金會<b class='flag-5'>OpenHarmony</b>醫療健康專委會

    stm32f429 emwin切換窗口閃的原因?

    我這里GUI初始化的時候全屏都初始化了,但是用的時候只用了右半邊,左半邊顯示的是攝像頭的圖像,現在是只要切換右半的頁面攝像頭這邊就會閃,不知道是什么問題。
    發表于 04-18 06:51

    OpenHarmony南向開發案例:【智能中控

    基于Hi3516開發板,使用開源OpenHarmony開發的應用。通過控制面板可以控制同一局域網內的空調,窗簾,燈等智能家居設備。
    的頭像 發表于 04-17 16:12 ?495次閱讀
    <b class='flag-5'>OpenHarmony</b>南向開發案例:【智能中控<b class='flag-5'>屏</b>】

    OpenHarmony南向能力征集令

    1、適配過程中缺少哪些接口能力或者南向能力,需要OpenHarmony去補齊的?例如內核、編譯、器件適配、單板適配等; 2、對標linux,需要OpenHarmony提供哪些能力?比如V4L2
    發表于 04-09 15:32

    OpenHarmony內核編程實戰

    編程入門[Hello,OpenHarmony]在正式開始之前,對于剛接觸OpenHarmony的伙伴們,面對大篇幅的源碼可能無從下手,不知道怎么去編碼寫程序,下面用一個簡單的例子帶伙伴們入門。▍任務
    的頭像 發表于 03-27 08:31 ?988次閱讀
    <b class='flag-5'>OpenHarmony</b>內核編程實戰
    主站蜘蛛池模板: 欧美三级网址 | 日本在线不卡免 | vip免费观看 | 1024手机最新手机在线 | 日韩欧美中文在线 | 免费看黄视频网站 | 可以看黄色的网站 | 日韩毛片大全 | 国产一级特黄a大片免费 | 国产中文99视频在线观看 | 婷婷综合激情网 | h版欧美一区二区三区四区 h网站亚洲 | 黄黄的网站在线观看 | 色综合久久一区二区三区 | 日韩加勒比在线 | 五月天婷婷丁香中文在线观看 | 亚洲黄网站wwwwww | hs网站在线观看 | 色多多拼多多网站 | 一级黄色片a | 天天综合网站 | 老师下面好湿好紧好滑好想要 | 亚洲一区在线视频观看 | 波多野结衣三个女人蕾丝边 | 精品一区二区在线观看 | 四虎黄色片| 一级片在线播放 | 欧色美| 国产综合精品久久久久成人影 | 日本福利小视频 | 亚洲婷婷六月 | 亚洲精品久久久久久婷婷 | 欧美一区二区三区免费高 | 特一级黄 | 亚洲成人网在线观看 | 黄视频网站免费 | 黄网观看 | 国产综合色精品一区二区三区 | 很黄很暴力 很污秽的小说 很黄很黄叫声床戏免费视频 | 日本黄色免费看 | 毛片资源网 |