+++ /dev/null
-#include "uicomponents/UIComponents.h"
-#include "common/text.h"
-#include "common/typed_text.h"
-#include "font/ui_font.h"
-#include <fcntl.h>
-#include <fstream>
-#include <iostream>
-#include <string.h>
-#include <string>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <vector>
-
-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(23);
- 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");
- }
-
- 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 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;
- }
-
- 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());
- }
-
- 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
- }
-
- // 设置标签按钮的位置和大小
- 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;
-}
-
-// 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<const ImageInfo **>(
- 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<const ImageInfo **>(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<const char **>(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<char *>(
- UIMalloc(strlen(buffer) + 1)); // 分配内存来存储每个路径
- if (imagePaths[i] == nullptr) {
- // 如果分配失败,释放已经分配的内存并返回 nullptr
- for (int j = 0; j < i; j++) {
- UIFree(const_cast<char *>(imagePaths[j])); // 释放已分配的内存
- }
- UIFree(imagePaths);
- return nullptr;
- }
- strcpy(const_cast<char *>(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<char *>(imagePaths[i])); // 释放每个路径字符串
- }
- UIFree(const_cast<const char **>(imagePaths)); // 释放整个数组
- }
-}
-/*解析动画资源数组 imageche*/
-std::vector<ImageInfo *> AnalysisArray(const char *filename, uint32_t startId,
- uint32_t lastId) {
- std::vector<ImageInfo *> 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; 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",
- path.c_str());
- }
-}
-
-// 检查文件是否可打开并且不为空
-bool IsFileAccessibleAndNotEmpty(const std::string &filePath) {
- std::ifstream file(filePath);
- return file.is_open() && file.peek() != std::ifstream::traits_type::eof();
-}
-
-} // namespace OHOS
+++ /dev/null
-#ifndef UICOMPONENTS_H
-#define UICOMPONENTS_H
-
-
-#include "View.h"
-#include <string>
-
-// UI 组件
-#include "components/ui_scroll_view.h"
-#include "components/ui_view_group.h"
-#include "components/ui_label.h"
-#include "components/ui_label_ext.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"
-#include "components/icon_adapter.h"
-#include "components/ui_card_page.h"
-#include "common/screen.h"
-// 日志和类型定义
-#include "wearable_log.h"
-#include "ohos_types.h"
-#include "common/image_cache_manager.h"
-
-#ifdef FD230
-#include "traing/include/TraingModel.h"
-#include "ui_resource_string.h"
-#endif
-
-namespace OHOS
-{
- /* -------------------------------------------------------------------------- */
- // 初始化和创建 UI 组件
- // =======================
- /* 初始化 UILabel */
- bool InitLabel(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize);
- bool InitUILabel(UILabel *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize);
-
- /* 初始化 UIButton */
- bool InitButton(UIButton *&button, int left, int top, int width, int height, const char *viewId);
-
- /* 初始化 UILabelButton */
- bool InitLabelButton(UILabelButton *&button, int left, int top, int width, int height, const char *viewId, const char *text, int fontSize);
-
- /* 初始化 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<std::string> &imagePaths, int numFrames, int left, int top, int width, int height, const char *viewId, int time);
-
- /* 自适应 UILabel */
- bool InitLabelAdapt(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize);
-
- // 默认参数的 UILabel 初始化
- void InitLabelDefault(UILabelExt *&label, int x, int y, int width, int height, const char *viewId, const char *text, int fontSize = 30, const char *fontName = DEFAULT_VECTOR_FONT_FILENAME);
- // 滚动
- bool InitRollLabel(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, const char *fontName, int fontSize);
- bool InitLabelExt(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, const char *fontName, int fontSize);
- bool InitButtonExt(UILabelButtonExt *&button, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, int fontSize);
-
- void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, ImageAnimatorInfo *&imageAnimatorInfo, const char **imagePaths, int numFrames, int left, int top, int width, int height, const char *viewId, int time);
-
- void FreeImageInfoArray(const ImageInfo **array, const char *basePath, int numImages, const char *fileExtension = "bin");
-
- // 更新 Label 的文本
- /* -------------------------------------------------------------------------- */
- /* -------------------------------------------------------------------------- */
- // 封装位置和尺寸信息的结构体
- struct GeneralConfig
- {
- int x, y, width, height; // 位置和尺寸
- const char *viewId; // 视图 ID
-
- // 构造函数,允许初始化默认值
- GeneralConfig(int x = 0, int y = 0, int width = 100, int height = 100, const char *viewId = "defaultId")
- : x(x), y(y), width(width), height(height), viewId(viewId) {}
- };
-
- // 封装动画相关配置
- struct ImageAnimatorConfig
- {
- const char **imagePaths; // 图像路径数组
- int numFrames; // 帧数
- int time; // 动画时间
-
- // 构造函数,允许初始化默认值
- ImageAnimatorConfig(const char **paths = nullptr, int frames = 0, int time = 100)
- : imagePaths(paths), numFrames(frames), time(time) {}
- };
-
- // 封装标签相关配置
- struct LabelConfig
- {
- const char *text; // 标签文本
- const char *fontName; // 字体名称
- int fontSize; // 字体大小
-
- // 构造函数,允许初始化默认值
- LabelConfig(const char *text = "", const char *fontName = DEFAULT_VECTOR_FONT_FILENAME, int fontSize = 30)
- : text(text), fontName(fontName), fontSize(fontSize) {}
- };
-
- // 初始化和创建 UI 组件
- // =======================
-
- /**
- * @brief 初始化 UILabel 对象。
- *
- * 此函数检查传入的 UILabel 指针是否为空,若为空则分配内存并设置其相关属性。
- *
- * @param label 要初始化的 UILabel 对象的引用。
- * @param config 带有位置、尺寸和视图 ID 的配置。
- * @param labelConfig 标签的样式和文本配置。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitUILabel(UILabel *&label, const GeneralConfig &config, const LabelConfig &labelConfig);
-
- /**
- * @brief 初始化 UILabelExt 对象。
- *
- * 与 InitUILabel 类似,但用于扩展的 UILabel。
- *
- * @param label 要初始化的 UILabelExt 对象的引用。
- * @param config 配置参数。
- * @param labelConfig 标签的样式和文本配置。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitLabel(UILabelExt *&label, const GeneralConfig &config, const LabelConfig &labelConfig);
-
- /**
- * @brief 初始化可自适应的 UILabelExt 对象。
- *
- * @param label 要初始化的 UILabelExt 对象的引用。
- * @param config 配置参数。
- * @param labelConfig 标签的样式和文本配置。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitLabelAdapt(UILabelExt *&label, const GeneralConfig &config, const LabelConfig &labelConfig);
-
- /**
- * @brief 初始化 UIButton 对象。
- *
- * 此函数将创建 UIButton,并设置其位置、大小和视图 ID。
- *
- * @param button 要初始化的 UIButton 对象的引用。
- * @param config 配置参数(位置、大小、视图 ID)。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitButton(UIButton *&button, const GeneralConfig &config);
-
- /**
- * @brief 初始化 UILabelButton 对象。
- *
- * 这个函数将创建 UILabelButton,设置它的文本、位置和样式。
- *
- * @param button 要初始化的 UILabelButton 对象的引用。
- * @param config 配置参数。
- * @param labelConfig 标签样式和文本配置。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitLabelButton(UILabelButton *&button, const GeneralConfig &config, const LabelConfig &labelConfig);
-
- /**
- * @brief 初始化 UIImageView 对象。
- *
- * 初始化UIImageView并设置位置、大小和视图 ID,同时加载图像。
- *
- * @param imageView 要初始化的 UIImageView 对象的引用。
- * @param config 配置参数。
- * @param imagePath 图像路径。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitImageView(UIImageView *&imageView, const GeneralConfig &config, const char *imagePath);
-
- // 动画资源数组生成
- // =======================
-
- /**
- * @brief 创建动态图像路径数组。
- *
- * 根据给定路径、数量和文件扩展名生成图像路径数组。
- *
- * @param basePath 基础路径。
- * @param numImages 图像数量。
- * @param fileExtension 文件扩展名(默认 "bin")。
- * @return const char** 动态分配的图像路径数组。
- */
- const char **CreateImagePathsArray(const char *basePath, int numImages, const char *fileExtension = "bin");
-
- /**
- * @brief 创建图像信息数组。
- *
- * 根据路径和数量生成图像信息数组。
- *
- * @param basePath 基础路径。
- * @param numImages 图像数量。
- * @param fileExtension 文件扩展名(默认 "bin")。
- * @return const ImageInfo** 动态分配的图像信息数组。
- */
- const ImageInfo **CreateImageInfoArray(const char *basePath, int numImages, const char *fileExtension = "bin");
-
- /**
- * @brief 释放图像信息数组。
- *
- * 释放由 CreateImageInfoArray 创建的图像信息数组的内存。
- *
- * @param array 要释放的图像信息数组。
- * @param numImages 数组长度。
- */
- void FreeImageInfoArray(const ImageInfo **array, int numImages);
-
- /**
- * @brief 释放图像路径数组。
- *
- * 释放由 CreateImagePathsArray 创建的图像路径数组的内存。
- *
- * @param imagePaths 要释放的图像路径数组。
- * @param numImages 数组长度。
- */
- void FreeImageInfoArray(const ImageInfo **imagePaths, int numImages);
-
- void FreeImagePathsArray(const char **imagePaths, int numImages);
- /**
- * @brief 初始化 UIImageAnimatorView,并设置为 GIF 动画。
- *
- * @param imageAnimator 初始化的 UIImageAnimatorView 对象的引用。
- * @param imageAnimatorInfo 储存 GIF 动画信息的数组指针。
- * @param config 动画相关的配置(包括路径和帧信息)。
- * @param generalConfig 视图的基本配置(位置、大小、视图 ID 等)。
- */
- void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, ImageAnimatorInfo *imageAnimatorInfo, const ImageInfo **imageInfo, int numFrames, int left, int top, int width, int height, const char *viewId, int time);
-
- /**
- * @brief 释放 UIImageAnimatorView 的资源。
- *
- * @param imageAnimatorInfo 要释放的动画信息。
- */
- void ReleaseImageAnimatorSource(ImageAnimatorInfo *&imageAnimatorInfo, int numFrames);
-
-#ifdef FD230
- // 扩展功能
- // =======================
- /**
- * @brief 初始化带扩展功能的 UILabelExt 对象。
- *
- * @param label 要初始化的 UILabelExt 对象的引用。
- * @param config 配置参数。
- * @param id 资源文本 ID。
- * @param labelConfig 标签样式和文本配置。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitLabelExt(UILabelExt *&label, const GeneralConfig &config, UIResourceTextId id, const LabelConfig &labelConfig);
-
- /**
- * @brief 初始化带扩展功能的 UILabelButtonExt 对象。
- *
- * @param button 要初始化的 UILabelButtonExt 对象的引用。
- * @param config 配置参数。
- * @param id 资源文本 ID。
- * @param fontSize 字体大小。
- * @return true 初始化成功;false 创建失败。
- */
- bool InitButtonExt(UILabelButtonExt *&button, const GeneralConfig &config, UIResourceTextId id, int fontSize);
-
- /**
- * @brief 更新 UILabelExt 对象的文本。
- *
- * @param label 要更新的 UILabelExt 对象的引用。
- * @param newText 新的文本。
- * @param id 资源文本 ID。
- * @return std::string 更新后的文本。
- */
- std::string UpdateLabelText(UILabelExt *&label, const std::string &newText, UIResourceTextId id);
-
- /**
- * @brief 更新 UILabelExt 对象的文本,允许数字。
- *
- * @param label 要更新的 UILabelExt 对象的引用。
- * @param num 需要更新的数字。
- * @param id 资源文本 ID。
- * @return std::string 更新后的文本。
- */
- std::string UpdateLabel(UILabelExt *&label, int num, UIResourceTextId id);
-
- /**
- * @brief 更新 UILabelExt 对象的文本,允许字符串更新。
- *
- * @param label 要更新的 UILabelExt 对象的引用。
- * @param updatestr 需要更新的字符串。
- * @param id 资源文本 ID。
- * @return std::string 更新后的文本。
- */
- std::string UpdateLabel(UILabelExt *&label, const std::string &updatestr, UIResourceTextId id);
-
- /**
- * @brief 设置 UILabel 的 ID。
- *
- * @param label 要设置 ID 的 UILabel 对象的引用。
- * @param id 资源文本 ID。
- */
- void SetLabelId(UILabel *&label, UIResourceTextId id);
-
- void updata_char(char *str_,char *str,uint32_t max_len);
-
- void CreateDirectoryIfNotExists(const std::string &path);
-
- bool IsFileAccessibleAndNotEmpty(const std::string& filePath);
-
-#endif
-
-} // namespace OHOS
-
-#endif // UICOMPONENTS_H
\ No newline at end of file