From: 罗正yun <1732036298@qq.com> Date: Tue, 16 Jan 2024 10:46:07 +0000 (+0800) Subject: 主界面添加天气视图 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=71292a820e0e88d0dded253e2a4d657694d1100b;p=mirrors%2FFD230.git 主界面添加天气视图 --- diff --git a/code/sdk/application/wearable/nativeapp/CMakeLists.txt b/code/sdk/application/wearable/nativeapp/CMakeLists.txt index c1a97cafe..4926a50c5 100755 --- a/code/sdk/application/wearable/nativeapp/CMakeLists.txt +++ b/code/sdk/application/wearable/nativeapp/CMakeLists.txt @@ -354,6 +354,8 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/FutureWeatherView.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/GetWeatherDataErrorPresenter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/GetWeatherDataErrorView.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/WeatherControlView.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/WeatherView.cpp #倒计时 ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/countdown/src/MainCountDownPresenter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/countdown/src/MainCountDownView.cpp diff --git a/code/sdk/application/wearable/nativeapp/nativeui/main/include/MainViewSample.h b/code/sdk/application/wearable/nativeapp/nativeui/main/include/MainViewSample.h index 24c3f76b1..cf7264f9b 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/main/include/MainViewSample.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/main/include/MainViewSample.h @@ -41,6 +41,7 @@ #include "dial/include/Digital_clockView.h" #include "blood/include/BloodView.h" #include "traing/include/TraingView.h" +#include "weather/include/WeatherControlView.h" namespace OHOS { class MainPresenterSample; @@ -61,6 +62,7 @@ namespace OHOS UICardPage *InitMusicPlayerPage(void); UICardPage *InitNewDialPage(void); UICardPage *InitBloodOxygenPage(void); + UICardPage *InitWeatherPage(void); UICardPage *InitTrainingPage(void); void AnimatorStart(int pageIndex); @@ -96,6 +98,7 @@ namespace OHOS HeartRateView *heartRateView{nullptr}; Digital_clockView *dialView{nullptr}; BloodView *bloodView{nullptr}; + WeatherControlView *weatherControlView{nullptr}; TraingView *traingView{nullptr}; /* -------------------------------------------------------------------------- */ diff --git a/code/sdk/application/wearable/nativeapp/nativeui/main/src/MainViewSample.cpp b/code/sdk/application/wearable/nativeapp/nativeui/main/src/MainViewSample.cpp index 26991d97b..ad1bcce91 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/main/src/MainViewSample.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/main/src/MainViewSample.cpp @@ -41,6 +41,7 @@ namespace OHOS {COMPASS, &MainViewSample::InitCompassPage}, {BloodOxygen, &MainViewSample::InitBloodOxygenPage}, {TRAINING, &MainViewSample::InitTrainingPage}, + {WEATHER,&MainViewSample::InitWeatherPage}, }; MainViewSample::MainViewSample() @@ -125,6 +126,11 @@ namespace OHOS delete bloodView; bloodView = nullptr; } + if(weatherControlView != nullptr) + { + delete weatherControlView; + weatherControlView = nullptr; + } g_pMainView = nullptr; } @@ -374,6 +380,17 @@ namespace OHOS } return traingView; } + //主界面天气卡片 + UICardPage *MainViewSample::InitWeatherPage(void) + { + weatherControlView = new WeatherControlView(); + if(weatherControlView == nullptr) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mainweatherView new fail"); + return nullptr; + } + return weatherControlView; + } /* -------------------------------------------------------------------------- */ UICardPage *MainViewSample::CreatePage(int32_t StreamId) { diff --git a/code/sdk/application/wearable/nativeapp/nativeui/screensetting/include/ScreenModels.h b/code/sdk/application/wearable/nativeapp/nativeui/screensetting/include/ScreenModels.h index 51f53926e..a4690990c 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/screensetting/include/ScreenModels.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/screensetting/include/ScreenModels.h @@ -24,6 +24,7 @@ namespace OHOS TRAINING, COMPASS, BloodOxygen, + WEATHER, MAX_CARD, } CardId; diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h index b7da48150..37bd30567 100755 --- a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h @@ -9,27 +9,9 @@ #include "components/ui_label.h" #include "components/ui_image_view.h" #include "components/ui_image_animator.h" +#include "weather/include/WeatherView.h" #include -namespace OHOS -{ -#define weather_src "/user/res/weather/" - static const char *background = weather_src "WEATHER_BACKGROUND.bin"; - static const char *weather130x130[] = - { - weather_src "WEATHER_CLEAR-CLOUDY.bin", - weather_src "WEATHER_CLEAR-SUNNY.bin", - weather_src "WEATHER_DUSTSTORM.bin", - weather_src "WEATHER_FOGGY.bin", - weather_src "WEATHER_HAZE.bin", - weather_src "WEATHER_OVERCAST.bin", - weather_src "WEATHER_RAIN.bin", - weather_src "WEATHER_SNOW.bin", - weather_src "WEATHER_SUNNY.bin", - weather_src "WEATHER_SUNNY-CLOUNDY.bin", - weather_src "WEATHER_THUNDERSHOWER.bin", - weather_src "WEATHER_UNKNOWN.bin"}; -} namespace OHOS { diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherControlView.h b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherControlView.h new file mode 100644 index 000000000..488253a81 --- /dev/null +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherControlView.h @@ -0,0 +1,26 @@ +#ifndef WEATHER_CONTROL_VIEW_H +#define WEATHER_CONTROL_VIEW_H +/*该视图主要放在主视图*/ + +#include "components/root_view.h" +#include "components/ui_card_page.h" +#include "wearable_log.h" +#include "weather/include/WeatherView.h" +namespace OHOS +{ + class WeatherControlView : public UICardPage + { + public: + WeatherControlView(); + virtual ~WeatherControlView(); + bool InitView(void); + void PreLoad(void) override; + void UnLoad(void) override; + + private: + void ClearAll(void); + WeatherView *weatherView{nullptr}; + bool viewiInitStatus{false}; + }; +} +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherView.h b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherView.h new file mode 100644 index 000000000..24b8e64d4 --- /dev/null +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherView.h @@ -0,0 +1,79 @@ +#ifndef WEATHER_VIEW_H +#define WEATHER_VIEW_H + +#include "BaseView.h" +#include "View.h" + +#include "components/root_view.h" +#include "components/ui_view_group.h" +#include "components/ui_scroll_view.h" +#include "components/ui_label.h" +#include "components/ui_image_view.h" +#include "components/ui_image_animator.h" +#include "wearable_log.h" +#include "AppGroupView.h" +#include + +namespace OHOS +{ +#define weather_src "/user/res/weather/" + static const char *background = weather_src "WEATHER_BACKGROUND.bin"; + static const char *weather130x130[] = + { + weather_src "WEATHER_CLEAR-CLOUDY.bin", + weather_src "WEATHER_CLEAR-SUNNY.bin", + weather_src "WEATHER_DUSTSTORM.bin", + weather_src "WEATHER_FOGGY.bin", + weather_src "WEATHER_HAZE.bin", + weather_src "WEATHER_OVERCAST.bin", + weather_src "WEATHER_RAIN.bin", + weather_src "WEATHER_SNOW.bin", + weather_src "WEATHER_SUNNY.bin", + weather_src "WEATHER_SUNNY-CLOUNDY.bin", + weather_src "WEATHER_THUNDERSHOWER.bin", + weather_src "WEATHER_UNKNOWN.bin"}; +} + +namespace OHOS +{ + class WeatherView : public UIViewGroup + { + + public: + WeatherView(); + virtual ~WeatherView(); + static WeatherView *GetInstance(void); + bool InitPage(void); + bool InitView(void); + void ClearAll(); + bool GetInitViewState(void); + + private: + UILabel *InitLabel(int x, int y, const char *text, const char *id, int textfont, int lineheight); + + private: + std::string GetTime(); + std::string GetWeatherImage(); + std::string GetWeatherTemperature(); + std::string GetWeatherDescription(); + std::string GetWeatherArea(); + std::string GetWeatherMaxAndMinTemperature(); + + std::string Time; // 时间 + std::string WeatherImag; // 天气图片 + std::string Temperature; // 温度 + std::string WeatherDescription; // 天气描述 + std::string WeatherArea; // 天气地区 + std::string MaxandMinTemperature; // 最高与最低气温 + private: + UIImageView *showWeatherImage = nullptr; // 显示天气图片 + UILabel *showCurrentTimeLabel{nullptr}; // 显示当前时间 + UILabel *showTemperatureLabel{nullptr}; // 显示天气温度 + UILabel *showWeatherDescriptionLabel{nullptr}; // 显示天气描述 + UILabel *showWeatherAreaLabel{nullptr}; // 显示天气地区 + UILabel *showMaxandMinTemperatureLabel{nullptr}; // 显示最高与最低气温 + bool viewInitStatus{false}; + }; +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherControlView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherControlView.cpp new file mode 100644 index 000000000..43affebe3 --- /dev/null +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherControlView.cpp @@ -0,0 +1,63 @@ +#include "weather/include/WeatherControlView.h" + +namespace OHOS +{ + WeatherControlView::WeatherControlView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherControlView::WeatherControlView()"); + SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION); + } + + WeatherControlView::~WeatherControlView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherControlView::~WeatherControlView()"); + ClearAll(); + } + + void WeatherControlView::ClearAll(void) + { + if (viewiInitStatus == true) + { + RemoveAll(); + if (weatherView != nullptr) + { + delete weatherView; + weatherView = nullptr; + } + } + } + + bool WeatherControlView::InitView(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherControlView::InitView()"); + weatherView = new WeatherView(); + if (weatherView == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new mainweatherView fail!"); + return false; + } + if (weatherView->InitPage() == false) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView->InitPage() == false!"); + return false; + } + Add(weatherView); + viewiInitStatus = true; + return true; + } + + void WeatherControlView::PreLoad(void) + { + if (!viewiInitStatus) + { + InitView(); + viewiInitStatus = true; + } + } + + void WeatherControlView::UnLoad(void) + { + // Weather's usage scenes is complicated, so we do not release it's resources inner slice. + } + +} \ No newline at end of file diff --git a/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp new file mode 100644 index 000000000..b281f2863 --- /dev/null +++ b/code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp @@ -0,0 +1,234 @@ +#include "weather/include/WeatherView.h" +#include "UiConfig.h" + +/* +当至天气界面时,主动获取最新天气数据。 +(此操作静默将进行,获取到则立刻更新显示,获取不到则重试 +重试3次无果结束更新) +在加载该视图时,需要加载:1时间,2天气图片,3温度,4天气描述,5,地点,6,温度范围 +*/ + +namespace OHOS +{ + static WeatherView *g_pWeatherView{nullptr}; + WeatherView::WeatherView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::WeatherView()"); + SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION); + Time = GetTime(); + WeatherImag = GetWeatherImage(); + Temperature = GetWeatherTemperature(); + WeatherDescription = GetWeatherDescription(); + WeatherArea = GetWeatherArea(); + MaxandMinTemperature = GetWeatherMaxAndMinTemperature(); + g_pWeatherView = this; + } + + WeatherView::~WeatherView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, " WeatherView::~WeatherView()"); + ClearAll(); + g_pWeatherView = nullptr; + } + + void WeatherView::ClearAll() + { + if (viewInitStatus == true) + { + RemoveAll(); + if (showCurrentTimeLabel != nullptr) + { + delete showCurrentTimeLabel; + showCurrentTimeLabel = nullptr; + } + if (showTemperatureLabel != nullptr) + { + delete showTemperatureLabel; + showTemperatureLabel = nullptr; + } + if (showWeatherImage != nullptr) + { + delete showWeatherImage; + showWeatherImage = nullptr; + } + if (showWeatherDescriptionLabel != nullptr) + { + delete showWeatherDescriptionLabel; + showWeatherDescriptionLabel = nullptr; + } + if (showWeatherAreaLabel != nullptr) + { + delete showWeatherAreaLabel; + showWeatherAreaLabel = nullptr; + } + if (showMaxandMinTemperatureLabel != nullptr) + { + delete showMaxandMinTemperatureLabel; + showMaxandMinTemperatureLabel = nullptr; + } + } + } + + WeatherView *WeatherView::GetInstance() + { + return g_pWeatherView; + } + + // 获取时间 + std::string WeatherView::GetTime() + { + srand(time(0)); + std::string area[] = {"23:54", "12:00", "7:25"}; + int num = rand() % (sizeof(area) / sizeof(area[0])); + return area[num]; + } + + // 获取天气图片 + std::string WeatherView::GetWeatherImage() + { + int size = sizeof(weather130x130) / sizeof(weather130x130[0]); + return weather130x130[rand() % size]; + } + // 获取温度 + + std::string WeatherView::GetWeatherTemperature() + { + // 使用系统时间作为种子 + srand(time(0)); + int temperature = rand() % 50; + return std::to_string(temperature) + "°"; + } + // 获取天气描述 + + std::string WeatherView::GetWeatherDescription() + { + srand(time(0)); + std::string area[] = {"晴", "雨夹雪", "雷阵雨伴有冰雹"}; + int num = rand() % (sizeof(area) / sizeof(area[0])); + return area[num]; + } + // 获取天气地区 + + std::string WeatherView::GetWeatherArea() + { + srand(time(0)); + std::string area[] = {"北京市东城区", "深圳市福田区"}; + int num = rand() % (sizeof(area) / sizeof(area[0])); + return area[num]; + } + // 获取最高气温和最低气温 + + std::string WeatherView::GetWeatherMaxAndMinTemperature() + { + // 使用系统时间作为种子 + srand(time(0)); + // 生成随机数 + int maxtemperature = 20 + rand() % 40; // 最高气温:20°~60° + int mintemperature = -5 + rand() % 20; + std::string MaxandMinTemperature = "最高" + std::to_string(maxtemperature) + "°" + " " + "最低" + std::to_string(mintemperature) + "°"; + return MaxandMinTemperature; + } + + // 初始化标签 + UILabel *WeatherView::InitLabel(int x, int y, const char *text, const char *id, int textfont, int lineheight) + { + UILabel *label = new UILabel(); + if (label == nullptr) + { + return nullptr; + } + label->SetPosition(x, y); + label->Resize(200, 30); + label->SetViewId(id); + label->SetStyle(STYLE_TEXT_FONT, textfont); + label->SetStyle(STYLE_LINE_HEIGHT, lineheight); + label->SetText(text); + label->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetRollSpeed(0); + label->SetFont(DEFAULT_VECTOR_FONT_FILENAME, textfont); + label->SetVisible(true); + return label; + } + + // 初始化天气页面 + bool WeatherView::InitPage(void) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, " WeatherView::InitPage()"); + /* -------------------------------------------------------------------------- */ + showWeatherImage = new UIImageView(); + if (showWeatherImage == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new showWeatherImage fail!"); + return false; + } + showWeatherImage->SetPosition(109, 124, 100, 100); + showWeatherImage->SetSrc(WeatherImag.c_str()); + /* -------------------------------------------------------------------------- */ + showCurrentTimeLabel = InitLabel(185, 52, Time.c_str(), "showCurrentTimeLabel", 40, 47); + if (showCurrentTimeLabel == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new showCurrentTimeLabel fail!"); + return false; + } + /* -------------------------------------------------------------------------- */ + showTemperatureLabel = InitLabel(249, 126, Temperature.c_str(), "showTemperatureLabel", 100, 125); + if (showTemperatureLabel == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new showTemperatureLabel fail!"); + return false; + } + /* -------------------------------------------------------------------------- */ + showWeatherDescriptionLabel = InitLabel(111, 264, WeatherDescription.c_str(), "showWeatherDescriptionLabel", 35, 41); + if (showWeatherDescriptionLabel == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new showWeatherDescriptionLabel fail!"); + return false; + } + /* -------------------------------------------------------------------------- */ + showWeatherAreaLabel = InitLabel(155, 330, WeatherArea.c_str(), "showWeatherAreaLabel", 26, 30); + if (showWeatherAreaLabel == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new showWeatherAreaLabel fail!"); + return false; + } + showWeatherAreaLabel->SetTextColor(Color::GetColorFromRGBA(0x99, 0x99, 0x99, 0xff)); + /* -------------------------------------------------------------------------- */ + showMaxandMinTemperatureLabel = InitLabel(158, 368, MaxandMinTemperature.c_str(), "showMaxandMinTemperatureLabel", 20, 23); + if (showMaxandMinTemperatureLabel == nullptr) + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::new showMaxandMinTemperatureLabel fail!"); + return false; + } + showMaxandMinTemperatureLabel->SetTextColor(Color::GetColorFromRGBA(0x99, 0x99, 0x99, 0xff)); + + /* -------------------------------- 将组件添加到根视图 ------------------------------- */ + Add(showWeatherImage); + Add(showCurrentTimeLabel); + Add(showTemperatureLabel); + Add(showWeatherDescriptionLabel); + Add(showWeatherAreaLabel); + Add(showMaxandMinTemperatureLabel); + viewInitStatus = true; + return true; + } + + bool WeatherView::InitView() + { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherView::InitView"); + if (InitPage() == false) + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitPage fail"); + return false; + } + SetDraggable(true); + SetTouchable(true); + return true; + } + + // 获取视图初始化状态 + bool WeatherView::GetInitViewState(void) + { + return viewInitStatus; + } +} \ No newline at end of file