查找手机界面UI优化,解决内存泄漏
authorluozheng <1732036298@qq.com>
Sat, 31 Aug 2024 10:30:05 +0000 (18:30 +0800)
committerluozheng <1732036298@qq.com>
Sat, 31 Aug 2024 10:30:05 +0000 (18:30 +0800)
code/sdk/application/wearable/fd230app/nativeui/findphone/src/FindPhoneView.cpp

index 5e2e81e..758763a 100644 (file)
@@ -8,7 +8,7 @@
  * @Author: luozheng 1732036298@qq.com
  * @Date: 2024-03-27 10:47:46
  * @LastEditors: luozheng 1732036298@qq.com
- * @LastEditTime: 2024-08-31 17:31:35
+ * @LastEditTime: 2024-08-31 18:27:22
  * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/findphone/src/FindPhoneView.cpp
  */
 #include "findphone/include/FindPhoneView.h"
@@ -35,7 +35,6 @@ namespace OHOS
     FindPhoneView::~FindPhoneView()
     {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "FindPhoneView::~FindPhoneView");
-        ClearAll();
         g_pFindPhoneView = nullptr;
     }
 
@@ -48,22 +47,30 @@ namespace OHOS
     {
         if (viewInitStatus == true)
         {
+            // UIViewGroup *tmpGroup = dynamic_cast<UIViewGroup *>(container_);
+            // UIViewGroup::RemoveAndDeleteAllRecursively(tmpGroup);
+            // FreeImageInfoArray(FindPhonePathsArray, FIND_PONE_IMAGE_SRC, FINDPHONE_IMAGE_ANIMATOR_VIEW_NUM_FRAMES);
+
+            if (container_ != nullptr)
+            {
+                container_->RemoveAll();
+                delete container_;
+                container_ = nullptr;
+            }
             if (FindPhoneGroupView != nullptr)
             {
                 FindPhoneGroupView->RemoveAll();
 
-                if (FindPhoneTitleLabel == nullptr)
+                if (FindPhoneTitleLabel != nullptr)
                 {
                     delete FindPhoneTitleLabel;
                     FindPhoneTitleLabel = nullptr;
                 }
-
                 if (FindPhoneImageView != nullptr)
                 {
                     delete FindPhoneImageView;
                     FindPhoneImageView = nullptr;
                 }
-
                 if (FindPhoneImageAnimator != nullptr)
                 {
                     FindPhoneImageAnimator->Stop();
@@ -72,21 +79,19 @@ namespace OHOS
                     // FreeImagePathsArray(FindPhonePathsArray, FINDPHONE_IMAGE_ANIMATOR_VIEW_NUM_FRAMES);
                     FreeImageInfoArray(FindPhonePathsArray, FIND_PONE_IMAGE_SRC, FINDPHONE_IMAGE_ANIMATOR_VIEW_NUM_FRAMES);
                 }
-
                 if (FindPhoneLabel != nullptr)
                 {
                     delete FindPhoneLabel;
                     FindPhoneLabel = nullptr;
                 }
-
                 if (FindPhoneLabelButton != nullptr)
                 {
                     delete FindPhoneLabelButton;
                     FindPhoneLabelButton = nullptr;
                 }
-
                 delete FindPhoneGroupView;
                 FindPhoneGroupView = nullptr;
+                WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "FindPhoneView::ClearAll over!!");
             }
         }
     }
@@ -106,6 +111,9 @@ namespace OHOS
             }
             FindPhoneGroupView->SetPosition(0, 0, 466, 466);
             FindPhoneGroupView->SetStyle(STYLE_BACKGROUND_OPA, 0);
+            FindPhoneGroupView->SetTouchable(true);
+            FindPhoneGroupView->SetDraggable(true);
+            FindPhoneGroupView->SetOnDragListener(presenter_);
             /* --------------------------- FindPhoneTitleLabel -------------------------- */
             FindPhoneTitleLabel = new UILabelExt();
             if (FindPhoneTitleLabel == nullptr)
@@ -161,13 +169,13 @@ namespace OHOS
             FindPhoneLabelButton->SetTextColor(Color::GetColorFromRGBA(0xff, 0x5a, 0x36, 0xff));
             FindPhoneLabelButton->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
             FindPhoneLabelButton->SetStyleForState(STYLE_BORDER_COLOR, 0xff0b1928, UIButton::PRESSED);
-            UIView::OnClickListener *clickListener = static_cast<UIView::OnClickListener *>(FindPhonePresenter::GetInstance());
-            FindPhoneLabelButton->SetOnClickListener(clickListener);
+            FindPhoneLabelButton->SetOnClickListener(presenter_);
             /* ----------------------------------- Add ---------------------------------- */
             FindPhoneGroupView->Add(FindPhoneTitleLabel);
             FindPhoneGroupView->Add(FindPhoneImageView);
             FindPhoneGroupView->Add(FindPhoneLabel);
             FindPhoneGroupView->Add(FindPhoneLabelButton);
+            container_->Add(FindPhoneGroupView);
         }
         viewInitStatus = true;
     }
@@ -187,6 +195,9 @@ namespace OHOS
             }
             FindPhoneGroupView->SetPosition(0, 0, 466, 466);
             FindPhoneGroupView->SetStyle(STYLE_BACKGROUND_OPA, 0);
+            FindPhoneGroupView->SetTouchable(true);
+            FindPhoneGroupView->SetDraggable(true);
+            FindPhoneGroupView->SetOnDragListener(presenter_);
             /* --------------------------- FindPhoneTitleLabel -------------------------- */
             FindPhoneTitleLabel = new UILabelExt();
             if (FindPhoneTitleLabel == nullptr)
@@ -228,7 +239,6 @@ namespace OHOS
                 FindPhoneImageAnimator->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
                 FindPhoneImageAnimator->SetImageAnimatorSrc(FindPhoneImageInfo, FINDPHONE_IMAGE_ANIMATOR_VIEW_NUM_FRAMES, FINDPHONE_IMAGE_ANIMATOR_VIEW_TIMEOFUPDATE);
                 FindPhoneImageAnimator->SetRepeat(true);
-                FindPhoneImageAnimator->Start();
             }
             /* ----------------------------- FindPhoneLabel ----------------------------- */
             FindPhoneLabel = new UILabelExt();
@@ -261,13 +271,13 @@ namespace OHOS
             FindPhoneLabelButton->SetTextColor(Color::GetColorFromRGBA(0xff, 0x5a, 0x36, 0xff));
             FindPhoneLabelButton->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 30);
             FindPhoneLabelButton->SetStyleForState(STYLE_BORDER_COLOR, 0xff0b1928, UIButton::PRESSED);
-            UIView::OnClickListener *clickListener = static_cast<UIView::OnClickListener *>(FindPhonePresenter::GetInstance());
-            FindPhoneLabelButton->SetOnClickListener(clickListener);
+            FindPhoneLabelButton->SetOnClickListener(presenter_);
             /* ----------------------------------- Add ---------------------------------- */
             FindPhoneGroupView->Add(FindPhoneTitleLabel);
             FindPhoneGroupView->Add(FindPhoneImageAnimator);
             FindPhoneGroupView->Add(FindPhoneLabel);
             FindPhoneGroupView->Add(FindPhoneLabelButton);
+            container_->Add(FindPhoneGroupView);
         }
         viewInitStatus = true;
     }
@@ -275,7 +285,6 @@ namespace OHOS
     void FindPhoneView::OnStart()
     {
         View<FindPhonePresenter>::OnStart();
-
         // if (FindPhoneModel::GetInstance().GetBlueToothConnectedState() == true) // 判断蓝牙是否是连接状态
         // {
         //     FindPhoneViewInit_BlueToothConnected();
@@ -285,25 +294,32 @@ namespace OHOS
         // {
         //     FindPhoneViewInit_BlueToothDisConnected();
         // }
+        SetContainerView(new UIViewGroup());
+        if (GetContainer() == nullptr)
+        {
+            WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "container_ new fail!!");
+            return;
+        }
+        container_->SetPosition(0, 0, Screen::GetInstance().GetWidth(), Screen::GetInstance().GetHeight());
+        container_->SetTouchable(true);
+        container_->SetDraggable(true);
 
-        FindPhoneViewInit_BlueToothConnected();
-        senfindphoneSend(FINDPHONE_START);
-
-        FindPhoneGroupView->SetTouchable(true);
-        FindPhoneGroupView->SetDraggable(true);
-        UIView::OnDragListener *dragListener = static_cast<UIView::OnDragListener *>(FindPhonePresenter::GetInstance());
-        FindPhoneGroupView->SetOnDragListener(dragListener);
-
-        // 增加按键监听
-        RootView::OnKeyActListener *keyactListener = (RootView::OnKeyActListener *)OHOS::FindPhonePresenter::GetInstance();
-        RootView::GetInstance()->SetOnKeyActListener(keyactListener);
+        if (!viewInitStatus)
+        {
+            FindPhoneViewInit_BlueToothConnected();
+            senfindphoneSend(FINDPHONE_START);
+            viewInitStatus = true;
+        }
+        RootView::GetInstance()->SetOnKeyActListener(presenter_);
     }
 
     void FindPhoneView::OnSetUpView()
     {
         View<FindPhonePresenter>::OnSetUpView();
-        RootView::GetInstance()->Add(FindPhoneGroupView);
-        container_ = FindPhoneGroupView;
+        if (FindPhoneImageAnimator)
+        {
+            FindPhoneImageAnimator->Start();
+        }
         Draw();
     }
 
@@ -316,8 +332,8 @@ namespace OHOS
     void FindPhoneView::OnStop()
     {
         View<FindPhonePresenter>::OnStop();
-        RootView::GetInstance()->RemoveAll();
         RootView::GetInstance()->ClearOnKeyActListener();
+        ClearAll();
         return;
     }
 }
\ No newline at end of file