使用LoadSingleRes与UnloadSingleRes进行动画资源加载释放 846/2
authorCaisking <3449750381@qq.com>
Sat, 31 Aug 2024 08:30:08 +0000 (16:30 +0800)
committerCaisking <3449750381@qq.com>
Sat, 31 Aug 2024 08:30:08 +0000 (16:30 +0800)
code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.cpp
code/sdk/application/wearable/CustomUIComponentTool/uicomponents/UIComponents.h
code/sdk/application/wearable/fd230app/nativeui/takephotos/include/TakephotosView.h
code/sdk/application/wearable/fd230app/nativeui/takephotos/src/TakephotosView.cpp
code/sdk/application/wearable/fd230app/nativeui/traing/training_card/include/TraingCardView.h
code/sdk/application/wearable/fd230app/nativeui/traing/training_card/src/TraingCardView.cpp

index 6f24886..9eecf69 100644 (file)
@@ -436,7 +436,7 @@ namespace OHOS
     {
         InitLabel(label, x, y, width, height, viewId, text, fontName, 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 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)
         {
@@ -451,12 +451,12 @@ namespace OHOS
         imageAnimatorInfo = new ImageAnimatorInfo[numFrames];
         for (int i = 0; i < numFrames; i++)
         {
-            imageAnimatorInfo[i].imagePath = imagePaths[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_FILE_PATH;
+            imageAnimatorInfo[i].imageType = IMG_SRC_IMAGE_INFO;
         }
 
         imageAnimator->SetImageAnimatorSrc(imageAnimatorInfo, numFrames, time); // 200 is the default delay time
@@ -698,7 +698,22 @@ namespace OHOS
         }
         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++)
@@ -745,7 +760,6 @@ namespace OHOS
         }
         return imagePaths;
     }
-
     /**
      * 释放创建的图片路径数组
      * @param imagePaths 包含图片路径的数组
@@ -762,7 +776,6 @@ namespace OHOS
             UIFree(const_cast<const char **>(imagePaths)); // 释放整个数组
         }
     }
-
     /*解析动画资源数组 imageche*/
     std::vector<ImageInfo *> AnalysisArray(const char *filename, uint32_t startId, uint32_t lastId)
     {
index 016b285..1b515ed 100644 (file)
@@ -218,8 +218,9 @@ namespace OHOS
      * @param imagePaths   要释放的图像路径数组。
      * @param numImages    数组长度。
      */
-    void FreeImagePathsArray(const char **imagePaths, int numImages);
+    void FreeImageInfoArray(const ImageInfo **imagePaths, int numImages);
 
+    void FreeImagePathsArray(const char **imagePaths, int numImages);
     /**
      * @brief 初始化 UIImageAnimatorView,并设置为 GIF 动画。
      *
@@ -228,7 +229,7 @@ namespace OHOS
      * @param config             动画相关的配置(包括路径和帧信息)。
      * @param generalConfig      视图的基本配置(位置、大小、视图 ID 等)。
      */
-    void InitImageAnimatorSource(UIImageAnimatorView *&imageAnimator, ImageAnimatorInfo *&imageAnimatorInfo, const ImageAnimatorConfig &config, const GeneralConfig &generalConfig);
+    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 的资源。
index 319e10a..e15d0b1 100644 (file)
@@ -84,12 +84,12 @@ namespace OHOS
 
         bool viewiInitStatus{false};
 
-        std::string creambasePath;
-        const char **creamimagePaths;
+        std::string creambasePath = "/user/res/takephotos/cream/";
+        const ImageInfo **creamimageInfos;
         ImageAnimatorInfo *creamimageAnimatorInfo{nullptr};
 
-        std::string smilebasePath;
-        const char **smileimagePaths;
+        std::string smilebasePath = "/user/res/takephotos/cream/";
+        const ImageInfo **smileimageInfos;
         ImageAnimatorInfo *smileimageAnimatorInfo{nullptr};
     };
 } // namespace OHOS
index f9ed31a..e998433 100644 (file)
@@ -31,10 +31,10 @@ namespace OHOS
         // DeInit();
         creamAnimator->Stop();
         smileAnimator->Stop();
-        FreeImagePathsArray(creamimagePaths, 50);
-        FreeImagePathsArray(smileimagePaths, 27);
-        ReleaseImageAnimatorSource(creamimageAnimatorInfo,50);
-        ReleaseImageAnimatorSource(smileimageAnimatorInfo,27);
+        FreeImageInfoArray(creamimageInfos, creambasePath.c_str(), 50);
+        FreeImageInfoArray(smileimageInfos, smilebasePath.c_str(), 50);
+        ReleaseImageAnimatorSource(creamimageAnimatorInfo, 50);
+        ReleaseImageAnimatorSource(smileimageAnimatorInfo, 50);
 
         UIViewGroup *tmpGroup = dynamic_cast<UIViewGroup *>(container_);
         UIViewGroup::RemoveAndDeleteAllRecursively(tmpGroup);
@@ -146,9 +146,8 @@ namespace OHOS
         buttongo->SetOnClickListener(static_cast<UIView::OnClickListener *>(TakephotosPresenter::GetInstance()));
 
         // 初始化图片数组以及动画
-        creambasePath = "/user/res/takephotos/cream/";
-        creamimagePaths = CreateImagePathsArray(creambasePath.c_str(), 50);
-        InitImageAnimatorSource(creamAnimator, creamimageAnimatorInfo, creamimagePaths, 50, 163, 103, 140, 140, "imageAnimator", 100);
+        creamimageInfos = CreateImageInfoArray(creambasePath.c_str(), 50);
+        InitImageAnimatorSource(creamAnimator, creamimageAnimatorInfo, creamimageInfos, 50, 163, 103, 140, 140, "imageAnimator", 100);
 
         creamView->Add(creamAnimator);
         creamView->Add(labeltitle_);
@@ -197,9 +196,8 @@ namespace OHOS
         buttontake->SetOnClickListener(static_cast<UIView::OnClickListener *>(TakephotosPresenter::GetInstance()));
 
         // 初始化图片数组以及动画 {修改为统一动画}
-        smilebasePath = "/user/res/takephotos/cream/";
-        smileimagePaths = CreateImagePathsArray(smilebasePath.c_str(), 27);
-        InitImageAnimatorSource(smileAnimator, smileimageAnimatorInfo, smileimagePaths, 27, 163, 140, 140, 140, "imageAnimator", 100);
+        smileimageInfos = CreateImageInfoArray(smilebasePath.c_str(), 50);
+        InitImageAnimatorSource(smileAnimator, smileimageAnimatorInfo, smileimageInfos, 50, 163, 140, 140, 140, "imageAnimator", 100);
 
         takeView->Add(smileAnimator);
         takeView->Add(labeltake_);
index bf3e671..12c67b4 100644 (file)
@@ -135,8 +135,8 @@ namespace OHOS
         bool viewiInitStatus{false};\r
         bool momentumFlag{false};\r
 \r
-        std::string winbasePath;\r
-        const char **winimagePaths;\r
+        std::string winbasePath = "/user/res/Training/training_GIF/win/";\r
+        const ImageInfo **winimageInfos;\r
         ImageAnimatorInfo *winimageAnimatorInfo{nullptr};\r
     };\r
 } // namespace OHOS\r
index c34330f..ee3da68 100644 (file)
@@ -87,7 +87,9 @@ namespace OHOS
 \r
     TraingCardView::~TraingCardView()\r
     {\r
-        FreeImagePathsArray(winimagePaths, 94);\r
+        imageAnimator->Stop();\r
+        FreeImageInfoArray(winimageInfos, winbasePath.c_str(), 94);\r
+        ReleaseImageAnimatorSource(winimageAnimatorInfo, 94);\r
         DeInit();\r
 \r
         g_pTraingCardView = nullptr;\r
@@ -181,9 +183,6 @@ namespace OHOS
         buttonnote_ = nullptr;\r
 \r
         /*动画*/\r
-        FreeImagePathsArray(winimagePaths, 94);\r
-        ReleaseImageAnimatorSource(winimageAnimatorInfo,94);\r
-        imageAnimator->Stop();\r
         delete imageAnimator;\r
         imageAnimator = nullptr;\r
 \r
@@ -490,9 +489,8 @@ namespace OHOS
         getView->Add(labeltag);\r
 \r
         // 初始化图片数组以及动画\r
-        winbasePath = "/user/res/Training/training_GIF/win/";\r
-        winimagePaths = CreateImagePathsArray(winbasePath.c_str(), 94);\r
-        InitImageAnimatorSource(imageAnimator, winimageAnimatorInfo, winimagePaths, 94, 173, 100, 120, 120, "imageAnimator1", 10);\r
+        winimageInfos = CreateImageInfoArray(winbasePath.c_str(), 94);\r
+        InitImageAnimatorSource(imageAnimator, winimageAnimatorInfo, winimageInfos, 94, 173, 100, 120, 120, "imageAnimator1", 10);\r
         getView->Add(imageAnimator);\r
     }\r
 \r