天气界面优化,采用imagecash管理图片
authorluozheng <1732036298@qq.com>
Sat, 13 Apr 2024 10:54:50 +0000 (18:54 +0800)
committerluozheng <1732036298@qq.com>
Sat, 13 Apr 2024 10:54:50 +0000 (18:54 +0800)
code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherModel.h
code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherReportView.h
code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h
code/sdk/application/wearable/fd230app/nativeui/weather/src/FutureWeatherView.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherView.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherReportView.cpp
code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherView.cpp

index 4e1a2a1..1c72c64 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-04-08 18:40:20
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-04-13 16:53:26
+ * @LastEditTime: 2024-04-13 18:23:57
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherModel.h
  */
 #ifndef WEATHER_MODEL_H
@@ -20,7 +20,6 @@ namespace OHOS
 #endif /* __cplusplus */
 #include "string.h"
 
-        static uint8_t WEATHER_DATA_VALID = 0; // 天气数据是否有效
         /*天气数据*/
         struct Weather_data
         {
@@ -62,11 +61,10 @@ namespace OHOS
     }
 #endif /* __cplusplus */
 
-    static constexpr const char *WeatherBackground = "WEATHER_BACKGROUND.bin"; // 天气背景
     static constexpr const char *weather130x130[] =
         {
             "/user/res/weather/weather130x130/WEATHER_CLEAR-CLOUDY.bin",
-            "/user/res/weather/weather130x130/WEATHER_CLEAR-SUNNY.bin", //晴
+            "/user/res/weather/weather130x130/WEATHER_CLEAR-SUNNY.bin", // 晴
             "/user/res/weather/weather130x130/WEATHER_DUSTSTORM.bin",
             "/user/res/weather/weather130x130/WEATHER_FOGGY.bin",
             "/user/res/weather/weather130x130/WEATHER_HAZE.bin",
@@ -94,6 +92,11 @@ namespace OHOS
         std::string GetWeather_data_seven_data_textDay(int date);
         std::string GetWeather_data_seven_data_fxDate(int date);
         std::string GetWeather_data_seven_data_tempMinAndtempMax(int date);
+        /* -------------------------------------------------------------------------- */
+        std::string GetHumidity();
+        std::string GetWindScale();
+        std::string GetUVIndex();
+        static uint8_t WEATHER_DATA_VALID; // 天气数据是否有效
     private:
         WeatherModel();
         WeatherModel(const WeatherModel &) = delete;
index a06a92a..553edbf 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com\r
  * @Date: 2024-03-30 09:14:38\r
  * @LastEditors: luozheng 1732036298@qq.com\r
- * @LastEditTime: 2024-04-12 16:41:27\r
+ * @LastEditTime: 2024-04-13 18:01:59\r
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherReportView.h\r
  */\r
 #ifndef WEATHER_REPORT_VIEW_H\r
@@ -31,7 +31,7 @@ namespace OHOS
             int16_t height;\r
             int16_t fontSize;      // 字体大小\r
             int16_t lineHeigh;     // 行高\r
-            const char *text;      // 文本\r
+            std::string text;      // 文本\r
             uint8_t lineBreakMode; // 断行模式。\r
         };\r
         WeatherReportView();\r
index 331cbcf..c86b933 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-03-30 09:14:38
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-04-12 15:06:35
+ * @LastEditTime: 2024-04-13 18:34:25
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/include/WeatherView.h
  */
 #ifndef WEATHER_VIEW_H
@@ -23,6 +23,7 @@
 
 namespace OHOS
 {
+    static constexpr const char *WeatherBackground = "/user/res/weather/WEATHER_BACKGROUND.bin"; // 天气背景
     class WeatherView : public AppGroupView
     {
 
@@ -49,6 +50,7 @@ namespace OHOS
         std::string WeatherArea;          // 天气地区
         std::string MaxandMinTemperature; // 最高与最低气温
     private:
+        UIImageView *WeatherBackGroundImg{nullptr};         // 天气背景
         UIImageView *showWeatherImage = nullptr;            // 显示天气图片
         UILabelExt *showCurrentTimeLabel{nullptr};          // 显示当前时间
         UILabelExt *showTemperatureLabel{nullptr};          // 显示天气温度
index 7539595..191b1a0 100644 (file)
@@ -1,7 +1,7 @@
 #include "weather/include/FutureWeatherView.h"\r
 #include "wearable_log.h"\r
 #include "weather/include/WeatherModel.h"\r
-\r
+#include "common/image_cache_manager.h"\r
 namespace OHOS\r
 {\r
 \r
@@ -108,8 +108,8 @@ namespace OHOS
                 {\r
                     weatherImg[i] = new UIImageView();\r
                 }\r
-                weatherImg[i]->SetPosition(68, 130 + i * yImg, 65, 65);                                     // SetPosition的参数分别是x, y, width, height\r
-                weatherImg[i]->SetSrc(weather65x65[p_WeatherModel->GetWeather_data_seven_data_iconDay(i)]); // 请设置实际的图片源\r
+                weatherImg[i]->SetPosition(68, 130 + i * yImg, 65, 65);                                                                                     // SetPosition的参数分别是x, y, width, height\r
+                weatherImg[i]->SetSrc(ImageCacheManager::GetInstance().LoadSingleRes(weather65x65[p_WeatherModel->GetWeather_data_seven_data_iconDay(i)])); // 请设置实际的图片源\r
 \r
                 // 创建日期标签\r
                 if (dateLabel[i] == nullptr)\r
index 18fd114..0433e80 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com\r
  * @Date: 2024-01-29 15:37:44\r
  * @LastEditors: luozheng 1732036298@qq.com\r
- * @LastEditTime: 2024-04-13 17:31:05\r
+ * @LastEditTime: 2024-04-13 17:51:01\r
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/MainWeatherView.cpp\r
  */\r
 #include "weather/include/MainWeatherView.h"\r
@@ -125,7 +125,7 @@ namespace OHOS
     void MainWeatherView::OnStart()\r
     {\r
         View<MainWeatherPresenter>::OnStart();\r
-        if (WEATHER_DATA_VALID)\r
+        if (WeatherModel::WEATHER_DATA_VALID)\r
         {\r
             MainWeatherViewInit();\r
             RootView::GetInstance()->Add(weatherSwipeViewList);\r
index b5a27dd..b9c182e 100644 (file)
@@ -2,7 +2,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-04-08 18:40:09
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-04-13 17:07:14
+ * @LastEditTime: 2024-04-13 18:00:02
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/weather/src/WeatherModel.cpp
  */
 #include "weather/include/WeatherModel.h"
@@ -36,7 +36,7 @@ namespace OHOS
             Weather_data_t_now.temp = temp;
             Weather_data_t_now.iconNight = iconNight;
 
-            WEATHER_DATA_VALID = 1;
+            WeatherModel::WEATHER_DATA_VALID = 1;
         }
 
         /*
@@ -94,6 +94,7 @@ namespace OHOS
         "\"code\":1}";
 
     /* -------------------------------------------------------------------------- */
+    uint8_t WeatherModel::WEATHER_DATA_VALID = 0;
     WeatherModel::WeatherModel()
     {
         WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "WeatherModel::WeatherModel");
@@ -176,8 +177,7 @@ namespace OHOS
     std::string WeatherModel::GetWeather_data_seven_data_fxDate(int date)
     {
         static char formattedDate[6]; // 5字符加上终结符'\0'
-        TimestampToMonthDayString(Weather_data_seven_data[date].fxDate,formattedDate);
-        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "formattedDate:%s",formattedDate);
+        TimestampToMonthDayString(Weather_data_seven_data[date].fxDate, formattedDate);
         return formattedDate;
     }
 
@@ -190,4 +190,29 @@ namespace OHOS
         return maxandminTemperature;
     }
 
+    /* -------------------------------------------------------------------------- */
+
+    /*获取湿度*/
+    std::string WeatherModel::GetHumidity()
+    {
+        int humidity = Weather_data_t_now.humidity;
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "humidity:%d", humidity);
+        return std::to_string(humidity);
+    }
+
+    /*获取风速*/
+    std::string WeatherModel::GetWindScale()
+    {
+        std::string windScale = Weather_data_t_now.windScale;
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "windScale:%s", windScale.c_str());
+        return windScale;
+    }
+
+    /*获取紫外线指数*/
+    std::string WeatherModel::GetUVIndex()
+    {
+        std::string uvIndex = std::to_string(Weather_data_t_now.uvIndex) + "%";
+        WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "uvIndex:%s", uvIndex.c_str());
+        return uvIndex;
+    }
 }
\ No newline at end of file
index 702037e..58cf2a3 100644 (file)
@@ -1,6 +1,7 @@
 #include "weather/include/WeatherReportView.h"\r
 #include "wearable_log.h"\r
 #include "weather/include/WeatherModel.h"\r
+#include "common/image_cache_manager.h"\r
 namespace OHOS\r
 {\r
     static constexpr int16_t UV_IMAGE_ANIMATOR_VIEW_X = 53;\r
@@ -102,7 +103,7 @@ namespace OHOS
         label->SetPosition(labelconfig.left, labelconfig.top, labelconfig.width, labelconfig.height); // 位置大小\r
         label->SetStyle(STYLE_TEXT_FONT, labelconfig.fontSize);                                       // 字体大小\r
         label->SetStyle(STYLE_LINE_HEIGHT, labelconfig.lineHeigh);                                    // 字体行高\r
-        label->SetText(labelconfig.text);                                                             // 内容\r
+        label->SetText(labelconfig.text.c_str());                                                             // 内容\r
         label->SetLineBreakMode(labelconfig.lineBreakMode);                                           // 断行模式。\r
         label->SetRollSpeed(40);\r
         label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);\r
@@ -128,6 +129,7 @@ namespace OHOS
             WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherReportView::title new fail!");\r
             return false;\r
         }\r
+        WeatherModel *p_WeatherModel = &WeatherModel::GetInstance();\r
         /* -------------------------------------------------------------------------- */\r
         labelconfig = {\r
             .left = 23,\r
@@ -136,7 +138,7 @@ namespace OHOS
             .height = 50,\r
             .fontSize = 40,\r
             .lineHeigh = 50,\r
-            .text = "7",\r
+            .text = p_WeatherModel->GetHumidity().c_str(),\r
             .lineBreakMode = UILabel::LINE_BREAK_ELLIPSIS,\r
         };\r
         UV_Number = SetUpLabel(labelconfig);\r
@@ -147,7 +149,7 @@ namespace OHOS
         }\r
 \r
         labelconfig.left = 173;\r
-        labelconfig.text = "1-2";\r
+        labelconfig.text = p_WeatherModel->GetWindScale();\r
         WindSpeed_Number = SetUpLabel(labelconfig);\r
         if (WindSpeed_Number == nullptr)\r
         {\r
@@ -156,7 +158,7 @@ namespace OHOS
         }\r
 \r
         labelconfig.left = 323;\r
-        labelconfig.text = "75%";\r
+        labelconfig.text = p_WeatherModel->GetUVIndex();\r
         Humidity_Number = SetUpLabel(labelconfig);\r
         if (Humidity_Number == nullptr)\r
         {\r
@@ -231,8 +233,8 @@ namespace OHOS
             }\r
             for (uint8_t i = 0; i < UV_IMAGE_ANIMATOR_VIEW_NUM_FRAMES; i++)\r
             {\r
-                UVimageAnimatorInfo[i].imagePath = (UVPathsArray[i]);\r
-                UVimageAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH;\r
+                UVimageAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(UVPathsArray[i]);\r
+                UVimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;\r
                 UVimageAnimatorInfo[i].pos.x = UV_IMAGE_ANIMATOR_VIEW_X;\r
                 UVimageAnimatorInfo[i].pos.y = UV_IMAGE_ANIMATOR_VIEW_Y;\r
                 UVimageAnimatorInfo[i].width = UV_IMAGE_ANIMATOR_VIEW_W;\r
@@ -257,8 +259,8 @@ namespace OHOS
             }\r
             for (uint8_t i = 0; i < WINDSPEED_IMAGE_ANIMATOR_VIEW_NUM_FRAMES; i++)\r
             {\r
-                WindSpeedimageAnimatorInfo[i].imagePath = (WindSpeedPathsArray[i]);\r
-                WindSpeedimageAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH;\r
+                WindSpeedimageAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(WindSpeedPathsArray[i]);\r
+                WindSpeedimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;\r
                 WindSpeedimageAnimatorInfo[i].pos.x = WINDSPEED_IMAGE_ANIMATOR_VIEW_X;\r
                 WindSpeedimageAnimatorInfo[i].pos.y = UV_IMAGE_ANIMATOR_VIEW_Y;\r
                 WindSpeedimageAnimatorInfo[i].width = UV_IMAGE_ANIMATOR_VIEW_W;\r
@@ -283,8 +285,8 @@ namespace OHOS
             }\r
             for (uint8_t i = 0; i < HUMIDITY_IMAGE_ANIMATOR_VIEW_NUM_FRAMES; i++)\r
             {\r
-                HumidityimageAnimatorInfo[i].imagePath = (HumidityPathsArray[i]);\r
-                HumidityimageAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH;\r
+                HumidityimageAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(HumidityPathsArray[i]);\r
+                HumidityimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;\r
                 HumidityimageAnimatorInfo[i].pos.x = HUMIDITY_IMAGE_ANIMATOR_VIEW_X;\r
                 HumidityimageAnimatorInfo[i].pos.y = UV_IMAGE_ANIMATOR_VIEW_Y;\r
                 HumidityimageAnimatorInfo[i].width = UV_IMAGE_ANIMATOR_VIEW_W;\r
index c1399bf..adec552 100644 (file)
@@ -1,6 +1,7 @@
 #include "weather/include/WeatherView.h"
 #include "config/include/ConfigModel.h"
 #include "weather/include/WeatherModel.h"
+#include "common/image_cache_manager.h"
 /*
 当至天气界面时,主动获取最新天气数据。
 (此操作静默将进行,获取到则立刻更新显示,获取不到则重试
 
 namespace OHOS
 {
+     /*主天气背景图片*/
+    static constexpr int16_t WEATHER_BACKGROUND_IMAGE_X = 0;
+    static constexpr int16_t WEATHER_BACKGROUND_IMAGE_Y = 0;
+    static constexpr int16_t WEATHER_BACKGROUND_IMAGE_W = 466;
+    static constexpr int16_t WEATHER_BACKGROUND_IMAGE_H = 466;
+
     /*天气图片*/
     static constexpr int16_t WEATHER_IMAGE_X = 77;
     static constexpr int16_t WEATHER_IMAGE_Y = 124;
@@ -71,15 +78,14 @@ namespace OHOS
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::WeatherView");
         SetPosition(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
         SetViewIndex(0); // 视图索引
-        if (1)
-        {
-            WeatherModel *p_WeatherModel = &WeatherModel::GetInstance();
-            WeatherImag = p_WeatherModel->GetWeatherImage();
-            Temperature = p_WeatherModel->GetWeatherTemperature();
-            WeatherDescription = p_WeatherModel->GetWeatherDescription();
-            WeatherArea = p_WeatherModel->GetWeatherArea();
-            MaxandMinTemperature = p_WeatherModel->GetWeatherMaxAndMinTemperature();
-        }
+
+        WeatherModel *p_WeatherModel = &WeatherModel::GetInstance();
+        WeatherImag = p_WeatherModel->GetWeatherImage();
+        Temperature = p_WeatherModel->GetWeatherTemperature();
+        WeatherDescription = p_WeatherModel->GetWeatherDescription();
+        WeatherArea = p_WeatherModel->GetWeatherArea();
+        MaxandMinTemperature = p_WeatherModel->GetWeatherMaxAndMinTemperature();
+
         g_pWeatherView = this;
     }
 
@@ -95,6 +101,11 @@ namespace OHOS
         if (viewInitStatus == true)
         {
             RemoveAll();
+            if (WeatherBackGroundImg != nullptr)
+            {
+                delete WeatherBackGroundImg;
+                WeatherBackGroundImg = nullptr;
+            }
             if (showWeatherImage != nullptr)
             {
                 delete showWeatherImage;
@@ -165,6 +176,15 @@ namespace OHOS
     {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, " WeatherView::InitPage");
         /* -------------------------------------------------------------------------- */
+        WeatherBackGroundImg = new UIImageView();
+        if (WeatherBackGroundImg == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "WeatherView::new WeatherBackGroundImg fail!");
+            return false;
+        }
+        WeatherBackGroundImg->SetPosition(WEATHER_BACKGROUND_IMAGE_X, WEATHER_BACKGROUND_IMAGE_Y, WEATHER_BACKGROUND_IMAGE_W, WEATHER_BACKGROUND_IMAGE_H);
+        WeatherBackGroundImg->SetSrc(ImageCacheManager::GetInstance().LoadSingleRes(WeatherBackground));
+        /* -------------------------------------------------------------------------- */
         showWeatherImage = new UIImageView();
         if (showWeatherImage == nullptr)
         {
@@ -172,7 +192,7 @@ namespace OHOS
             return false;
         }
         showWeatherImage->SetPosition(WEATHER_IMAGE_X, WEATHER_IMAGE_Y, WEATHER_IMAGE_W, WEATHER_IMAGE_W);
-        showWeatherImage->SetSrc(WeatherImag.c_str());
+        showWeatherImage->SetSrc(ImageCacheManager::GetInstance().LoadSingleRes(WeatherImag));
         /* -------------------------------------------------------------------------- */
         showCurrentTimeLabel = InitLabelExt(CURRENT_TIME_LABEL_X, CURRENT_TIME_LABEL_Y, CURRENT_TIME_LABEL_W, CURRENT_TIME_LABEL_H, "",
                                             CURRENT_TIME_LABEL_TEXT_FONT, CURRENT_TIME_LABEL_LINE_HEIGHT);
@@ -227,8 +247,8 @@ namespace OHOS
             }
             for (uint8_t i = 0; i < PULLUP_ANIMATOR_VIEW_NUM_FRAMES; i++)
             {
-                PullUpimageAnimatorInfo[i].imagePath = (PullUpPathsArray[i]);
-                PullUpimageAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH;
+                PullUpimageAnimatorInfo[i].imageInfo = ImageCacheManager::GetInstance().LoadSingleRes(PullUpPathsArray[i]);
+                PullUpimageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
                 PullUpimageAnimatorInfo[i].pos.x = PULLUP_ANIMATOR_VIEW_X;
                 PullUpimageAnimatorInfo[i].pos.y = PULLUP_ANIMATOR_VIEW_Y;
                 PullUpimageAnimatorInfo[i].width = PULLUP_ANIMATOR_VIEW_W;
@@ -242,6 +262,7 @@ namespace OHOS
             PullUpAnimatorView->Start();
         }
         /* -------------------------------- 将组件添加到根视图 ------------------------------- */
+        Add(WeatherBackGroundImg);
         Add(showWeatherImage);
         Add(showCurrentTimeLabel);
         Add(showTemperatureLabel);