增加表盘电量动态变化 544/1 544/head 545/target/1 546/target/1
authorCaisking <3449750381@qq.com>
Wed, 17 Jul 2024 08:20:22 +0000 (16:20 +0800)
committerCaisking <3449750381@qq.com>
Wed, 17 Jul 2024 08:20:22 +0000 (16:20 +0800)
code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h
code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp

index b777722..068a9cd 100644 (file)
@@ -1,3 +1,9 @@
+/***
+ * @Date: 2024-05-30 15:59:37
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-07-17 15:01:17
+ * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/include/dial/DialBoxProgressView.h
+ */
 /*
  * Copyright (c) @CompanyNameMagicTag. 2023. All rights reserved.
  */
 #include "DialView.h"
 #include "components/ui_box_progress.h"
 
-namespace OHOS {
-class DialBoxProgressView : public UIBoxProgress, public DialView {
-public:
-    DialBoxProgressView();
-    ~DialBoxProgressView() override;
-    static DialBoxProgressView* GetInstance()
+namespace OHOS
+{
+    class DialBoxProgressView : public UIBoxProgress, public DialView
     {
-        static DialBoxProgressView instance;
-        return &instance;
-    }
-    void HandleFloatData(float data) override;
-private:
-    int32_t value_ = -1;
-};
+    public:
+        DialBoxProgressView();
+        ~DialBoxProgressView() override;
+        static DialBoxProgressView *GetInstance()
+        {
+            static DialBoxProgressView instance;
+            return &instance;
+        }
+        void HandleFloatData(float data) override;
+        void UpdataChargColor();
+        void UpdateBatteryColor();
+        void SetViewColor(int64_t color);
+
+    private:
+        int32_t value_ = -1;
+        int64_t color_ = Color::White().full;
+    };
 }
 #endif
\ No newline at end of file
index ce5c756..da4911a 100644 (file)
@@ -1,3 +1,9 @@
+/***
+ * @Date: 2024-04-15 10:55:58
+ * @LastEditors: Caisking 3449750381@qq.com
+ * @LastEditTime: 2024-07-17 14:54:10
+ * @FilePath: /FD230/code/sdk/application/wearable/fd230app/nativeui/main/src/dial/DialBoxProgressView.cpp
+ */
 /*
  * Copyright (c) @CompanyNameMagicTag. 2023. All rights reserved.
  */
 #include "battery_hal.h"
 extern uint8_t battery_get_voltage(float *voltage);
 
+// 定义电量宏
+#define BATTERY_LOW_THRESHOLD 10
+#define BATTERY_MEDIUM_THRESHOLD 20
+
 namespace OHOS
 {
     DialBoxProgressView::DialBoxProgressView(void)
     {
+        value_ = 0;
     }
 
     DialBoxProgressView::~DialBoxProgressView(void)
@@ -20,7 +31,7 @@ namespace OHOS
     void DialBoxProgressView::HandleFloatData(float data)
     {
         float ratio = data;
-        //WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DialBoxProgressView:%d", data);
+        // WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "DialBoxProgressView:%d", data);
         if (ratio > 1.0f)
         {
             ratio = 1.0f;
@@ -30,17 +41,51 @@ namespace OHOS
             ratio = 0.0f;
         }
         int32_t value = ratio * (GetRangeMax() - GetRangeMin());
+
+        // 更新电池颜色
+        UpdateBatteryColor();
+
         if (value_ == value)
         {
             return;
         }
-#if 1
-        // 获取电池电压值
-        float voltage = 0.0F;
-        value = battery_get_voltage(&voltage);
+
         value_ = value;
-#endif
-        //WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP,"battery_get_value:%d", value_);
         SetValue(value_);
     }
-}
+
+    void DialBoxProgressView::UpdateBatteryColor()
+    {
+        bool isCharging = get_charge_stat(); // 获取充电状态
+
+        if (isCharging)
+        {
+            SetViewColor(Color::Green().full); // 充电状态为绿色
+            return;
+        }
+        else
+        {
+            if (value_ <= BATTERY_LOW_THRESHOLD)
+            {
+                SetViewColor(Color::Red().full);
+            }
+            else if (BATTERY_LOW_THRESHOLD < value_ && value_ <= BATTERY_MEDIUM_THRESHOLD)
+            {
+                SetViewColor(Color::Yellow().full);
+            }
+            else
+            {
+                SetViewColor(Color::White().full);
+            }
+        }
+    }
+    void DialBoxProgressView::SetViewColor(int64_t color)
+    {
+        if (color == color_)
+        {
+            return;
+        }
+        color_ = color;
+        SetForegroundStyle(STYLE_BACKGROUND_COLOR, color_);
+    }
+}
\ No newline at end of file