优化指针表盘bug以及数字表盘
authorCaisking <3449750381@qq.com>
Tue, 9 Jan 2024 08:50:57 +0000 (16:50 +0800)
committerCaisking <3449750381@qq.com>
Tue, 9 Jan 2024 08:50:57 +0000 (16:50 +0800)
code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h
code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/clocks/KUMI.json
code/sdk/middleware/services/gui/uikit/ui/frameworks/components/ui_image_view.cpp

index ac67d75..e2a6ef9 100644 (file)
 #include "components/ui_button.h"
 #include "traing/include/TraingWinView.h"
 #include "components/ui_view_group.h"
-#include "components/ui_label.h"
-#include "components/ui_image_view.h"
-#include "components/ui_button.h"
-#include "components/ui_image_animator.h"
 #include "components/ui_label_button_ext.h"
 #include "components/ui_chart.h"
 #include "components/ui_list.h"
@@ -109,13 +105,13 @@ namespace OHOS
         virtual void Callback(UIView *view) override
         {
             // 获取电池电压值
-            // float voltage = 0.0F;
-            // uint8_t value = 0;
-            // value = battery_get_voltage(&voltage);
+            float voltage = 0.0F;
+            uint8_t value = 0;
+            value = battery_get_voltage(&voltage);
             // 设置种子
-            srand(static_cast<unsigned int>(time(0)));
+            // srand(static_cast<unsigned int>(time(0)));
             // 生成随机数
-            int value = rand() % 101; // 取余操作可以将随机数限制在0到100之间
+            // int value = rand() % 101; // 取余操作可以将随机数限制在0到100之间
             printf("value:%d\n", value);
             // 根据电池电量值选择相应的进度条前景图片
             char *progress1ForeImageSrc = nullptr;
@@ -146,10 +142,12 @@ namespace OHOS
         UIImageView *hour2{nullptr};   // 时钟小时第二位图像控件指针
         UIImageView *minute1{nullptr}; // 时钟分钟第一位图像控件指针
         UIImageView *minute2{nullptr}; // 时钟分钟第二位图像控件指针
-        std::string stringtime = "14:56";
+        std::string stringtime;
+        std::string imageFolderPath;
 
     public:
-        explicit mytimeimageCallback(UIImageView *hour1, UIImageView *hour2, UIImageView *minute1, UIImageView *minute2) : hour1(hour1), hour2(hour2), minute1(minute1), minute2(minute2) {}
+        explicit mytimeimageCallback(UIImageView *hour1, UIImageView *hour2, UIImageView *minute1, UIImageView *minute2, std::string imageFolderPath, std::string stringtime)
+            : hour1(hour1), hour2(hour2), minute1(minute1), minute2(minute2), imageFolderPath(imageFolderPath), stringtime(stringtime) {}
         ~mytimeimageCallback() {}
 
         // 时钟图片更新的回调函数
@@ -168,11 +166,11 @@ namespace OHOS
             // 将小时和分钟转换为字符串,并更新 stringtime
             stringtime = (hour < 10 ? "0" : "") + std::to_string(hour) + ":" + (minute < 10 ? "0" : "") + std::to_string(minute);
             printf("Before updating images\n");
-            std::string imageFolderPath = "/user/res/clocks/flowerbud/";
-            updateImage(hour1, imageFolderPath, stringtime.substr(0, 1));
-            updateImage(hour2, imageFolderPath, stringtime.substr(1, 1));
-            updateImage(minute1, imageFolderPath, stringtime.substr(3, 1));
-            updateImage(minute2, imageFolderPath, stringtime.substr(4, 1));
+            printf("--%s:%s--\n", imageFolderPath.c_str(), stringtime.c_str());
+            updateImage(hour1, imageFolderPath.c_str(), stringtime.substr(0, 1));
+            updateImage(hour2, imageFolderPath.c_str(), stringtime.substr(1, 1));
+            updateImage(minute1, imageFolderPath.c_str(), stringtime.substr(3, 1));
+            updateImage(minute2, imageFolderPath.c_str(), stringtime.substr(4, 1));
             printf("After updating images\n");
         }
 
@@ -185,6 +183,7 @@ namespace OHOS
             {
                 printf("File exists: %s\n", imagePath.c_str());
                 imageControl->SetSrc(imagePath.c_str());
+                printf("File set!\n");
             }
             else
             {
@@ -262,8 +261,9 @@ namespace OHOS
         Animator *sweepClock1animator_ = nullptr;
         bool viewiInitStatus{false};
         const char **imagePaths;
+        std::string digitspath;
         std::string stringtime;
-        std::string JosnfilePath = "/user/res/clocks/maomao.json";
+        std::string JosnfilePath = "/user/res/clocks/KUMI.json";
     };
 } // namespace OHOS
 #endif // Digital_clock_PRESENTER_H
index fe958d3..415f17c 100644 (file)
@@ -14,7 +14,9 @@
 #include <sys/stat.h>
 #include "cJSON.h"
 #include <dirent.h>
-
+#include <regex>
+#include <iomanip>   // 用于格式化输出
+#include <stdexcept> //异常处理
 
 #define PRINT_DEBUG_INFO printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
 
@@ -144,6 +146,9 @@ namespace OHOS
                 // Add code to set the background as a video
             }
         }
+        // 增加背景
+        container_->Add(background);
+        background->SetVisible(true);
         PRINT_DEBUG_INFO
     }
     /*初始化表盘时钟*/
@@ -160,8 +165,6 @@ namespace OHOS
     }
     void Digital_clockView ::Initdigit()
     {
-        container_->Add(background);
-        background->SetVisible(true);
         cJSON *background_cjson = cJSON_GetObjectItem(cjson_json, "background");
         std::string backgroundimagesPath = cJSON_GetObjectItem(background_cjson, "backgroundimagesPath")->valuestring;
         if (isDirectory(backgroundimagesPath))
@@ -178,18 +181,19 @@ namespace OHOS
         {
             cJSON *picturesdisplay = cJSON_GetObjectItem(digits, "picturesdisplay");
             cJSON *coordinates = cJSON_GetObjectItem(picturesdisplay, "coordinates");
-            const char *digitspath = cJSON_GetObjectItem(cjson_json, "digitspath")->valuestring;
+            digitspath = cJSON_GetObjectItem(cjson_json, "digitspath")->valuestring;
             int hour1_left = cJSON_GetObjectItem(coordinates, "left")->valueint;
             int hour1_top = cJSON_GetObjectItem(coordinates, "top")->valueint;
             int digitsize = cJSON_GetObjectItem(digits, "digitsize")->valueint;
 
-            addImageView(hour1_left, hour1_top, (std::string(digitspath) + stringtime.substr(0, 1) + ".bin").c_str(), "hour1", 0, 0);
-            addImageView(hour1_left, hour1_top, (std::string(digitspath) + stringtime.substr(1, 1) + ".bin").c_str(), "hour1", digitsize, 0);
-            addImageView(hour1_left, hour1_top, (std::string(digitspath) + "point.bin").c_str(), "colon", digitsize * 2 + 10, 20);
-            addImageView(hour1_left, hour1_top, (std::string(digitspath) + stringtime.substr(3, 1) + ".bin").c_str(), "minute1", digitsize * 3, 0);
-            addImageView(hour1_left, hour1_top, (std::string(digitspath) + stringtime.substr(4, 1) + ".bin").c_str(), "minute2", digitsize * 4, 0);
-            mytimeimageCallback_ = new mytimeimageCallback(hour1, hour2, minute1, minute2);
-            timeimageanimator_ = new Animator(mytimeimageCallback_, hour1, 10000, true);
+            addImageView(hour1_left, hour1_top, (digitspath + stringtime.substr(0, 1) + ".bin").c_str(), "hour1", 0, 0);
+            addImageView(hour1_left, hour1_top, (digitspath + stringtime.substr(1, 1) + ".bin").c_str(), "hour1", digitsize, 0);
+            addImageView(hour1_left, hour1_top, (digitspath + "point.bin").c_str(), "colon", digitsize * 2 + 10, 20);
+            addImageView(hour1_left, hour1_top, (digitspath + stringtime.substr(3, 1) + ".bin").c_str(), "minute1", digitsize * 3, 0);
+            addImageView(hour1_left, hour1_top, (digitspath + stringtime.substr(4, 1) + ".bin").c_str(), "minute2", digitsize * 4, 0);
+            printf("--%s--\n", digitspath.c_str());
+            mytimeimageCallback_ = new mytimeimageCallback(hour1, hour2, minute1, minute2, digitspath, stringtime);
+            timeimageanimator_ = new Animator(mytimeimageCallback_, container_, 10000, true);
             // callback.mytimeimageCallback("14:57");
             // timeimageanimator_->Start();
         }
@@ -222,7 +226,7 @@ namespace OHOS
             timeLable->SetVisible(true);
 
             timeLableCallback_ = new timeLableCallback(timeLable, stringtime);
-            timeLableanimator_ = new Animator(timeLableCallback_, timeLable, 10000, true);
+            timeLableanimator_ = new Animator(timeLableCallback_, timeLable, 1000, true);
             timeLableanimator_->Start();
         }
     }
@@ -261,10 +265,6 @@ namespace OHOS
         sweepClock1_->SetStyle(STYLE_BACKGROUND_COLOR, 0xff000000);
         sweepClock1_->SetStyle(STYLE_BACKGROUND_OPA, 0x00);
 
-        // 增加背景
-        sweepClock1_->Add(background);
-        background->SetVisible(true);
-
         // Set additional property
         sweepClock1_->SetWorkMode(UISweepClock::WorkMode::NORMAL);
         Image *sweepClock1HourImage = new Image();
@@ -293,17 +293,45 @@ namespace OHOS
         container_->Add(sweepClock1_);
         sweepClock1_->SetVisible(true);
     }
+
+    std::string FormatDate(const std::string &dateStr, const std::string &displayformat)
+    {
+        // 定义默认返回值
+        std::string formattedDate = "Invalid format";
+        // 输出日期字符串和子字符串
+        std::cout << "--date: " << dateStr << "--" << dateStr.substr(0, 4) << "--" << dateStr.substr(5, 2) << "--" << dateStr.substr(8, 2) << std::endl;
+        int year = std::stoi(dateStr.substr(0, 4));  // 从第0个字符开始,提取4个字符作为年份
+        int month = std::stoi(dateStr.substr(5, 2)); // 从第5个字符开始,提取2个字符作为月份
+        int day = std::stoi(dateStr.substr(8, 2));   // 从第8个字符开始,提取2个字符作为日期
+        if (displayformat == "yyyy-mm-dd")
+        {
+            formattedDate = std::to_string(year) + "-" + (month < 10 ? "0" : "") + std::to_string(month) + "-" + (day < 10 ? "0" : "") + std::to_string(day);
+        }
+        else if (displayformat == "mm月dd日")
+        {
+            formattedDate = std::to_string(month) + "月" + (day < 10 ? "0" : "") + std::to_string(day) + "号";
+        }
+        else if (displayformat == "dd")
+        {
+            formattedDate = (day < 10 ? "0" : "") + std::to_string(day);
+        }
+        PRINT_DEBUG_INFO
+        // 添加其他显示格式的处理逻辑
+
+        return formattedDate;
+    }
     void Digital_clockView ::Initdata()
     {
         // loadJsonData(JosnfilePath);
         cJSON *date = cJSON_GetObjectItem(cjson_json, "date");
         cJSON *textdisplay = cJSON_GetObjectItem(date, "textdisplay");
-        cJSON *displayformat = cJSON_GetObjectItem(textdisplay, "displayformat");
         cJSON *coordinates = cJSON_GetObjectItem(textdisplay, "coordinates");
+        PRINT_DEBUG_INFO
         // const char *font = cJSON_GetObjectItem(textdisplay, "font")->valuestring;
         int fontsize = cJSON_GetObjectItem(textdisplay, "fontsize")->valueint;
         int date_left = cJSON_GetObjectItem(coordinates, "left")->valueint;
         int date_top = cJSON_GetObjectItem(coordinates, "top")->valueint;
+        PRINT_DEBUG_INFO
         // Add Label to App
         if (dateLable == nullptr)
         {
@@ -312,9 +340,14 @@ namespace OHOS
         dateLable->SetPosition(date_left, date_top); // widget's left position and top position
         dateLable->SetViewId("label1");              // Set style
         dateLable->SetStyle(STYLE_TEXT_FONT, fontsize);
+        PRINT_DEBUG_INFO
+        std::string dateStr = "2024-02-09";
 
-        // Set additional property
-        dateLable->SetText("1月8号");
+        std::string displayformat = cJSON_GetObjectItem(textdisplay, "displayformat")->valuestring;
+        // 调用日期格式化函数
+        std::string formattedDate = FormatDate(dateStr, displayformat);
+        PRINT_DEBUG_INFO
+        dateLable->SetText(formattedDate.c_str());
         dateLable->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
         dateLable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
         dateLable->SetRollSpeed(0);
@@ -323,6 +356,7 @@ namespace OHOS
         // Add to rootView
         container_->Add(dateLable);
         dateLable->SetVisible(true);
+        PRINT_DEBUG_INFO
     };
     void Digital_clockView ::Initweek()
     {
@@ -346,7 +380,17 @@ namespace OHOS
         weekLable->SetStyle(STYLE_TEXT_FONT, fontsize);
 
         // Set additional property
-        weekLable->SetText("星期一");
+        cJSON *weekformat = cJSON_GetObjectItem(displayformat, "weekformat");
+        std ::string weekStr;
+        if (weekformat != nullptr && cJSON_IsArray(weekformat))
+        {
+            cJSON *weekformat1 = cJSON_GetArrayItem(weekformat, 1);
+            if (weekformat1 != nullptr && cJSON_IsString(weekformat1))
+            {
+                weekStr = weekformat1->valuestring;
+            }
+        }
+        weekLable->SetText(weekStr.c_str());
         weekLable->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT);
         weekLable->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
         weekLable->SetRollSpeed(0);
@@ -440,6 +484,7 @@ namespace OHOS
         {
             std::string Dialtype = cJSON_GetObjectItem(cjson_json, "Dialtype")->valuestring;
             Initbackground();
+            PRINT_DEBUG_INFO
             if (Dialtype == "pointer")
             {
                 Initpointer();
@@ -448,9 +493,13 @@ namespace OHOS
             {
                 Initdigit();
             }
+            PRINT_DEBUG_INFO
             Initdata();
+            PRINT_DEBUG_INFO
             Initweek();
+            PRINT_DEBUG_INFO
             Initbattery();
+            PRINT_DEBUG_INFO
             cJSON_Delete(cjson_json);
         }
     }
@@ -469,40 +518,64 @@ namespace OHOS
             }
             delete tempView;
         }
-        // FreeImagePathsArray(imagePaths);
+        /********************************************/
         if (timeLableanimator_ != nullptr)
         {
             timeLableanimator_->Stop();
             delete timeLableanimator_;
             timeLableanimator_ = nullptr;
         }
+        if (timeLableCallback_ != nullptr)
+        {
+            delete timeLableCallback_;
+            timeLableCallback_ = nullptr;
+        }
+        /********************************************/
+        if (mytimeimageCallback_ != nullptr)
+        {
+            delete mytimeimageCallback_;
+            mytimeimageCallback_ = nullptr;
+        }
         if (timeimageanimator_ != nullptr)
         {
             timeimageanimator_->Stop();
             delete timeimageanimator_;
             timeimageanimator_ = nullptr;
         }
+        /********************************************/
+        if (progress1callback_ != nullptr)
+        {
+            delete progress1callback_;
+            progress1callback_ = nullptr;
+        }
         if (progress1animator_ != nullptr)
         {
             progress1animator_->Stop();
             delete progress1animator_;
             progress1animator_ = nullptr;
         }
+        /********************************************/
+        if (sweepClock1callback_ != nullptr)
+        {
+            delete sweepClock1callback_;
+            sweepClock1callback_ = nullptr;
+        }
         if (sweepClock1animator_ != nullptr)
         {
-            PRINT_DEBUG_INFO
-            sweepClock1_->RemoveAll();
-    
-            delete sweepClock1_;
-            sweepClock1_ = nullptr;
             sweepClock1animator_->Stop();
-            PRINT_DEBUG_INFO
             delete sweepClock1animator_;
             sweepClock1animator_ = nullptr;
         }
-        backgroundAnimator->Stop();
+        /********************************************/
+        if (backgroundAnimator != nullptr)
+        {
+            backgroundAnimator->Stop();
+            delete backgroundAnimator;
+            backgroundAnimator = nullptr;
+        }
         delete container_;
         container_ = nullptr;
+        PRINT_DEBUG_INFO
     }
 
     void Digital_clockView::PreLoad(void)
@@ -510,7 +583,6 @@ namespace OHOS
 
         if (!viewiInitStatus)
         {
-
             InitView();
             viewiInitStatus = true;
         }
index a6b43f9..23fb794 100644 (file)
@@ -5,11 +5,11 @@
         "timeformat": "24format",\r
         "digitsize": 60,\r
         "playformat": "textdisplay",\r
-        "fontsize": 50,\r
+        "fontsize": 55,\r
         "textdisplay": {\r
             "coordinates": {\r
-                "left": 168,\r
-                "top": 310\r
+                "left": 161,\r
+                "top": 293\r
             }\r
         }\r
     },\r
@@ -19,8 +19,8 @@
             "font": "STYLE_TEXT_FONT",\r
             "fontsize": 35,\r
             "coordinates": {\r
-                "left": 245,\r
-                "top": 380\r
+                "left": 250,\r
+                "top": 365\r
             }\r
         }\r
     },\r
@@ -40,7 +40,7 @@
             "fontsize": 35,\r
             "coordinates": {\r
                 "left": 181,\r
-                "top": 380\r
+                "top": 365\r
             }\r
         }\r
     },\r
             },\r
             "coordinates": {\r
                 "left": 180,\r
-                "top": 270\r
+                "top": 30\r
             }\r
         },\r
         "textdisplay": {\r
             "fontsize": 28,\r
             "coordinates": {\r
-                "left": 230,\r
-                "top": 262\r
+                "left": 220,\r
+                "top": 25\r
             }\r
         }\r
     },\r
index 3e4593e..9e18ed2 100755 (executable)
@@ -476,6 +476,7 @@ void UIImageView::OnDraw(BufferInfo& gfxDstBuffer, const Rect& invalidatedArea)
 
 void UIImageView::SetSrc(const char* src)
 {
+    printf("Function: %s, Line: %d\n", __PRETTY_FUNCTION__, __LINE__);
 #ifndef VERSION_LITE
     if (src == nullptr) {
         return;