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

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

3天內不再提示

鴻蒙OS開發:【一次開發,多端部署】(一多天氣)項目

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-05-20 14:59 ? 次閱讀

一多天氣

介紹

本示例展示一個天氣應用界面,包括首頁、城市管理、添加城市、更新時間彈窗,體現一次開發,多端部署的能力。

1.本示例參考一次開發,多端部署的指導,主要使用響應式布局的柵格斷點系統實現在不同尺寸窗口界面上不同的顯示效果。

2.使用[SideBarContainer]實現側邊欄功能。

3.使用[柵格容器組件]實現界面內容的分割和展示。

4.使用Canvas和CanvasRenderingContext2D完成空氣質量和日出月落圖的曲線繪制。

開發前請熟悉鴻蒙開發指導文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

效果預覽

image.png

使用說明:

1.啟動應用后,首頁展示已添加城市的天氣信息,默認展示2個城市,左右滑動可以切換城市,在LG設備上,默認顯示側邊欄,側邊欄顯示時,右側內容區占2/3,側邊欄隱藏時,內容區自動鋪滿界面。

2.在支持窗口自由拖拽的設備上,拖拽窗口大小,可以分別實現拖動到最大窗口側邊欄顯示(點擊側邊欄控制按鈕可以隱藏和顯示側邊欄),拖動窗口縮小到MD大小時側邊欄和側邊欄控制按鈕隱藏。

3.在支持窗口自由拖拽的設備上,拖拽窗口大小,天氣內容區跟隨窗口大小會自動換行顯示。

4.點擊右上角菜單按鈕,在菜單中點擊 更新時間 ,彈出更新時間彈窗,沒有功能,此處只做展示,在平板設備上顯示2列,在小屏設備上顯示一列。

5.點擊右上角菜單按鈕,在菜單中點擊 管理城市 ,進入管理城市界面,展示已添加的城市,在平板設備上顯示2列,在小屏設備上顯示一列。

6.點擊管理城市界面的 添加城市 ,進入添加城市界面,已添加的城市不可點擊,未添加的城市點擊可以添加并返回管理城市界面顯示。

工程目錄

/code/SuperFeature/MultiDeviceAppDev/Weather/product/default
└─src
    ├─main
    │  │
    │  ├─ets
    │  │  ├─Application
    │  │  │      MyAbilityStage.ts          //自定義ability
    │  │  │
    │  │  ├─common                          //公共資源庫
    │  │  ├─feature
    │  │  │      AirQualityFeature.ts       //空氣繪畫
    │  │  │      SunCanvasFeature.ts        //晴天繪畫
    │  │  │
    │  │  ├─MainAbility
    │  │  │      MainAbility.ts             //主窗口
    │  │  │
    │  │  └─pages
    │  │      │  AddCity.ets                //添加城市
    │  │      │  CityList.ets               //城市列表
    │  │      │  Home.ets                   //入口
    │  │      │
    │  │      └─home
    │  │              AirQuality.ets         //空氣質量
    │  │              HomeContent.ets        //主頁面
    │  │              HoursWeather.ets       //每小時天氣組件
    │  │              IndexEnd.ets           //首頁尾 
    │  │              IndexHeader.ets        //首頁頭
    │  │              IndexTitleBar.ets      //首頁標題
    │  │              LifeIndex.ets          //生活建議
    │  │              MultidayWeather.ets    //天氣組件
    │  │              SideContent.ets        //側邊欄
    │  │              SunCanvas.ets          //晴天樣式
    │  │              UpdateTimeDialog.ets   //時間更新彈窗
    │  │
    │  └─resources                           //資源包

具體實現

1、home.ets中引入SideContent()和homeContent()。
2、定義showSideBar來判斷是否展示側邊欄,定義mediaquery.MediaQueryListener媒體監聽器smListener、mdListener、lgListener。
3、在aboutToAppear調用mediaquery對界面進行監聽,[源碼參考]。

/*

 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import mediaquery from '@ohos.mediaquery';

import HomeContent from './home/HomeContent';

import IndexTitleBar from './home/IndexTitleBar';

import SideContent from './home/SideContent';

import { CityListData, Style, getBg, getCityListWeatherData, Logger } from '@ohos/common';



const TAG: string = 'Home';



@Entry

@Component

struct Home {

  @StorageLink('isRefresh') @Watch('refreshChange') isRefresh: boolean = false;

  @StorageLink('swiperIndex') swiperIndex: number = 0;

  @State curBp: string = 'md';

  @State cityListWeatherData: CityListData[] = getCityListWeatherData();

  @State popupState: boolean = false;

  @State showSideBar: boolean = false;

  private smListener: mediaquery.MediaQueryListener;

  private mdListener: mediaquery.MediaQueryListener;

  private lgListener: mediaquery.MediaQueryListener;



  build() {

    SideBarContainer(SideBarContainerType.Embed) {

      SideContent({ showSideBar: $showSideBar })

        .height('100%')

      Column() {

        IndexTitleBar({ showSideBar: $showSideBar })

          .height(56)

        Swiper() {

          ForEach(this.cityListWeatherData, (item, index) = > {

            HomeContent({ showSideBar: this.showSideBar, cityListData: item, index: index })

          }, item = > item.city)

        }

        .id('swiper')

        .padding({ left: Style.NORMAL_PADDING, right: Style.NORMAL_PADDING })

        .indicatorStyle({

          selectedColor: Color.White

        })

        .onChange(index = > {

          this.swiperIndex = index;

          AppStorage.SetOrCreate('swiperIndex', this.swiperIndex);

        })

        .indicator(this.curBp !== 'lg')

        .index(this.swiperIndex)

        .loop(false)

        .width('100%')

        .layoutWeight(1)

      }

      .height('100%')

    }

    .height('100%')

    .sideBarWidth('33.3%')

    .minSideBarWidth('33.3%')

    .maxSideBarWidth('33.3%')

    .showControlButton(false)

    .showSideBar(this.showSideBar)

    .backgroundImageSize(ImageSize.Cover)

    .backgroundImage(getBg(this.cityListWeatherData[this.swiperIndex].header.weatherType))

  }



  aboutToAppear() {

    this.smListener = mediaquery.matchMediaSync('(320vp< width<=600vp)');

    this.smListener.on("change", this.isBreakpointSM);

    this.mdListener = mediaquery.matchMediaSync('(600vp< width<=840vp)');

    this.mdListener.on("change", this.isBreakpointMD);

    this.lgListener = mediaquery.matchMediaSync('(840vp< width)');

    this.lgListener.on("change", this.isBreakpointLG);

  }



  aboutToDisappear() {

    this.smListener.off("change", this.isBreakpointSM);

    this.mdListener.off("change", this.isBreakpointMD);

    this.lgListener.off("change", this.isBreakpointLG);

  }



  isBreakpointSM = (mediaQueryResult) = > {

    if (mediaQueryResult.matches) {

      this.curBp = 'sm';

      this.showSideBar = false;

      AppStorage.SetOrCreate('curBp', this.curBp);

    }

    Logger.info(TAG, `this.curBp = ${this.curBp}`);

  }

  isBreakpointMD = (mediaQueryResult) = > {

    if (mediaQueryResult.matches) {

      this.curBp = 'md';

      this.showSideBar = false;

      AppStorage.SetOrCreate('curBp', this.curBp);

    }

    Logger.info(TAG, `this.curBp = ${this.curBp}`);

  }

  isBreakpointLG = (mediaQueryResult) = > {

    if (mediaQueryResult.matches) {

      if (this.curBp !== 'lg') {

        this.showSideBar = true;

      }

      this.curBp = 'lg';

      AppStorage.SetOrCreate('curBp', this.curBp);

    }

    Logger.info(TAG, `this.curBp = ${this.curBp}`);

  }



  refreshChange() {

    Logger.info(TAG, `refreshChange}`);

    if (this.isRefresh) {

      this.cityListWeatherData = getCityListWeatherData();

      AppStorage.SetOrCreate('isRefresh', false);

    }

    Logger.info(TAG, `refreshChange, this.cityListWeatherData.length = ${this.cityListWeatherData.length}`);

  }

}

`HarmonyOSOpenHarmony鴻蒙文檔籽料:mau123789是v直接拿`

搜狗高速瀏覽器截圖20240326151450.png

4、監聽到當前屏幕大小,調用this.isBreakpoint斷點,對curBp、showSideBar進行賦值,[源碼參考]。

/*

 * Copyright (c) 2022-2023 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */



import mediaquery from '@ohos.mediaquery';

import HomeContent from './home/HomeContent';

import IndexTitleBar from './home/IndexTitleBar';

import SideContent from './home/SideContent';

import { CityListData, Style, getBg, getCityListWeatherData, Logger } from '@ohos/common';



const TAG: string = 'Home';



@Entry

@Component

struct Home {

  @StorageLink('isRefresh') @Watch('refreshChange') isRefresh: boolean = false;

  @StorageLink('swiperIndex') swiperIndex: number = 0;

  @State curBp: string = 'md';

  @State cityListWeatherData: CityListData[] = getCityListWeatherData();

  @State popupState: boolean = false;

  @State showSideBar: boolean = false;

  private smListener: mediaquery.MediaQueryListener;

  private mdListener: mediaquery.MediaQueryListener;

  private lgListener: mediaquery.MediaQueryListener;



  build() {

    SideBarContainer(SideBarContainerType.Embed) {

      SideContent({ showSideBar: $showSideBar })

        .height('100%')

      Column() {

        IndexTitleBar({ showSideBar: $showSideBar })

          .height(56)

        Swiper() {

          ForEach(this.cityListWeatherData, (item, index) = > {

            HomeContent({ showSideBar: this.showSideBar, cityListData: item, index: index })

          }, item = > item.city)

        }

        .id('swiper')

        .padding({ left: Style.NORMAL_PADDING, right: Style.NORMAL_PADDING })

        .indicatorStyle({

          selectedColor: Color.White

        })

        .onChange(index = > {

          this.swiperIndex = index;

          AppStorage.SetOrCreate('swiperIndex', this.swiperIndex);

        })

        .indicator(this.curBp !== 'lg')

        .index(this.swiperIndex)

        .loop(false)

        .width('100%')

        .layoutWeight(1)

      }

      .height('100%')

    }

    .height('100%')

    .sideBarWidth('33.3%')

    .minSideBarWidth('33.3%')

    .maxSideBarWidth('33.3%')

    .showControlButton(false)

    .showSideBar(this.showSideBar)

    .backgroundImageSize(ImageSize.Cover)

    .backgroundImage(getBg(this.cityListWeatherData[this.swiperIndex].header.weatherType))

  }



  aboutToAppear() {

    this.smListener = mediaquery.matchMediaSync('(320vp< width<=600vp)');

    this.smListener.on("change", this.isBreakpointSM);

    this.mdListener = mediaquery.matchMediaSync('(600vp< width<=840vp)');

    this.mdListener.on("change", this.isBreakpointMD);

    this.lgListener = mediaquery.matchMediaSync('(840vp< width)');

    this.lgListener.on("change", this.isBreakpointLG);

  }



  aboutToDisappear() {

    this.smListener.off("change", this.isBreakpointSM);

    this.mdListener.off("change", this.isBreakpointMD);

    this.lgListener.off("change", this.isBreakpointLG);

  }



  isBreakpointSM = (mediaQueryResult) = > {

    if (mediaQueryResult.matches) {

      this.curBp = 'sm';

      this.showSideBar = false;

      AppStorage.SetOrCreate('curBp', this.curBp);

    }

    Logger.info(TAG, `this.curBp = ${this.curBp}`);

  }

  isBreakpointMD = (mediaQueryResult) = > {

    if (mediaQueryResult.matches) {

      this.curBp = 'md';

      this.showSideBar = false;

      AppStorage.SetOrCreate('curBp', this.curBp);

    }

    Logger.info(TAG, `this.curBp = ${this.curBp}`);

  }

  isBreakpointLG = (mediaQueryResult) = > {

    if (mediaQueryResult.matches) {

      if (this.curBp !== 'lg') {

        this.showSideBar = true;

      }

      this.curBp = 'lg';

      AppStorage.SetOrCreate('curBp', this.curBp);

    }

    Logger.info(TAG, `this.curBp = ${this.curBp}`);

  }



  refreshChange() {

    Logger.info(TAG, `refreshChange}`);

    if (this.isRefresh) {

      this.cityListWeatherData = getCityListWeatherData();

      AppStorage.SetOrCreate('isRefresh', false);

    }

    Logger.info(TAG, `refreshChange, this.cityListWeatherData.length = ${this.cityListWeatherData.length}`);

  }

}

審核編輯 黃宇

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

    關注

    0

    文章

    12

    瀏覽量

    11220
  • 鴻蒙
    +關注

    關注

    55

    文章

    1930

    瀏覽量

    42203
  • 鴻蒙OS
    +關注

    關注

    0

    文章

    186

    瀏覽量

    4304
收藏 人收藏

    評論

    相關推薦

    ?HarmonyOS"一次開發,多端部署"優秀實踐——玩機技巧

    的潛在用戶群體。一個應用要在多類設備上提供統一的內容,需要適配不同的屏幕尺寸和硬件,開發成本較高。"一次開發,多端部署"(后文中簡稱為"
    的頭像 發表于 08-30 10:25 ?1736次閱讀
    ?HarmonyOS"<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>"優秀實踐——玩機技巧

    HarmonyOS開發案例:【一次開發,多端部署(視頻應用)】

    者提供了“一次開發,多端部署”的系統能力,讓開發者可以基于一次
    的頭像 發表于 05-11 15:41 ?721次閱讀
    HarmonyOS<b class='flag-5'>開發</b>案例:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>(視頻應用)】

    HarmonyOS開發案例:【一次開發,多端部署-音樂專輯】

    基于自適應和響應式布局,實現一次開發、多端部署音樂專輯頁面。
    的頭像 發表于 05-13 16:48 ?427次閱讀
    HarmonyOS<b class='flag-5'>開發</b>案例:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>-音樂專輯】

    鴻蒙OS開發:【一次開發,多端部署】(天氣應用)案例

    本章通過一個天氣應用,介紹一多應用的整體開發過程,包括UX設計、工程管理及調試、頁面開發等。
    的頭像 發表于 05-15 15:42 ?450次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】(<b class='flag-5'>天氣</b>應用)案例

    鴻蒙OS開發:【一次開發,多端部署】(音樂專輯主頁)

    本示例使用一次開發多端部署中介紹的自適應布局能力和響應式布局能力進行多設備(或多窗口尺寸)適配,保證應用在不同設備或不同窗口尺寸下可以正常顯示。
    的頭像 發表于 05-21 14:48 ?382次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】(音樂專輯主頁)

    鴻蒙OS開發:【一次開發,多端部署】(音樂專輯頁面)

    基于自適應和響應式布局,實現一次開發、多端部署音樂專輯頁面。
    的頭像 發表于 05-25 16:21 ?221次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】(音樂專輯頁面)

    鴻蒙OS開發:【一次開發,多端部署】(視頻應用)

    者提供了“一次開發,多端部署”的系統能力,讓開發者可以基于一次
    的頭像 發表于 05-25 16:29 ?221次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】(視頻應用)

    鴻蒙OS開發:典型頁面場景【一次開發,多端部署】實戰(音樂專輯頁2)

    本示例使用[一次開發多端部署]中介紹的自適應布局能力和響應式布局能力進行多設備(或多窗口尺寸)適配,保證應用在不同設備或不同窗口尺寸下可以正常顯示。
    的頭像 發表于 05-25 16:47 ?1630次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:典型頁面場景【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】實戰(音樂專輯頁2)

    鴻蒙OS開發:典型頁面場景【一次開發,多端部署】實戰(設置典型頁面)

    本示例展示了設置應用的典型頁面,其在小窗口和大窗口有不同的顯示效果,體現一次開發、多端部署的能力。
    的頭像 發表于 05-27 09:36 ?497次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:典型頁面場景【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】實戰(設置典型頁面)

    Happ開發后,如何實現多端部署?

    做出來后,怎么燒錄到它上面?說好的“一次開發,多端部署”2、DevEco Studio 開發出來的Happ,它能不能安裝到 android的
    發表于 09-10 21:42

    HarmonyOS\"一次開發,多端部署\"優秀實踐——玩機技巧,碼上起航

    的潛在用戶群體。個應用要在多類設備上提供統的內容,需要適配不同的屏幕尺寸和硬件,開發成本較高。\"一次開發,
    發表于 08-30 18:14

    華為開發者大會2021:一次開發 多端部署

    一次開發 多端部署使能開發者從單設備生態跨入多設備生態!
    的頭像 發表于 10-22 15:09 ?1435次閱讀
    華為<b class='flag-5'>開發</b>者大會2021:<b class='flag-5'>一次</b><b class='flag-5'>開發</b> <b class='flag-5'>多端</b><b class='flag-5'>部署</b>

    華為開發者大會2021:軟件部總裁龔體 鴻蒙系統 一次開發 多端部署 萬物互連

    華為開發者大會2021:鴻蒙系統 一次開發 多端部署 萬物互連 在華為
    的頭像 發表于 10-22 15:09 ?4150次閱讀
    華為<b class='flag-5'>開發</b>者大會2021:軟件部總裁龔體 <b class='flag-5'>鴻蒙</b>系統 <b class='flag-5'>一次</b><b class='flag-5'>開發</b> <b class='flag-5'>多端</b><b class='flag-5'>部署</b> 萬物互連

    鴻蒙OS開發:【一次開發,多端部署】(多設備自適應能力)簡單介紹

    本示例是《一次開發,多端部署》的配套示例代碼,展示了[頁面開發一多能力],包括自適應布局、響應
    的頭像 發表于 05-21 14:59 ?181次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】(多設備自適應能力)簡單介紹

    鴻蒙OS開發:【一次開發,多端部署】( 設置app頁面)

    本示例展示了設置應用的典型頁面,其在小窗口和大窗口有不同的顯示效果,體現一次開發、多端部署的能力。
    的頭像 發表于 05-21 14:56 ?185次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>OS</b><b class='flag-5'>開發</b>:【<b class='flag-5'>一次</b><b class='flag-5'>開發</b>,<b class='flag-5'>多端</b><b class='flag-5'>部署</b>】( 設置app頁面)
    亚洲欧美日韩精品久久_久久精品AⅤ无码中文_日本中文字幕有码在线播放_亚洲视频高清不卡在线观看
    <acronym id="s8ci2"><small id="s8ci2"></small></acronym>
    <rt id="s8ci2"></rt><rt id="s8ci2"><optgroup id="s8ci2"></optgroup></rt>
    <acronym id="s8ci2"></acronym>
    <acronym id="s8ci2"><center id="s8ci2"></center></acronym>