主界面添加天气视图
author罗正yun <1732036298@qq.com>
Tue, 16 Jan 2024 10:46:07 +0000 (18:46 +0800)
committer罗正yun <1732036298@qq.com>
Tue, 16 Jan 2024 10:46:07 +0000 (18:46 +0800)
code/sdk/application/wearable/nativeapp/CMakeLists.txt
code/sdk/application/wearable/nativeapp/nativeui/main/include/MainViewSample.h
code/sdk/application/wearable/nativeapp/nativeui/main/src/MainViewSample.cpp
code/sdk/application/wearable/nativeapp/nativeui/screensetting/include/ScreenModels.h
code/sdk/application/wearable/nativeapp/nativeui/weather/include/MainWeatherView.h
code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherControlView.h [new file with mode: 0644]
code/sdk/application/wearable/nativeapp/nativeui/weather/include/WeatherView.h [new file with mode: 0644]
code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherControlView.cpp [new file with mode: 0644]
code/sdk/application/wearable/nativeapp/nativeui/weather/src/WeatherView.cpp [new file with mode: 0644]

index c1a97ca..4926a50 100755 (executable)
@@ -354,6 +354,8 @@ set(SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/FutureWeatherView.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/GetWeatherDataErrorPresenter.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/GetWeatherDataErrorView.cpp\r
+    ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/WeatherControlView.cpp\r
+    ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/weather/src/WeatherView.cpp\r
     #倒计时\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/countdown/src/MainCountDownPresenter.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/countdown/src/MainCountDownView.cpp\r
index 24c3f76..cf7264f 100755 (executable)
@@ -41,6 +41,7 @@
 #include "dial/include/Digital_clockView.h"\r
 #include "blood/include/BloodView.h"\r
 #include "traing/include/TraingView.h"\r
+#include "weather/include/WeatherControlView.h"\r
 namespace OHOS\r
 {\r
     class MainPresenterSample;\r
@@ -61,6 +62,7 @@ namespace OHOS
         UICardPage *InitMusicPlayerPage(void);\r
         UICardPage *InitNewDialPage(void);\r
         UICardPage *InitBloodOxygenPage(void);\r
+        UICardPage *InitWeatherPage(void);\r
         UICardPage *InitTrainingPage(void);\r
 \r
         void AnimatorStart(int pageIndex);\r
@@ -96,6 +98,7 @@ namespace OHOS
         HeartRateView *heartRateView{nullptr};\r
         Digital_clockView *dialView{nullptr};\r
         BloodView *bloodView{nullptr};\r
+        WeatherControlView *weatherControlView{nullptr};\r
         TraingView *traingView{nullptr};\r
 \r
         /* -------------------------------------------------------------------------- */\r
index 26991d9..ad1bcce 100755 (executable)
@@ -41,6 +41,7 @@ namespace OHOS
         {COMPASS, &MainViewSample::InitCompassPage},\r
         {BloodOxygen, &MainViewSample::InitBloodOxygenPage},\r
         {TRAINING, &MainViewSample::InitTrainingPage},\r
+        {WEATHER,&MainViewSample::InitWeatherPage},\r
     };\r
 \r
     MainViewSample::MainViewSample()\r
@@ -125,6 +126,11 @@ namespace OHOS
             delete bloodView;\r
             bloodView = nullptr;\r
         }\r
+        if(weatherControlView != nullptr)\r
+        {\r
+            delete weatherControlView;\r
+            weatherControlView = nullptr;\r
+        }\r
         g_pMainView = nullptr;\r
     }\r
 \r
@@ -374,6 +380,17 @@ namespace OHOS
         }\r
         return traingView;\r
     }\r
+    //主界面天气卡片\r
+    UICardPage *MainViewSample::InitWeatherPage(void)\r
+    {\r
+        weatherControlView = new WeatherControlView();\r
+        if(weatherControlView == nullptr)\r
+        {\r
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mainweatherView new fail");\r
+            return nullptr;\r
+        }\r
+        return weatherControlView;\r
+    }\r
     /* -------------------------------------------------------------------------- */\r
     UICardPage *MainViewSample::CreatePage(int32_t StreamId)\r
     {\r
index 51f5392..a469099 100755 (executable)
@@ -24,6 +24,7 @@ namespace OHOS
         TRAINING,\r
         COMPASS,\r
         BloodOxygen,\r
+        WEATHER,\r
         MAX_CARD,\r
     } CardId;\r
 \r
index b7da481..37bd305 100755 (executable)
@@ -9,27 +9,9 @@
 #include "components/ui_label.h"\r
 #include "components/ui_image_view.h"\r
 #include "components/ui_image_animator.h"\r
+#include "weather/include/WeatherView.h"\r
 #include <iostream>\r
 \r
-namespace OHOS\r
-{\r
-#define weather_src "/user/res/weather/"\r
-    static const char *background = weather_src "WEATHER_BACKGROUND.bin";\r
-    static const char *weather130x130[] =\r
-        {\r
-            weather_src "WEATHER_CLEAR-CLOUDY.bin",\r
-            weather_src "WEATHER_CLEAR-SUNNY.bin",\r
-            weather_src "WEATHER_DUSTSTORM.bin",\r
-            weather_src "WEATHER_FOGGY.bin",\r
-            weather_src "WEATHER_HAZE.bin",\r
-            weather_src "WEATHER_OVERCAST.bin",\r
-            weather_src "WEATHER_RAIN.bin",\r
-            weather_src "WEATHER_SNOW.bin",\r
-            weather_src "WEATHER_SUNNY.bin",\r
-            weather_src "WEATHER_SUNNY-CLOUNDY.bin",\r
-            weather_src "WEATHER_THUNDERSHOWER.bin",\r
-            weather_src "WEATHER_UNKNOWN.bin"};\r
-}\r
 \r
 namespace OHOS\r
 {\r
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 (file)
index 0000000..488253a
--- /dev/null
@@ -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 (file)
index 0000000..24b8e64
--- /dev/null
@@ -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 <iostream>
+
+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 (file)
index 0000000..43affeb
--- /dev/null
@@ -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 (file)
index 0000000..b281f28
--- /dev/null
@@ -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