From: Caisking <3449750381@qq.com> Date: Tue, 23 Apr 2024 08:41:10 +0000 (+0800) Subject: 增加UIComponents文件 X-Git-Tag: release_V1.2.70_20241020_135402~1144 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=6c9a03c8ef918e3fecd13c82d8b82762469c532c;p=mirrors%2FFD230.git 增加UIComponents文件 --- diff --git a/code/sdk/application/wearable/fd230app/CMakeLists.txt b/code/sdk/application/wearable/fd230app/CMakeLists.txt index 499db9233..e90229ac5 100644 --- a/code/sdk/application/wearable/fd230app/CMakeLists.txt +++ b/code/sdk/application/wearable/fd230app/CMakeLists.txt @@ -285,6 +285,9 @@ set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/heart_blood/src/BloodCardView.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/heart_blood/src/HeartCardView.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/heart_blood/health_service/health_service.cpp + + #UI组件初始化 + ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/uicomponents/UIComponents.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/countdown/src/MainCountDownPresenter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/countdown/src/CountDownModel.cpp diff --git a/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp new file mode 100644 index 000000000..125e7864d --- /dev/null +++ b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp @@ -0,0 +1,270 @@ +/*** + * @Date: 2024-04-23 16:04:40 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-04-23 16:11:30 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.cpp + */ + +#include "uicomponents/UIComponents.h" + +namespace OHOS +{ + /*** + * @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) + { + 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); + // 设置标签可见 + label->SetVisible(true); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is succeed"); + return true; + } + + /*** + * @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) + { + 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->SetFontId(); + label->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER); + label->SetLineBreakMode(UILabel::LINE_BREAK_ELLIPSIS); + label->SetRollSpeed(100); + // 设置标签可见 + label->SetVisible(true); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitLabelExt is succeed"); + return true; + } + /*** + * @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) + { + 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); + button->SetVisible(true); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitButton is succeed"); + 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) + { + 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 {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 (imageView == nullptr) + { + imageView = new UIImageView(); + } + if (imageView == nullptr) + { + return false; // 内存分配失败,返回false + } + imageView->SetPosition(left, top); + imageView->SetAutoEnable(true); + // imageView->Resize(width, height); + imageView->SetViewId(viewId); + imageView->SetSrc(imagePath); + imageView->SetVisible(true); + // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "InitImageView is succeed"); + return true; + } + /** + * 创建包含图片文件路径的数组 + * @param basePath 包含图片文件的基本路径 + * @param fileExtension 图片文件的扩展名 + * @param numImages 图片文件的数量 + * @return 返回包含图片文件路径的数组 + */ + const char **CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages) + { + const char **imagePaths = new const char *[numImages]; + char buffer[256]; + for (int i = 0; i < numImages; i++) + { + snprintf(buffer, sizeof(buffer), "%s/%d.%s", basePath, i + 1, fileExtension); + imagePaths[i] = strdup(buffer); + } + return imagePaths; + } + /** + * @brief 初始化UIImageAnimatorView,并设置为GIF动画。 + * + * 该函数用于使用提供的GIF图像和属性初始化UIImageAnimatorView。 + * + * @param imageAnimator 要初始化的UIImageAnimatorView对象的引用。 + * @param paths GIF图像文件路径的数组。 + * @param numFrames GIF中的帧数。 + * @param left 视图的左侧位置。 + * @param top 视图的顶部位置。 + * @param width 视图的宽度。 + * @param height 视图的高度。 + * @param viewId 视图的ID。 + * @param time 视图的帧速 + * @return 如果初始化成功则返回true,否则返回false。 + */ + void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, const char **imagePaths, 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 *imageAnimatorInfo = new ImageAnimatorInfo[numFrames]; + for (int i = 0; i < numFrames; i++) + { + imageAnimatorInfo[i].imagePath = imagePaths[i]; + imageAnimatorInfo[i].pos.x = left; + imageAnimatorInfo[i].pos.y = top; + imageAnimatorInfo[i].width = width; + imageAnimatorInfo[i].height = height; + imageAnimatorInfo[i].imageType = IMG_SRC_FILE_PATH; + } + + imageAnimator->SetImageAnimatorSrc(imageAnimatorInfo, numFrames, time); // 200 is the default delay time + imageAnimator->SetTimeOfPause(time); + imageAnimator->SetRepeatTimes(1); + imageAnimator->SetReverse(true); + // imageAnimator->SetVisible(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); + } +} \ No newline at end of file diff --git a/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h new file mode 100644 index 000000000..1b5d04f5c --- /dev/null +++ b/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h @@ -0,0 +1,50 @@ +/*** + * @Date: 2024-04-23 16:04:31 + * @LastEditors: Caisking 3449750381@qq.com + * @LastEditTime: 2024-04-23 16:07:41 + * @FilePath: /XTS_FD230/FD230/code/sdk/application/wearable/fd230app/nativeui/uicomponents/UIComponents.h + */ +#ifndef UICOMPONENTS_H +#define UICOMPONENTS_H + +#include "BaseView.h" +#include "View.h" +#include +#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 "wearable_log.h" +#include "ohos_types.h" +#include "components/ui_card_page.h" +#include "traing/include/TraingModel.h" +#include "language/font/ui_resource_string.h" + +namespace OHOS +{ + /*初始化label[可使用text]*/ + bool InitLabel(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize); + /*初始化labelExt*/ + bool InitLabelExt(UILabelExt *&label, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, const char *fontName, int fontSize); + /*初始化button*/ + bool InitButton(UIButton *&button, int left, int top, int width, int height, const char *viewId); + /*初始化buttonExt*/ + bool InitButtonExt(UILabelButtonExt *&button, int left, int top, int width, int height, const char *viewId, UIResourceTextId id, int fontSize); + /*初始化image*/ + bool InitImageView(UIImageView *&imageView, int left, int top, int width, int height, const char *viewId, const char *imagePath); + /*生成动画资源数组*/ + const char **CreateImagePathsArray(const char *basePath, const char *fileExtension, int numImages); + /*初始化动画资源*/ + void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, const char **imagePaths, int numFrames, int left, int top, int width, int height, const char *viewId, int time); + /*InitLabel[默认参数]*/ + 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); + +} +#endif // UICOMPONENTS_H \ No newline at end of file