#include "ui_resource_image.h"
#include <ui_scroll_view.h>
#include "components/ui_button.h"
+
namespace OHOS
{
static const char *imagePath_win = "/user/res/Training/win.bin";
static TraingWinView *g_pTraingWinView{nullptr};
-
TraingWinView::TraingWinView()
{
g_pTraingWinView = this;
DeInit();
g_pTraingWinView = nullptr;
}
-
TraingWinView *TraingWinView::GetInstance()
{
return g_pTraingWinView;
}
- /*
- InitLabel:初始化标签
- @param label 标签指针
- @param left 标签左边位置
- @param top 标签顶部位置
- @param width 标签宽度
- @param height 标签高度
- @param viewId 标签的视图 ID
- @param text 标签显示的文本
- @param fontName 字体名称
- @param fontSize 字体大小
+
+ /**
+ * 创建包含图片文件路径的数组
+ * @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;
+ }
+
+ /**
+ * 释放图片文件路径数组占用的内存
+ * @param imagePaths 图片文件路径数组
+ * @param numImages 图片文件的数量
+ */
+ void FreeImagePathsArray(const char **imagePaths, int numImages)
+ {
+ for (int i = 0; i < numImages; i++)
+ {
+ free((void *)imagePaths[i]);
+ }
+ delete[] imagePaths;
+ }
+ /**************************************************/
+ /**
+ InitLabel:初始化标签
+ @param label 标签指针
+ @param left 标签左边位置
+ @param top 标签顶部位置
+ @param width 标签宽度
+ @param height 标签高度
+ @param viewId 标签的视图 ID
+ @param text 标签显示的文本
+ @param fontName 字体名称
+ @param fontSize 字体大小
*/
void TraingWinView::InitLabel(UILabel *&label, int left, int top, int width, int height, const char *viewId, const char *text, const char *fontName, int fontSize)
{
// 设置标签可见
label->SetVisible(true);
}
- /*
- InitButton:初始化按钮
- @param button 按钮指针
- @param left 按钮左边位置
- @param top 按钮顶部位置
- @param width 按钮宽度
- @param height 按钮高度
- @param viewId 按钮的视图 ID
+ /**
+ InitButton:初始化按钮
+ @param button 按钮指针
+ @param left 按钮左边位置
+ @param top 按钮顶部位置
+ @param width 按钮宽度
+ @param height 按钮高度
+ @param viewId 按钮的视图 ID
*/
void TraingWinView::InitButton(UIButton *&button, int left, int top, int width, int height, const char *viewId)
{
// 设置按钮可见
button->SetVisible(true);
}
- /*
- InitImageView:初始化图片
- @param imageView 图片指针
- @param left 图片左边位置
- @param top 图片顶部位置
- @param width 图片宽度
- @param height 图片高度
- @param viewId 图片的视图 ID
- @param imagePath 图片路径
- */
+
+ /**
+ * 初始化图片视图
+ * @param imageView 图片视图指针
+ * @param left 图片左边位置
+ * @param top 图片顶部位置
+ * @param width 图片宽度
+ * @param height 图片高度
+ * @param viewId 图片的视图 ID
+ * @param imagePath 图片路径
+ */
void TraingWinView::InitImageView(UIImageView *&imageView, int left, int top, int width, int height, const char *viewId, const char *imagePath)
{
if (imageView == nullptr)
imageView->SetVisible(true);
}
+ /**
+ * @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。
+ * @return 如果初始化成功则返回true,否则返回false。
+ */
+ void TraingWinView::InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, const char **imagePaths, int numFrames, int left, int top, int width, int height, const char *viewId)
+ {
+ 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, 10); // 200 is the default delay time
+ imageAnimator->SetTimeOfPause(10);
+ imageAnimator->SetRepeatTimes(1);
+ imageAnimator->SetReverse(true);
+ imageAnimator->SetVisible(true);
+ }
/* InitView:初始化界面*/
void TraingWinView ::InitView()
{
label4_->SetTextColor(Color::GetColorFromRGBA(0xff, 0xff, 0xff, 0x7f));
container_->Add(label4_);
- // 初始化image
- InitImageView(image1_, 173, 100, 120, 120, "image1", imagePath_win);
- container_->Add(image1_);
+ /*
+ 初始化image
+ InitImageView(image1_, 173, 100, 120, 120, "image1", imagePath_win);
+ container_->Add(image1_);
+ */
+
+ // 初始化图片数组
+ const char *basePath = "/user/res/Training/win/";
+ const char *fileExtension = "bin";
+ const char **imagePaths = CreateImagePathsArray(basePath, fileExtension, 94);
+ InitImageAnimatorSource(imageAnimator, imagePaths, 94, 173, 100, 120, 120, "imageAnimator1");
+ container_->Add(imageAnimator);
+ //FreeImagePathsArray(imagePaths, 94); //删除数组,释放内存
// 初始化Button
InitButton(button1_, 0, 366, 466, 100, BUTTONOK);
button1_->SetImageSrc(button1defaultImgSrc, button1triggeredImgSrc);
container_->Add(button1_);
}
+
/* DeInit:删除组件*/
void TraingWinView::DeInit()
{
if (!viewiInitStatus)
{
InitView();
+ imageAnimator->Start();
viewiInitStatus = true;
}
}
void TraingWinView::OnStop()
{
View<TraingWinPresenter>::OnStop();
+ imageAnimator->Stop();
RootView::GetInstance()->RemoveAll();
return;
}