From: Caisking <3449750381@qq.com> Date: Fri, 4 Oct 2024 05:50:14 +0000 (+0800) Subject: 系统:增加线条的统一绘制接口 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=712dda6f2a92bbad48acc214e2814fb14e52c67d;p=mirrors%2FFD230.git 系统:增加线条的统一绘制接口 --- diff --git a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp index cfb778daf..3c575f59a 100644 --- a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp +++ b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp @@ -1,925 +1,875 @@ #include "uicomponents/UIComponents.h" -#include -#include -#include #include "common/text.h" #include "common/typed_text.h" #include "font/ui_font.h" -#include -#include -#include -#include -#include -#include -#include - +#include +#include +#include +#include +#include +#include +#include +#include +#include -namespace OHOS -{ +namespace OHOS { // 定义按键颜色宏 #define DEFAULT_BACKGROUND_COLOR 0xff191919 #define PRESSED_BACKGROUND_COLOR 0xff249115 #define PRESSED_BORDER_COLOR 0xff0b1928 - /*** - * @description: 初始化标签 - * @param {UILabel} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @param {char} *text - * @param {char} *fontName - * @param {int} fontSize - * @return - * @brief 初始化标签 - */ - bool InitUILabel(UILabel *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabel(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is not nullptr"); - return false; // 内存分配失败,返回false - } - // 设置标签的位置和大小 - label->SetPosition(left, top); - label->Resize(width, height); - // 设置标签的视图 ID - label->SetViewId(viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, fontSize); - label->SetFont(fontName, fontSize); - // 设置标签的文本和其他属性 - label->SetText(text); - label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - - return true; - } - /*** - * @description: 初始化标签 - * @param {UILabel} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @param {char} *text - * @param {char} *fontName - * @param {int} fontSize - * @return - * @brief 初始化标签 - */ - bool InitRollLabel(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, const char *fontName, int fontSize) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabelExt(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is not nullptr"); - return false; // 内存分配失败,返回false - } - // 设置标签的位置和大小 - label->SetPosition(left, top); - label->Resize(width, height); - // 设置标签的视图 ID - label->SetViewId(viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, fontSize); - label->SetFont(fontName, fontSize); - // 设置标签的文本和其他属性 - label->SetTextId(id); - label->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); - label->SetRollSpeed(70); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - - return true; - } - /*** - * @description: 初始化标签 - * @param {UILabel} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @param {char} *text - * @param {char} *fontName - * @param {int} fontSize - * @return - * @brief 初始化标签 - */ - bool InitLabel(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabelExt(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is not nullptr"); - return false; // 内存分配失败,返回false - } - // 设置标签的位置和大小 - label->SetPosition(left, top); - label->Resize(width, height); - // 设置标签的视图 ID - label->SetViewId(viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, fontSize); - label->SetFont(fontName, fontSize); - // 设置标签的文本和其他属性 - label->SetText(text); - label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - label->SetRollSpeed(0); - - return true; - } - bool InitLabelAdapt(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabelExt(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is not nullptr"); - return false; // 内存分配失败,返回false - } - // 设置标签的位置和大小 - label->SetPosition(left, top); - label->Resize(width, height); - // 设置标签的视图 ID - label->SetViewId(viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, fontSize); - label->SetFont(fontName, fontSize); - // 设置标签的文本和其他属性 - label->SetText(text); - label->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - label->SetRollSpeed(0); - return true; - } +/*** + * @description: 初始化标签 + * @param {UILabel} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {char} *text + * @param {char} *fontName + * @param {int} fontSize + * @return + * @brief 初始化标签 + */ +bool InitUILabel(UILabel *&label, int left, int top, int width, int height, + const char *viewId, const char *text, const char *fontName, + int fontSize) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabel(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is not nullptr"); + return false; // 内存分配失败,返回false + } + // 设置标签的位置和大小 + label->SetPosition(left, top); + label->Resize(width, height); + // 设置标签的视图 ID + label->SetViewId(viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, fontSize); + label->SetFont(fontName, fontSize); + // 设置标签的文本和其他属性 + label->SetText(text); + label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + + return true; +} +/*** + * @description: 初始化标签 + * @param {UILabel} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {char} *text + * @param {char} *fontName + * @param {int} fontSize + * @return + * @brief 初始化标签 + */ +bool InitRollLabel(UILabelExt *&label, int left, int top, int width, int height, + const char *viewId, UIResourceTextId id, + const char *fontName, int fontSize) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabelExt(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is not nullptr"); + return false; // 内存分配失败,返回false + } + // 设置标签的位置和大小 + label->SetPosition(left, top); + label->Resize(width, height); + // 设置标签的视图 ID + label->SetViewId(viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, fontSize); + label->SetFont(fontName, fontSize); + // 设置标签的文本和其他属性 + label->SetTextId(id); + label->SetLineBreakMode(UILabel::LINE_BREAK_OSCILLATION); + label->SetRollSpeed(70); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + + return true; +} +/*** + * @description: 初始化标签 + * @param {UILabel} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {char} *text + * @param {char} *fontName + * @param {int} fontSize + * @return + * @brief 初始化标签 + */ +bool InitLabel(UILabelExt *&label, int left, int top, int width, int height, + const char *viewId, const char *text, const char *fontName, + int fontSize) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabelExt(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is not nullptr"); + return false; // 内存分配失败,返回false + } + // 设置标签的位置和大小 + label->SetPosition(left, top); + label->Resize(width, height); + // 设置标签的视图 ID + label->SetViewId(viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, fontSize); + label->SetFont(fontName, fontSize); + // 设置标签的文本和其他属性 + label->SetText(text); + label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetRollSpeed(0); + + return true; +} +bool InitLabelAdapt(UILabelExt *&label, int left, int top, int width, + int height, const char *viewId, const char *text, + const char *fontName, int fontSize) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabelExt(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is not nullptr"); + return false; // 内存分配失败,返回false + } + // 设置标签的位置和大小 + label->SetPosition(left, top); + label->Resize(width, height); + // 设置标签的视图 ID + label->SetViewId(viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, fontSize); + label->SetFont(fontName, fontSize); + // 设置标签的文本和其他属性 + label->SetText(text); + label->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetRollSpeed(0); + return true; +} #ifdef FD230 - /*** - * @description: - * @param {UILabelExt} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @param {char} *text - * @param {char} *fontName - * @param {int} fontSize - * @return {*} - * @brief 初始化多国语言标签 - */ - bool InitLabelExt(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, const char *fontName, int fontSize) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabelExt(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is not nullptr"); - return false; // 内存分配失败,返回false - } - // 设置标签的位置和大小 - label->SetPosition(left, top); - label->Resize(width, height); - // 设置标签的视图 ID - label->SetViewId(viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, fontSize); - label->SetFont(fontName, fontSize); - // 设置标签的文本和其他属性 - label->SetTextId(id); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); - return true; - } - - /*** - * @description: - * @param {UILabelButtonExt} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @param {UIResourceTextId} id - * @param {int} fontSize - * @return {*} - * @brief 初始化带label的button - */ - bool InitButtonExt(UILabelButtonExt *&button, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, int fontSize) - { - // 创建新按钮(如果当前按钮为空) - if (button == nullptr) - { - button = new UILabelButtonExt(); - if (button == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButtonExt is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UILabelButtonExt UILabelButtonExt is not nullptr"); - return false; // 内存分配失败,返回false - } - button->SetPosition(left, top); - button->Resize(width, height); - button->SetViewId(viewId); - button->SetTextId(id); - button->SetStyle(STYLE_TEXT_FONT, fontSize); - button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, fontSize); - button->SetStyle(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR); - button->SetStyleForState(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR, UIButton::PRESSED); - button->SetStyleForState(STYLE_BORDER_COLOR, PRESSED_BORDER_COLOR, UIButton::PRESSED); - button->SetVisible(true); - // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is succeed"); - return true; - } - /*** - * @description: - * @param {UILabelButton} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @param {UIResourceTextId} id - * @param {int} fontSize - * @return {*} - * @brief 初始化带label的button - */ - bool InitLabelButton(UILabelButton *&button, int left, int top, int width, int height, const char *viewId, const char *text, int fontSize) - { - // 创建新按钮(如果当前按钮为空) - if (button == nullptr) - { - button = new UILabelButton(); - if (button == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButtonExt is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UILabelButton UILabelButton is not nullptr"); - return false; // 内存分配失败,返回false - } - button->SetPosition(left, top); - button->Resize(width, height); - button->SetViewId(viewId); - button->SetText(text); - button->SetStyle(STYLE_TEXT_FONT, fontSize); - button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, fontSize); - button->SetStyle(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR); - button->SetStyleForState(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR, UIButton::PRESSED); - button->SetStyleForState(STYLE_BORDER_COLOR, PRESSED_BORDER_COLOR, UIButton::PRESSED); - button->SetVisible(true); - return true; - } - /* -------------------------------------------------------------------------- */ - /*替换字符串中的变量*/ - std::string UpdateLabelText(UILabelExt *&label, const std::string &newText, UIResourceTextId id) - { - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UpdateLabelText label is nullptr"); - return nullptr; - } - /*取Id对应的字符串*/ - UILabelExt *labelext = new UILabelExt(); - labelext->SetTextId(id); - std::string text = labelext->GetText(); - - printf("UpdateLabel:%s->%s\r\n", text.c_str(), newText.c_str()); - - // 找到标志"XX"在字符串中的位置 - size_t pos = text.find("XX"); - if (pos != std::string::npos) - { - // 替换标志"XX"为更新字符串 - text.replace(pos, 2, newText); // 2 是标志"XX"的长度 - printf("newText:%s\r\n", text.c_str()); - label->SetText(text.c_str()); - } - else - { - printf("找不到变量标志位XX,请注意是否已经被修改!\r\n"); - } +/*** + * @description: + * @param {UILabelExt} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {char} *text + * @param {char} *fontName + * @param {int} fontSize + * @return {*} + * @brief 初始化多国语言标签 + */ +bool InitLabelExt(UILabelExt *&label, int left, int top, int width, int height, + const char *viewId, UIResourceTextId id, const char *fontName, + int fontSize) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabelExt(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is not nullptr"); + return false; // 内存分配失败,返回false + } + // 设置标签的位置和大小 + label->SetPosition(left, top); + label->Resize(width, height); + // 设置标签的视图 ID + label->SetViewId(viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, fontSize); + label->SetFont(fontName, fontSize); + // 设置标签的文本和其他属性 + label->SetTextId(id); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + return true; +} - delete labelext; - return text; - } - /*更新数字*/ - std::string UpdateLabel(UILabelExt *&label, int num, UIResourceTextId id) - { - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error"); - return nullptr; - } - std::string str = UpdateLabelText(label, std::to_string(num), id); - return str; - } +/*** + * @description: + * @param {UILabelButtonExt} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {UIResourceTextId} id + * @param {int} fontSize + * @return {*} + * @brief 初始化带label的button + */ +bool InitButtonExt(UILabelButtonExt *&button, int left, int top, int width, + int height, const char *viewId, UIResourceTextId id, + int fontSize) { + // 创建新按钮(如果当前按钮为空) + if (button == nullptr) { + button = new UILabelButtonExt(); + if (button == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButtonExt is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "UILabelButtonExt UILabelButtonExt is not nullptr"); + return false; // 内存分配失败,返回false + } + button->SetPosition(left, top); + button->Resize(width, height); + button->SetViewId(viewId); + button->SetTextId(id); + button->SetStyle(STYLE_TEXT_FONT, fontSize); + button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, fontSize); + button->SetStyle(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR); + button->SetStyleForState(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR, + UIButton::PRESSED); + button->SetStyleForState(STYLE_BORDER_COLOR, PRESSED_BORDER_COLOR, + UIButton::PRESSED); + button->SetVisible(true); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is succeed"); + return true; +} +/*** + * @description: + * @param {UILabelButton} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {UIResourceTextId} id + * @param {int} fontSize + * @return {*} + * @brief 初始化带label的button + */ +bool InitLabelButton(UILabelButton *&button, int left, int top, int width, + int height, const char *viewId, const char *text, + int fontSize) { + // 创建新按钮(如果当前按钮为空) + if (button == nullptr) { + button = new UILabelButton(); + if (button == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButtonExt is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "UILabelButton UILabelButton is not nullptr"); + return false; // 内存分配失败,返回false + } + button->SetPosition(left, top); + button->Resize(width, height); + button->SetViewId(viewId); + button->SetText(text); + button->SetStyle(STYLE_TEXT_FONT, fontSize); + button->SetFont(DEFAULT_VECTOR_FONT_FILENAME, fontSize); + button->SetStyle(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR); + button->SetStyleForState(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR, + UIButton::PRESSED); + button->SetStyleForState(STYLE_BORDER_COLOR, PRESSED_BORDER_COLOR, + UIButton::PRESSED); + button->SetVisible(true); + return true; +} +/* -------------------------------------------------------------------------- */ +/*替换字符串中的变量*/ +std::string UpdateLabelText(UILabelExt *&label, const std::string &newText, + UIResourceTextId id) { + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "UpdateLabelText label is nullptr"); + return nullptr; + } + /*取Id对应的字符串*/ + UILabelExt *labelext = new UILabelExt(); + labelext->SetTextId(id); + std::string text = labelext->GetText(); + + printf("UpdateLabel:%s->%s\r\n", text.c_str(), newText.c_str()); + + // 找到标志"XX"在字符串中的位置 + size_t pos = text.find("XX"); + if (pos != std::string::npos) { + // 替换标志"XX"为更新字符串 + text.replace(pos, 2, newText); // 2 是标志"XX"的长度 + printf("newText:%s\r\n", text.c_str()); + label->SetText(text.c_str()); + } else { + printf("找不到变量标志位XX,请注意是否已经被修改!\r\n"); + } + + delete labelext; + return text; +} +/*更新数字*/ +std::string UpdateLabel(UILabelExt *&label, int num, UIResourceTextId id) { + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error"); + return nullptr; + } + std::string str = UpdateLabelText(label, std::to_string(num), id); + return str; +} - /*更新文本*/ - std::string UpdateLabel(UILabelExt *&label, const std::string &updateStr, UIResourceTextId id) - { - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error"); - return nullptr; - } +/*更新文本*/ +std::string UpdateLabel(UILabelExt *&label, const std::string &updateStr, + UIResourceTextId id) { + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error"); + return nullptr; + } - std::string str = UpdateLabelText(label, updateStr, id); - return str; - } + std::string str = UpdateLabelText(label, updateStr, id); + return str; +} - /*设置label的Id*/ - void SetLabelId(UILabel *&label, UIResourceTextId id) - { - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error"); - return; - } +/*设置label的Id*/ +void SetLabelId(UILabel *&label, UIResourceTextId id) { + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "label is error"); + return; + } - UILabelExt *labelext = new UILabelExt(); - labelext->SetTextId(id); + UILabelExt *labelext = new UILabelExt(); + labelext->SetTextId(id); - const std::string text = labelext->GetText(); - if (!text.empty()) - { - printf("SetLabelId:%s\r\n", text.c_str()); - label->SetText(text.c_str()); - } + const std::string text = labelext->GetText(); + if (!text.empty()) { + printf("SetLabelId:%s\r\n", text.c_str()); + label->SetText(text.c_str()); + } - delete labelext; - } + delete labelext; +} #endif - /*** - * @description: - * @param {UIButton} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @return - * @brief 初始化按钮 - */ - bool InitButton(UIButton *&button, int left, int top, int width, int height, const char *viewId) - { - // 创建新按钮(如果当前按钮为空) - if (button == nullptr) - { - button = new UIButton(); - if (button == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "new UIButton fail"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is not nullptr"); - return false; // 内存分配失败,返回false - } - button->SetPosition(left, top); - button->Resize(width, height); - button->SetViewId(viewId); - button->SetStyle(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR); - button->SetStyleForState(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR, UIButton::PRESSED); - button->SetStyleForState(STYLE_BORDER_COLOR, PRESSED_BORDER_COLOR, UIButton::PRESSED); - return true; - } - - /*** - * @description: - * @param {UIImageView} * - * @param {int} left - * @param {int} top - * @param {int} width - * @param {int} height - * @param {char} *viewId - * @param {char} *imagePath - * @return {*} - * @brief 初始化图片 - */ - bool InitImageView(UIImageView *&imageView, int left, int top, int width, int height, const char *viewId, const char *imagePath) - { - if (imagePath == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "imagePath is fail"); - return false; - } - // 创建新图片(如果当前图片为空) - if (imageView == nullptr) - { - imageView = new UIImageView(); - if (imageView == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "imagePath %s fail", imagePath); - return false; - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is not nullptr"); - return false; // 内存分配失败,返回false - } - - imageView->SetPosition(left, top); - imageView->SetAutoEnable(true); - imageView->SetViewId(viewId); - - ImageInfo *image = ImageCacheManager::GetInstance().LoadSingleRes(imagePath, false); - if (image == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadAllDialFromRam loadimage %s fail", - imagePath); - return false; - } - imageView->SetSrc(image); - return true; - } - /*带默认参数的初始化label*/ - void InitLabelDefault(UILabelExt *&label, int x, int y, int width, int height, const char *viewId, const char *text, int fontSize, const char *fontName) - { - InitLabel(label, x, y, width, height, viewId, text, fontName, fontSize); - } - void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, ImageAnimatorInfo *&imageAnimatorInfo, const ImageInfo **imageInfo, int numFrames, int left, int top, int width, int height, const char *viewId, int time) - { - if (imageAnimator == nullptr) - { - imageAnimator = new UIImageAnimatorView(); - } - imageAnimator->SetPosition(left, top); - imageAnimator->Resize(width, height); - imageAnimator->SetViewId(viewId); - // imageAnimator->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff); - imageAnimator->SetStyle(STYLE_BACKGROUND_OPA, 0xff); - - imageAnimatorInfo = new ImageAnimatorInfo[numFrames]; - for (int i = 0; i < numFrames; i++) - { - imageAnimatorInfo[i].imageInfo = imageInfo[i]; - imageAnimatorInfo[i].pos.x = left; - imageAnimatorInfo[i].pos.y = top; - imageAnimatorInfo[i].width = width; - imageAnimatorInfo[i].height = height; - imageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO; - } - - imageAnimator->SetImageAnimatorSrc(imageAnimatorInfo, numFrames, time); // 200 is the default delay time - imageAnimator->SetTimeOfPause(time); - imageAnimator->SetRepeat(true); - } - void ReleaseImageAnimatorSource(ImageAnimatorInfo *&imageAnimatorInfo, int numFrames) - { - if (imageAnimatorInfo) - { - for (int i = 0; i < numFrames; i++) - { - delete[] imageAnimatorInfo[i].imagePath; // 释放每个 imagePath 的内存 - } - delete[] imageAnimatorInfo; // 释放 ImageAnimatorInfo 数组的内存 - imageAnimatorInfo = nullptr; // 避免悬挂指针 - } - } - /* -------------------------------------------------------------------------- */ - // UILabel 初始化 - bool InitUILabel(UILabel *&label, const GeneralConfig &config, const LabelConfig &labelConfig) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabel(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is not nullptr"); - return false; // 标签已存在,返回false - } - - // 设置标签的位置和大小 - label->SetPosition(config.x, config.y); - label->Resize(config.width, config.height); - // 设置标签的视图 ID - label->SetViewId(config.viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); - label->SetFont(labelConfig.fontName, labelConfig.fontSize); - // 设置标签的文本和其他属性 - label->SetText(labelConfig.text); - label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - - return true; - } - - // UILabelExt 初始化 - bool InitLabel(UILabelExt *&label, const GeneralConfig &config, const LabelConfig &labelConfig) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabelExt(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is not nullptr"); - return false; // 标签已存在,返回false - } - - // 设置标签的位置和大小 - label->SetPosition(config.x, config.y); - label->Resize(config.width, config.height); - // 设置标签的视图 ID - label->SetViewId(config.viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); - label->SetFont(labelConfig.fontName, labelConfig.fontSize); - // 设置标签的文本和其他属性 - label->SetText(labelConfig.text); - label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - - return true; - } - - // 自适应 UILabel 初始化 - bool InitLabelAdapt(UILabelExt *&label, const GeneralConfig &config, const LabelConfig &labelConfig) - { - // 检查标签指针是否为空 - if (label == nullptr) - { - label = new UILabelExt(); - if (label == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is not nullptr"); - return false; // 标签已存在,返回false - } - - // 设置标签的位置和大小 - label->SetPosition(config.x, config.y); - label->Resize(config.width, config.height); - // 设置标签的视图 ID - label->SetViewId(config.viewId); - // 设置标签的样式 - label->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); - label->SetFont(labelConfig.fontName, labelConfig.fontSize); - // 设置标签的文本和其他属性 - label->SetText(labelConfig.text); - label->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); // 可以使用剪裁模式 - label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); - - return true; - } - // UIButton 初始化 - bool InitButton(UIButton *&button, const GeneralConfig &config) - { - // 检查按钮指针是否为空 - if (button == nullptr) - { - button = new UIButton(); - if (button == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is not nullptr"); - return false; // 按钮已存在,返回false - } - - // 设置按钮的位置和大小 - button->SetPosition(config.x, config.y); - button->Resize(config.width, config.height); - // 设置按钮的视图 ID - button->SetViewId(config.viewId); - - return true; - } - - // UILabelButton 初始化 - bool InitLabelButton(UILabelButton *&button, const GeneralConfig &config, const LabelConfig &labelConfig) - { - // 检查标签按钮指针是否为空 - if (button == nullptr) - { - button = new UILabelButton(); - if (button == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelButton is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelButton is not nullptr"); - return false; // 标签按钮已存在,返回false - } +/*** + * @description: + * @param {UIButton} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @return + * @brief 初始化按钮 + */ +bool InitButton(UIButton *&button, int left, int top, int width, int height, + const char *viewId) { + // 创建新按钮(如果当前按钮为空) + if (button == nullptr) { + button = new UIButton(); + if (button == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "new UIButton fail"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is not nullptr"); + return false; // 内存分配失败,返回false + } + button->SetPosition(left, top); + button->Resize(width, height); + button->SetViewId(viewId); + button->SetStyle(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR); + button->SetStyleForState(STYLE_BACKGROUND_COLOR, DEFAULT_BACKGROUND_COLOR, + UIButton::PRESSED); + button->SetStyleForState(STYLE_BORDER_COLOR, PRESSED_BORDER_COLOR, + UIButton::PRESSED); + return true; +} - // 设置标签按钮的位置和大小 - button->SetPosition(config.x, config.y); - button->Resize(config.width, config.height); - // 设置标签按钮的视图 ID - button->SetViewId(config.viewId); - // 设置标签按钮的样式 - button->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); - button->SetFont(labelConfig.fontName, labelConfig.fontSize); - // 设置标签按钮的文本 - button->SetText(labelConfig.text); - - return true; - } +/*** + * @description: + * @param {UIImageView} * + * @param {int} left + * @param {int} top + * @param {int} width + * @param {int} height + * @param {char} *viewId + * @param {char} *imagePath + * @return {*} + * @brief 初始化图片 + */ +bool InitImageView(UIImageView *&imageView, int left, int top, int width, + int height, const char *viewId, const char *imagePath) { + if (imagePath == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "imagePath is fail"); + return false; + } + // 创建新图片(如果当前图片为空) + if (imageView == nullptr) { + imageView = new UIImageView(); + if (imageView == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "imagePath %s fail", imagePath); + return false; + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is not nullptr"); + return false; // 内存分配失败,返回false + } + + imageView->SetPosition(left, top); + imageView->SetAutoEnable(true); + imageView->SetViewId(viewId); + + ImageInfo *image = + ImageCacheManager::GetInstance().LoadSingleRes(imagePath, false); + if (image == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "LoadAllDialFromRam loadimage %s fail", imagePath); + return false; + } + imageView->SetSrc(image); + return true; +} +/*带默认参数的初始化label*/ +void InitLabelDefault(UILabelExt *&label, int x, int y, int width, int height, + const char *viewId, const char *text, int fontSize, + const char *fontName) { + InitLabel(label, x, y, width, height, viewId, text, fontName, fontSize); +} +void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, + ImageAnimatorInfo *&imageAnimatorInfo, + const ImageInfo **imageInfo, int numFrames, + int left, int top, int width, int height, + const char *viewId, int time) { + if (imageAnimator == nullptr) { + imageAnimator = new UIImageAnimatorView(); + } + imageAnimator->SetPosition(left, top); + imageAnimator->Resize(width, height); + imageAnimator->SetViewId(viewId); + // imageAnimator->SetStyle(STYLE_BACKGROUND_COLOR, 0xffffffff); + imageAnimator->SetStyle(STYLE_BACKGROUND_OPA, 0xff); + + imageAnimatorInfo = new ImageAnimatorInfo[numFrames]; + for (int i = 0; i < numFrames; i++) { + imageAnimatorInfo[i].imageInfo = imageInfo[i]; + imageAnimatorInfo[i].pos.x = left; + imageAnimatorInfo[i].pos.y = top; + imageAnimatorInfo[i].width = width; + imageAnimatorInfo[i].height = height; + imageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO; + } + + imageAnimator->SetImageAnimatorSrc(imageAnimatorInfo, numFrames, + time); // 200 is the default delay time + imageAnimator->SetTimeOfPause(time); + imageAnimator->SetRepeat(true); +} +void ReleaseImageAnimatorSource(ImageAnimatorInfo *&imageAnimatorInfo, + int numFrames) { + if (imageAnimatorInfo) { + for (int i = 0; i < numFrames; i++) { + delete[] imageAnimatorInfo[i].imagePath; // 释放每个 imagePath 的内存 + } + delete[] imageAnimatorInfo; // 释放 ImageAnimatorInfo 数组的内存 + imageAnimatorInfo = nullptr; // 避免悬挂指针 + } +} +/* -------------------------------------------------------------------------- */ +// UILabel 初始化 +bool InitUILabel(UILabel *&label, const GeneralConfig &config, + const LabelConfig &labelConfig) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabel(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitUILabel is not nullptr"); + return false; // 标签已存在,返回false + } + + // 设置标签的位置和大小 + label->SetPosition(config.x, config.y); + label->Resize(config.width, config.height); + // 设置标签的视图 ID + label->SetViewId(config.viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); + label->SetFont(labelConfig.fontName, labelConfig.fontSize); + // 设置标签的文本和其他属性 + label->SetText(labelConfig.text); + label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + + return true; +} - // UIImageView 初始化 - bool InitImageView(UIImageView *&imageView, const GeneralConfig &config, const char *imagePath) - { - // 检查图片视图指针是否为空 - if (imageView == nullptr) - { - imageView = new UIImageView(); - if (imageView == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is error"); - return false; // 内存分配失败,返回false - } - } - else - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is not nullptr"); - return false; // 图片视图已存在,返回false - } +// UILabelExt 初始化 +bool InitLabel(UILabelExt *&label, const GeneralConfig &config, + const LabelConfig &labelConfig) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabelExt(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabel is not nullptr"); + return false; // 标签已存在,返回false + } + + // 设置标签的位置和大小 + label->SetPosition(config.x, config.y); + label->Resize(config.width, config.height); + // 设置标签的视图 ID + label->SetViewId(config.viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); + label->SetFont(labelConfig.fontName, labelConfig.fontSize); + // 设置标签的文本和其他属性 + label->SetText(labelConfig.text); + label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + + return true; +} - // 设置图片视图的位置和大小 - imageView->SetPosition(config.x, config.y); - imageView->Resize(config.width, config.height); - // 设置图片视图的视图 ID - imageView->SetViewId(config.viewId); - // 设置图片的路径 - imageView->SetSrc(imagePath); +// 自适应 UILabel 初始化 +bool InitLabelAdapt(UILabelExt *&label, const GeneralConfig &config, + const LabelConfig &labelConfig) { + // 检查标签指针是否为空 + if (label == nullptr) { + label = new UILabelExt(); + if (label == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelAdapt is not nullptr"); + return false; // 标签已存在,返回false + } + + // 设置标签的位置和大小 + label->SetPosition(config.x, config.y); + label->Resize(config.width, config.height); + // 设置标签的视图 ID + label->SetViewId(config.viewId); + // 设置标签的样式 + label->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); + label->SetFont(labelConfig.fontName, labelConfig.fontSize); + // 设置标签的文本和其他属性 + label->SetText(labelConfig.text); + label->SetLineBreakMode(UILabel::LINE_BREAK_ADAPT); // 可以使用剪裁模式 + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + + return true; +} +// UIButton 初始化 +bool InitButton(UIButton *&button, const GeneralConfig &config) { + // 检查按钮指针是否为空 + if (button == nullptr) { + button = new UIButton(); + if (button == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is not nullptr"); + return false; // 按钮已存在,返回false + } + + // 设置按钮的位置和大小 + button->SetPosition(config.x, config.y); + button->Resize(config.width, config.height); + // 设置按钮的视图 ID + button->SetViewId(config.viewId); + + return true; +} - return true; - } - /* -------------------------------------------------------------------------- */ - /** - * 创建包含图片文件路径的数组 - * @param basePath 包含图片文件的基本路径 PullUp_%d.bin - * @param fileExtension 图片文件的扩展名 - * @param numImages 图片文件的数量 - * @return 返回包含图片文件info的数组 - */ - const ImageInfo **CreateImageInfoArray(const char *basePath, int numImages, const char *fileExtension) - { - const ImageInfo **imageinfo_array = static_cast(UIMalloc(numImages * sizeof(const ImageInfo *))); - if (imageinfo_array == nullptr) - { - return nullptr; // 检查内存分配是否成功 - } - for (int i = 0; i < numImages; i++) - { - char buffer[256] = {0}; - snprintf(buffer, sizeof(buffer), "%s%d.%s", basePath, i + 1, fileExtension); - imageinfo_array[i] = ImageCacheManager::GetInstance().LoadSingleRes(buffer); - // imageinfo_array[i] = ImageCacheManager::GetInstance().LoadSingleRes(buffer); - // if (imageinfo_array[i] == nullptr) - // { - // UIFree(imageinfo_array); - // return nullptr; // 检查内存分配是否成功 - // } - } - return imageinfo_array; - } - /** - * 释放创建的图片路径数组 - * @param imagePaths 包含图片路径的数组 - * @param numImages 图片文件的数量 - */ - void FreeImageInfoArray(const ImageInfo **imagePaths, int numImages) - { - if (imagePaths != nullptr) - { - for (int i = 0; i < numImages; i++) - { - ImageCacheManager::GetInstance().UnloadSingleRes((char *)imagePaths[i]); - } - UIFree(const_cast(imagePaths)); // 释放整个数组 - } - } - void FreeImageInfoArray(const ImageInfo **array, const char *basePath, int numImages, const char *fileExtension) - { - for (int i = 0; i < numImages; i++) - { - char buffer[256] = {0}; - snprintf(buffer, sizeof(buffer), "%s%d.%s", basePath, i + 1, fileExtension); - ImageCacheManager::GetInstance().UnloadSingleRes(buffer); - } - UIFree(array); - return; - } - /* -------------------------------------------------------------------------- */ - /** - * 创建包含图片文件路径的数组 - * @param basePath 包含图片文件的基本路径 - * @param fileExtension 图片文件的扩展名 - * @param numImages 图片文件的数量 - * @return 返回包含图片文件路径的数组 - */ - const char **CreateImagePathsArray(const char *basePath, int numImages, const char *fileExtension) - { - const char **imagePaths = static_cast(UIMalloc(numImages * sizeof(const char *))); - if (imagePaths == nullptr) - { - return nullptr; // 检查内存分配是否成功 - } +// UILabelButton 初始化 +bool InitLabelButton(UILabelButton *&button, const GeneralConfig &config, + const LabelConfig &labelConfig) { + // 检查标签按钮指针是否为空 + if (button == nullptr) { + button = new UILabelButton(); + if (button == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelButton is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelButton is not nullptr"); + return false; // 标签按钮已存在,返回false + } + + // 设置标签按钮的位置和大小 + button->SetPosition(config.x, config.y); + button->Resize(config.width, config.height); + // 设置标签按钮的视图 ID + button->SetViewId(config.viewId); + // 设置标签按钮的样式 + button->SetStyle(STYLE_TEXT_FONT, labelConfig.fontSize); + button->SetFont(labelConfig.fontName, labelConfig.fontSize); + // 设置标签按钮的文本 + button->SetText(labelConfig.text); + + return true; +} - char buffer[256]; - for (int i = 0; i < numImages; i++) - { - snprintf(buffer, sizeof(buffer), "%s/%d.%s", basePath, i + 1, fileExtension); - imagePaths[i] = static_cast(UIMalloc(strlen(buffer) + 1)); // 分配内存来存储每个路径 - if (imagePaths[i] == nullptr) - { - // 如果分配失败,释放已经分配的内存并返回 nullptr - for (int j = 0; j < i; j++) - { - UIFree(const_cast(imagePaths[j])); // 释放已分配的内存 - } - UIFree(imagePaths); - return nullptr; - } - strcpy(const_cast(imagePaths[i]), buffer); // 使用 strcpy 拷贝字符串 - } - return imagePaths; - } - /** - * 释放创建的图片路径数组 - * @param imagePaths 包含图片路径的数组 - * @param numImages 图片文件的数量 - */ - void FreeImagePathsArray(const char **imagePaths, int numImages) - { - if (imagePaths != nullptr) - { - for (int i = 0; i < numImages; i++) - { - UIFree(const_cast(imagePaths[i])); // 释放每个路径字符串 - } - UIFree(const_cast(imagePaths)); // 释放整个数组 - } - } - /*解析动画资源数组 imageche*/ - std::vector AnalysisArray(const char *filename, uint32_t startId, uint32_t lastId) - { - std::vector imageArray; - - for (uint32_t i = startId; i <= lastId; i++) - { - ImageInfo *image = ImageCacheManager::GetInstance().LoadOneInMultiRes(i, filename); - if (image == nullptr) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadAppMultiImages %x fail", i); - // 如果加载失败,可以选择继续加载下一个,或者在这里处理其他逻辑 - continue; - } - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "LoadAppMultiImages %x", i); - imageArray.push_back(image); - } - return imageArray; - } +// UIImageView 初始化 +bool InitImageView(UIImageView *&imageView, const GeneralConfig &config, + const char *imagePath) { + // 检查图片视图指针是否为空 + if (imageView == nullptr) { + imageView = new UIImageView(); + if (imageView == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is error"); + return false; // 内存分配失败,返回false + } + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is not nullptr"); + return false; // 图片视图已存在,返回false + } + + // 设置图片视图的位置和大小 + imageView->SetPosition(config.x, config.y); + imageView->Resize(config.width, config.height); + // 设置图片视图的视图 ID + imageView->SetViewId(config.viewId); + // 设置图片的路径 + imageView->SetSrc(imagePath); + + return true; +} +// 创建线条的通用函数 +bool CreateLine(UIImageView *&imageView, int x, int y, int width, + int height, const char *viewId) { + if (imageView == nullptr) { + imageView = new UIImageView(); + if (imageView == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "CreateLine is error"); + return false; + } + } + + // 设置线条的位置和大小 + imageView->SetPosition(x, y); + imageView->Resize(width, height); // 线高 = height, 线宽 = width + + // 设置线条的样式(背景颜色等) + imageView->SetStyle(STYLE_BACKGROUND_COLOR, 0xff333333); // 设置线条颜色 + imageView->SetStyle(STYLE_BACKGROUND_OPA, 0xff); // 设置线条透明度 + imageView->SetViewId(viewId); // 设置视图 ID + imageView->SetVisible(true); // 显示线条 + + return true; +} +/* -------------------------------------------------------------------------- */ +/** + * 创建包含图片文件路径的数组 + * @param basePath 包含图片文件的基本路径 PullUp_%d.bin + * @param fileExtension 图片文件的扩展名 + * @param numImages 图片文件的数量 + * @return 返回包含图片文件info的数组 + */ +const ImageInfo **CreateImageInfoArray(const char *basePath, int numImages, + const char *fileExtension) { + const ImageInfo **imageinfo_array = static_cast( + UIMalloc(numImages * sizeof(const ImageInfo *))); + if (imageinfo_array == nullptr) { + return nullptr; // 检查内存分配是否成功 + } + for (int i = 0; i < numImages; i++) { + char buffer[256] = {0}; + snprintf(buffer, sizeof(buffer), "%s%d.%s", basePath, i + 1, fileExtension); + imageinfo_array[i] = ImageCacheManager::GetInstance().LoadSingleRes(buffer); + // imageinfo_array[i] = + // ImageCacheManager::GetInstance().LoadSingleRes(buffer); if + // (imageinfo_array[i] == nullptr) + // { + // UIFree(imageinfo_array); + // return nullptr; // 检查内存分配是否成功 + // } + } + return imageinfo_array; +} +/** + * 释放创建的图片路径数组 + * @param imagePaths 包含图片路径的数组 + * @param numImages 图片文件的数量 + */ +void FreeImageInfoArray(const ImageInfo **imagePaths, int numImages) { + if (imagePaths != nullptr) { + for (int i = 0; i < numImages; i++) { + ImageCacheManager::GetInstance().UnloadSingleRes((char *)imagePaths[i]); + } + UIFree(const_cast(imagePaths)); // 释放整个数组 + } +} +void FreeImageInfoArray(const ImageInfo **array, const char *basePath, + int numImages, const char *fileExtension) { + for (int i = 0; i < numImages; i++) { + char buffer[256] = {0}; + snprintf(buffer, sizeof(buffer), "%s%d.%s", basePath, i + 1, fileExtension); + ImageCacheManager::GetInstance().UnloadSingleRes(buffer); + } + UIFree(array); + return; +} +/* -------------------------------------------------------------------------- */ +/** + * 创建包含图片文件路径的数组 + * @param basePath 包含图片文件的基本路径 + * @param fileExtension 图片文件的扩展名 + * @param numImages 图片文件的数量 + * @return 返回包含图片文件路径的数组 + */ +const char **CreateImagePathsArray(const char *basePath, int numImages, + const char *fileExtension) { + const char **imagePaths = + static_cast(UIMalloc(numImages * sizeof(const char *))); + if (imagePaths == nullptr) { + return nullptr; // 检查内存分配是否成功 + } + + char buffer[256]; + for (int i = 0; i < numImages; i++) { + snprintf(buffer, sizeof(buffer), "%s/%d.%s", basePath, i + 1, + fileExtension); + imagePaths[i] = static_cast( + UIMalloc(strlen(buffer) + 1)); // 分配内存来存储每个路径 + if (imagePaths[i] == nullptr) { + // 如果分配失败,释放已经分配的内存并返回 nullptr + for (int j = 0; j < i; j++) { + UIFree(const_cast(imagePaths[j])); // 释放已分配的内存 + } + UIFree(imagePaths); + return nullptr; + } + strcpy(const_cast(imagePaths[i]), buffer); // 使用 strcpy 拷贝字符串 + } + return imagePaths; +} +/** + * 释放创建的图片路径数组 + * @param imagePaths 包含图片路径的数组 + * @param numImages 图片文件的数量 + */ +void FreeImagePathsArray(const char **imagePaths, int numImages) { + if (imagePaths != nullptr) { + for (int i = 0; i < numImages; i++) { + UIFree(const_cast(imagePaths[i])); // 释放每个路径字符串 + } + UIFree(const_cast(imagePaths)); // 释放整个数组 + } +} +/*解析动画资源数组 imageche*/ +std::vector AnalysisArray(const char *filename, uint32_t startId, + uint32_t lastId) { + std::vector imageArray; + + for (uint32_t i = startId; i <= lastId; i++) { + ImageInfo *image = + ImageCacheManager::GetInstance().LoadOneInMultiRes(i, filename); + if (image == nullptr) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "LoadAppMultiImages %x fail", i); + // 如果加载失败,可以选择继续加载下一个,或者在这里处理其他逻辑 + continue; + } + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "LoadAppMultiImages %x", i); + imageArray.push_back(image); + } + return imageArray; +} - void updata_char(char *str_,char *str,uint32_t max_len) - { - strcpy_s(str_,max_len,str); - uint32_t j = 0; - uint32_t letter = 0; - if (str == nullptr) { - return ; - } - while (str[j] != '\0') - { - int i=j; - letter = TypedText::GetUTF8Next(str, j, j); - int len=j-i; - UIFont* fontEngine= UIFont::GetInstance(); - GlyphPathData path; - if(!fontEngine->GetGlyphPathData(letter, 0, path)) - { - printf("********************* letter%d %d %d not exist\r\n",i,j,letter); - char *buf= "□"; - uint16_t buf_len=strlen(buf); - uint16_t offset =0; - int num=len/buf_len; - int mod_num=len%buf_len; - for(int x=0;xGetGlyphPathData(letter, 0, path)) { + printf("********************* letter%d %d %d not exist\r\n", i, j, + letter); + char *buf = "□"; + uint16_t buf_len = strlen(buf); + uint16_t offset = 0; + int num = len / buf_len; + int mod_num = len % buf_len; + for (int x = 0; x < num; x++) { + memcpy_s(&str_[i + offset], buf_len, buf, buf_len); + offset += buf_len; + } + if (mod_num != 0) { + for (int x = 0; x < mod_num; x++) { + str_[i + offset] = ' '; + offset += 1; + } + } + } + } + printf("%s\r\n", str_); +} - /*创建文件夹*/ - void CreateDirectoryIfNotExists(const std::string &path) { - struct stat info; - if (stat(path.c_str(), &info) != 0) { - if (mkdir(path.c_str(), 0755) != 0) { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, - "Error: Could not create directory %s. Errno: %d", - path.c_str(), errno); - } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Created directory: %s", - path.c_str()); - } - } else if (info.st_mode & S_IFDIR) { - WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Directory already exists: %s", +/*创建文件夹*/ +void CreateDirectoryIfNotExists(const std::string &path) { + struct stat info; + if (stat(path.c_str(), &info) != 0) { + if (mkdir(path.c_str(), 0755) != 0) { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, + "Error: Could not create directory %s. Errno: %d", + path.c_str(), errno); + } else { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "Created directory: %s", path.c_str()); } - } + } else if (info.st_mode & S_IFDIR) { + WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "Directory already exists: %s", + path.c_str()); + } +} - // 检查文件是否可打开并且不为空 - bool IsFileAccessibleAndNotEmpty(const std::string& filePath) { - std::ifstream file(filePath); - return file.is_open() && file.peek() != std::ifstream::traits_type::eof(); - } - +// 检查文件是否可打开并且不为空 +bool IsFileAccessibleAndNotEmpty(const std::string &filePath) { + std::ifstream file(filePath); + return file.is_open() && file.peek() != std::ifstream::traits_type::eof(); } + +} // namespace OHOS diff --git a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h index 1b28230ca..c1e7facb9 100644 --- a/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h +++ b/code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h @@ -46,7 +46,8 @@ namespace OHOS /* 初始化 UIImageView */ bool InitImageView(UIImageView *&imageView, int left, int top, int width, int height, const char *viewId, const char *imagePath); - + /*创建线条的通用函数*/ + bool CreateLine(UIImageView *&imageView, int x, int y, int width, int height, const char *viewId); /* 初始化动画资源 */ void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, const std::vector &imagePaths, int numFrames, int left, int top, int width, int height, const char *viewId, int time);