女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

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】歡迎添加關注!文章轉載請注明出處。


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

    關注

    59

    文章

    2532

    瀏覽量

    43790
  • OpenHarmony
    +關注

    關注

    27

    文章

    3835

    瀏覽量

    18179

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

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

收藏 人收藏

    評論

    相關推薦
    熱點推薦

    泰克示波器MSO58B光標橫豎切換操作指南與實用技巧

    是提升測量效率與精度的關鍵操作之一。本文將詳細介紹MSO58B示波器的光標橫豎切換方法、應用場景及實用技巧,幫助用戶高效掌握這一核心功能。 ? 一、光標切換的基本操作步驟 泰克MSO58B的光標
    的頭像 發表于 05-26 17:08 ?123次閱讀
    泰克示波器MSO58B光標<b class='flag-5'>橫豎</b><b class='flag-5'>切換</b>操作指南與實用技巧

    OpenHarmony5.0系統怎么去除鎖直接進入界面?教你2步搞定

    本文介紹在OpenHarmony5.0Release操作系統下,去除鎖開機后直接進入界面的方法。觸覺智能PurplePiOH鴻蒙開發板演示,搭載了瑞芯微RK3566四核處理器,1TOPS算力NPU
    的頭像 發表于 03-12 18:51 ?394次閱讀
    <b class='flag-5'>OpenHarmony</b>5.0系統怎么去除鎖<b class='flag-5'>屏</b>直接進入界面?教你2步搞定

    OpenHarmony程序分析框架論文入選ICSE 2025

      近日,ICSE 2025軟件工程實踐Track放榜,面向OpenAtom OpenHarmony(以下簡稱“OpenHarmony”)的ArkTS程序分析基礎框架--方舟程序分析器(論文題目為
    的頭像 發表于 01-02 13:41 ?981次閱讀
    <b class='flag-5'>OpenHarmony</b>程序分析框架論文入選ICSE 2025

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

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

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

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

    OpenHarmony人才生態大會南向生態社區發展論壇在武漢圓滿舉辦

    專家介紹OpenHarmony在社區開發者手機、Watch和大生態共建進展。Laval社區開發者手機共建項目自2023年啟動,隨OpenHarmony版本演進增強,承載行業5G解決方案的孵化,助力硬開
    發表于 11-29 09:54

    OpenHamrony4.0去除鎖是一種什么體驗?觸覺智能給你支支招

    本文介紹開源鴻蒙OpenHarmony 4.0系統下,去除鎖開機后直接進入界面的方法,觸覺智能Purple Pi OH鴻蒙開發板演示,已適配全新OpenHarmony5.0 Release系統!
    的頭像 發表于 11-13 10:37 ?586次閱讀
    OpenHamrony4.0去除鎖<b class='flag-5'>屏</b>是一種什么體驗?觸覺智能給你支支招

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

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

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

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

    【龍芯2K0300蜂鳥板試用】OpenHarmony代碼

    收到龍芯2K0300蜂鳥開發板后,對開發板做了一些了解和研究,現將OpenHarmony代碼提供給大家測試,也希望大家能更多的認識龍芯2K0300蜂鳥開發板,下面先簡單介紹一下這塊開發板。 廣東
    發表于 09-18 11:42

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

    1、程序簡介該程序是基于OpenHarmony標準系統編寫的UI應用類:HelloOpenHarmony。本案例是基于API9接口開發。本案例已在OpenHarmony凌蒙派-RK3568開發
    的頭像 發表于 09-15 08:09 ?768次閱讀
    基于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

    河南大學OpenHarmony技術俱樂部正式揭牌成立

    8月30日,由OpenAtom OpenHarmony(以下簡稱“OpenHarmony”)項目群技術指導委員會與河南大學共同舉辦的“河南大學OpenHarmony技術俱樂部成立大會”在鄭州校區友蘭
    的頭像 發表于 09-03 16:12 ?674次閱讀
    河南大學<b class='flag-5'>OpenHarmony</b>技術俱樂部正式揭牌成立

    電源切換芯片怎么設置

    電源切換芯片是一種用于實現電源切換功能的電子元件,廣泛應用于各種電子設備中,如計算機、手機、電源適配器等。本文將介紹電源切換芯片的設置方法,包括工作原理、分類、選擇、設計、測試和應用等方面。 電源
    的頭像 發表于 07-15 10:57 ?1972次閱讀

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

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