From: Caisking <3449750381@qq.com> Date: Tue, 9 Jan 2024 08:50:57 +0000 (+0800) Subject: 优化指针表盘bug以及数字表盘 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=27512c956a8aca6c6aeb579c17b21d9ebebd59ac;p=mirrors%2FFD230.git 优化指针表盘bug以及数字表盘 --- diff --git a/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h b/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h index ac67d757c..e2a6ef9fa 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h +++ b/code/sdk/application/wearable/nativeapp/nativeui/dial/include/Digital_clockView.h @@ -10,10 +10,6 @@ #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(time(0))); + // srand(static_cast(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 diff --git a/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp b/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp index fe958d375..415f17c8c 100644 --- a/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp +++ b/code/sdk/application/wearable/nativeapp/nativeui/dial/src/Digital_clockView.cpp @@ -14,7 +14,9 @@ #include #include "cJSON.h" #include - +#include +#include // 用于格式化输出 +#include //异常处理 #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; } diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/clocks/KUMI.json b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/clocks/KUMI.json index a6b43f922..23fb79470 100644 --- a/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/clocks/KUMI.json +++ b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/clocks/KUMI.json @@ -5,11 +5,11 @@ "timeformat": "24format", "digitsize": 60, "playformat": "textdisplay", - "fontsize": 50, + "fontsize": 55, "textdisplay": { "coordinates": { - "left": 168, - "top": 310 + "left": 161, + "top": 293 } } }, @@ -19,8 +19,8 @@ "font": "STYLE_TEXT_FONT", "fontsize": 35, "coordinates": { - "left": 245, - "top": 380 + "left": 250, + "top": 365 } } }, @@ -40,7 +40,7 @@ "fontsize": 35, "coordinates": { "left": 181, - "top": 380 + "top": 365 } } }, @@ -52,14 +52,14 @@ }, "coordinates": { "left": 180, - "top": 270 + "top": 30 } }, "textdisplay": { "fontsize": 28, "coordinates": { - "left": 230, - "top": 262 + "left": 220, + "top": 25 } } }, diff --git a/code/sdk/middleware/services/gui/uikit/ui/frameworks/components/ui_image_view.cpp b/code/sdk/middleware/services/gui/uikit/ui/frameworks/components/ui_image_view.cpp index 3e4593e8b..9e18ed2ad 100755 --- a/code/sdk/middleware/services/gui/uikit/ui/frameworks/components/ui_image_view.cpp +++ b/code/sdk/middleware/services/gui/uikit/ui/frameworks/components/ui_image_view.cpp @@ -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;