优化 MoveChildByOffset 函数中的振动调用逻辑,减少震动函数调用次数
authorluozheng <1732036298@qq.com>
Tue, 13 Jan 2026 08:23:23 +0000 (16:23 +0800)
committerluozheng <1732036298@qq.com>
Tue, 13 Jan 2026 08:23:23 +0000 (16:23 +0800)
code/sdk/middleware/services/gui/uikit/ui/frameworks/components/ui_list.cpp

index 75d331a..ba279a8 100644 (file)
@@ -17,6 +17,9 @@
 
 #include "components/ui_abstract_scroll_bar.h"
 #include "gfx_utils/graphic_log.h"
+#if ENABLE_VIBRATOR
+#include "hal_tick.h"
+#endif
 
 namespace OHOS {
 UIList::Recycle::~Recycle()
@@ -150,6 +153,11 @@ void UIList::Recycle::MoveAdapterItemsRelativeRect(int16_t x, int16_t y)
 
 UIList::UIList() : UIList(VERTICAL) {}
 
+/* --------------------------------- 减少震动次数 --------------------------------- */
+static uint32_t lastVibratorTime_;
+static constexpr uint32_t MIN_VIBRATOR_INTERVAL_MS = 100; // 最小振动间隔100毫秒
+/* -------------------------------------------------------------------------- */
+
 UIList::UIList(uint8_t direction)
     : onSelectedView_(nullptr),
       isLoopList_(false),
@@ -171,6 +179,7 @@ UIList::UIList(uint8_t direction)
 #if ENABLE_VIBRATOR
     needVibration_ = false;
     vibratorType_ = VibratorType::VIBRATOR_TYPE_ONE;
+    lastVibratorTime_ = 0;
 #endif
 #if ENABLE_FOCUS_MANAGER
     focusable_ = true;
@@ -707,6 +716,25 @@ void UIList::MoveChildByOffset(int16_t xOffset, int16_t yOffset)
                 }
             }
 #if ENABLE_VIBRATOR
+            VibratorFunc vibratorFunc = VibratorManager::GetInstance()->GetVibratorFunc();
+            if (isSelectViewFind && needVibration_ && vibratorFunc != nullptr) {
+                printf("isSelectViewFind = %d\n", isSelectViewFind);
+                uint32_t currentTime = HALTick::GetInstance().GetTime();
+                // 只有在规定时间间隔后才允许调用振动(首次调用时lastVibratorTime_为0,允许调用)
+                bool canVibrate = (lastVibratorTime_ == 0) || 
+                                  (HALTick::GetInstance().GetElapseTime(lastVibratorTime_) >= MIN_VIBRATOR_INTERVAL_MS);
+                if (canVibrate) {
+                    if (!isLoopList_ && (onSelectedIndex_ == 0 || onSelectedIndex_ == recycle_.adapter_->GetCount() - 1)) {
+                        vibratorFunc(VibratorType::VIBRATOR_TYPE_THREE);
+                        GRAPHIC_LOGI("UIList::MoveChildByOffset calls TYPE_THREE vibrator");
+                    } else {
+                        vibratorFunc(VibratorType::VIBRATOR_TYPE_TWO);
+                        GRAPHIC_LOGI("UIList::MoveChildByOffset calls TYPE_TWO vibrator");
+                    }
+                    lastVibratorTime_ = currentTime;
+                }
+            }
+            #if 0
             VibratorFunc vibratorFunc = VibratorManager::GetInstance()->GetVibratorFunc();
             if (isSelectViewFind && needVibration_ && vibratorFunc != nullptr) {
                 if (!isLoopList_ && (onSelectedIndex_ == 0 || onSelectedIndex_ == recycle_.adapter_->GetCount() - 1)) {
@@ -717,6 +745,7 @@ void UIList::MoveChildByOffset(int16_t xOffset, int16_t yOffset)
                     GRAPHIC_LOGI("UIList::MoveChildByOffset calls TYPE_TWO vibrator");
                 }
             }
+            #endif
 #endif
         }
         view = view->GetNextSibling();