增加组件初始化 UIInitalize 后续持续更新组件初始化函数
authorCaisking <3449750381@qq.com>
Mon, 25 Dec 2023 10:18:09 +0000 (18:18 +0800)
committerCaisking <3449750381@qq.com>
Mon, 25 Dec 2023 10:18:09 +0000 (18:18 +0800)
code/sdk/application/wearable/nativeapp/CMakeLists.txt
code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/ComponentConfig.h [new file with mode: 0644]
code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/UIInitializer.cpp [new file with mode: 0644]
code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/UIInitializer.h [new file with mode: 0644]
code/sdk/application/wearable/nativeapp/nativeui/traing/src/TraingView.cpp

index f9f5ce6..ee69243 100755 (executable)
@@ -37,6 +37,7 @@ set(PRIVATE_HEADER
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/uiservice/include\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/uisample/include\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/\r
+    ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/UIInitializer/\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/InitComponent/include\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/uisample/uitest/framework/include\r
     ${UI_SAMPLE_ROOT}/components\r
@@ -79,6 +80,7 @@ set(SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/uiservice/src/ui_service.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativelauncher/src/slices/main/MainPresenter.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativelauncher/src/slices/main/MainView.cpp\r
+    ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/UIInitializer/UIInitializer.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/main/src/MainViewSample.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/main/src/MainPresenterSample.cpp\r
     ${CMAKE_CURRENT_SOURCE_DIR}/nativeui/main/src/MainSwipeViewGroup.cpp\r
diff --git a/code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/ComponentConfig.h b/code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/ComponentConfig.h
new file mode 100644 (file)
index 0000000..2711dd8
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Description: nativeui
+ * Author:Caiking
+ * Create: 2023-12-25
+ */
+
+// ComponentConfig.h
+#ifndef COMPONENT_CONFIG_H
+#define COMPONENT_CONFIG_H
+
+#include <string>
+/**
+ * @brief UI元素的通用属性
+ */
+struct UIgenerality
+{
+    int left;           ///< 元素的左边界位置
+    int top;            ///< 元素的顶部位置
+    int width;          ///< 元素的宽度
+    int height;         ///< 元素的高度
+    const char *viewId; ///< 元素的视图ID
+};
+
+/**
+ * @brief 标签结构体
+ */
+struct UILabelConfig : public UIgenerality
+{
+    const char *text;     ///< 标签显示的文本
+    const char *fontName; ///< 标签的字体名称
+    int fontSize;         ///< 标签的字体大小
+
+    UILabelConfig(int l, int t, int w, int h, const char *id, const char *txt, const char *fName, int fSize)
+        : UIgenerality{l, t, w, h, id}, text(txt), fontName(fName), fontSize(fSize) {}
+};
+
+/**
+ * @brief 按键结构体
+ */
+struct UIButtonConfig : public UIgenerality
+{
+    UIButtonConfig(int l, int t, int w, int h, const char *id)
+        : UIgenerality{l, t, w, h, id} {}
+};
+
+/**
+ * @brief 图片结构体
+ */
+struct UIImageConfig : public UIgenerality
+{
+    const char *imagePath; ///< 图片的文件路径
+
+    UIImageConfig(int l, int t, int w, int h, const char *id, const char *path)
+        : UIgenerality{l, t, w, h, id}, imagePath(path) {}
+};
+#endif
\ No newline at end of file
diff --git a/code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/UIInitializer.cpp b/code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/UIInitializer.cpp
new file mode 100644 (file)
index 0000000..a81a6e0
--- /dev/null
@@ -0,0 +1,60 @@
+
+#include "common/image_cache_manager.h"
+#include "UiConfig.h"
+#include "ui_resource_image.h"
+#include <ui_scroll_view.h>
+#include "components/ui_button.h"
+#include "UIInitializer.h"
+
+namespace OHOS
+{
+    /**
+     * @brief 初始化按键
+     * @param label  要初始化的标签对象的引用
+     * @param labelconfig  包含标签配置信息的对象
+     * @return  如果初始化成功返回 true,否则返回 false
+     */
+    void UIInitializer::InitLabel(UILabel *&label, const UILabelConfig &labelconfig)
+    {
+        // 检查标签指针是否为空
+        if (label == nullptr)
+        {
+            label = new UILabel();
+        }
+        // 设置标签的位置和大小
+        label->SetPosition(labelconfig.left, labelconfig.top);
+        label->Resize(labelconfig.width, labelconfig.height);
+        // 设置标签的视图 ID
+        label->SetViewId(labelconfig.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);
+        label->SetRollSpeed(0);
+        // 设置标签可见
+        label->SetVisible(true);
+    }
+
+    /**
+     * @brief 初始化按键
+     * @param button  要初始化的按键对象的引用
+     * @param buttonconfig  包含按键配置信息的对象
+     * @return  如果初始化成功返回 true,否则返回 false
+     */
+    void UIInitializer::InitButton(UIButton *&button, const UIButtonConfig &buttonconfig)
+    {
+    }
+
+    /**
+     * @brief 初始化图像视图
+     * @param imageview  要初始化的图像视图对象的引用
+     * @param imageconfig  包含图像视图配置信息的对象
+     * @return  如果初始化成功返回 true,否则返回 false
+     */
+    void UIInitializer::InitImageView(UIImageView *&imageview, const UIImageConfig &imageconfig)
+    {
+    }
+}
\ No newline at end of file
diff --git a/code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/UIInitializer.h b/code/sdk/application/wearable/nativeapp/nativeui/UIInitializer/UIInitializer.h
new file mode 100644 (file)
index 0000000..6e2e8a7
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Description: nativeui
+ * Author:Caiking
+ * Create: 2023-12-25
+ */
+
+// UIInitializer.h
+#ifndef UI_INITIALIZER_H
+#define UI_INITIALIZER_H
+
+#include "ComponentConfig.h" // 包含组件配置结构体的头文件
+#include "View.h"
+#include <ui_scroll_view.h>
+#include "components/ui_view_group.h"
+#include "components/ui_label.h"
+#include "components/ui_image_view.h"
+#include "components/ui_button.h"
+#include "components/ui_image_animator.h"
+#include "components/ui_label_button_ext.h"
+#include "components/ui_chart.h"
+#include "components/ui_list.h"
+#include "components/icon_adapter.h"
+#include "wearable_log.h"
+#include "ohos_types.h"
+#include "components/ui_card_page.h"
+
+namespace OHOS
+{
+
+    class UIInitializer
+    {
+    public:
+        static void InitLabel(UILabel *&label, const UILabelConfig &labelconfig);
+        static void InitButton(UIButton *&button, const UIButtonConfig &buttonconfig);
+        static void InitImageView(UIImageView *&imageview, const UIImageConfig &imageconfig);
+    };
+
+}
+
+#endif
\ No newline at end of file
index 05fc04a..d1a38ef 100755 (executable)
@@ -6,6 +6,7 @@
 #include "ui_resource_image.h"\r
 #include <ui_scroll_view.h>\r
 #include "components/ui_button.h"\r
+#include "UIInitializer.h"\r
 namespace OHOS\r
 {\r
     static TraingView *g_pTraingView{nullptr};\r
@@ -37,7 +38,7 @@ namespace OHOS
             container_->SetStyle(STYLE_BACKGROUND_OPA, 0);\r
         }\r
 \r
-        // Add Label to App\r
+        /* Add Label to App\r
         if (label1_ == nullptr)\r
         {\r
             label1_ = new UILabel();\r
@@ -57,6 +58,13 @@ namespace OHOS
         // Add to rootView\r
         container_->Add(label1_);\r
         label1_->SetVisible(true);\r
+        */\r
+\r
+        // 创建一个 ComponentConfig 结构体实例\r
+        UILabelConfig labelConfig = {173, 52, 200, 60, "label1", "快速训练", DEFAULT_VECTOR_FONT_FILENAME, 30};\r
+\r
+        // 使用 UIInitializer 类中的初始化函数来初始化标签\r
+        UIInitializer::InitLabel(label1_, labelConfig);\r
 \r
         // Add Button to App\r
         if (button3_ == nullptr)\r