增加电量动态显示以及百分比数字同步
authorCaisking <3449750381@qq.com>
Tue, 19 Dec 2023 09:08:10 +0000 (17:08 +0800)
committerCaisking <3449750381@qq.com>
Tue, 19 Dec 2023 09:08:10 +0000 (17:08 +0800)
code/sdk/application/wearable/nativeapp/nativeui/clock/include/MainClockView.h
code/sdk/application/wearable/nativeapp/nativeui/clock/src/MainClockView.cpp
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery-E.bin [new file with mode: 0644]
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Limit.bin [new file with mode: 0644]
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Nomoal.bin [new file with mode: 0644]
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Save.bin [new file with mode: 0644]
code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Icon.bin [new file with mode: 0644]
code/sdk/middleware/services/gui/uikit/sample/uitest/components/ui_test_clock_dial/MainClockView.h

index b98ed65..0f55177 100755 (executable)
 #include "animator/animator_manager.h"
 #include "time.h"
 #include "sys/time.h"
+#include "animator/animator.h"
+#include "common/image_cache_manager.h"
+#include <time.h>
+#include "sys/time.h"
+#include "components/ui_box_progress.h"
+#include "components/ui_image_view.h"
+#include "components/ui_label.h"
 
 namespace OHOS {
 static constexpr int16_t SEC_TO_MILLI = 1000;
@@ -60,7 +67,35 @@ public:
 private:
     UISweepClock *sweepClock{nullptr};
 };
+class ProgressCallback : public AnimatorCallback {
+public:
+    explicit ProgressCallback(UIBoxProgress *progress, UILabel *label) : progress(progress), label(label) {}
+    ~ProgressCallback() {}
+    virtual void Callback(UIView *view)
+    {
+        int value = std::rand() % 101; // 生成一个介于 0 和 100 之间的随机数
+        char* progress1ForeImageSrc = nullptr;
+        char* progress1BackImageSrc = nullptr;
+        if (value <= 10) {
+            progress1ForeImageSrc = "/user/res/Battery/Battery_Limit.bin";
+        } else if (value <= 20) {
+            progress1ForeImageSrc = "/user/res/Battery/Battery_Save.bin";
 
+        } else {
+            progress1ForeImageSrc = "/user/res/Battery/Battery_Nomoal.bin";
+        }
+        progress->SetImage(progress1ForeImageSrc, progress1BackImageSrc);
+        progress->SetValue(value); // 更新界面
+        label->SetText((std::to_string(value) + "%").c_str()); // 更新百分比显示
+        //for(int i=0;i<1000000000;i++){};
+    }
+    
+private:
+    UILabel *label{nullptr};
+    UIBoxProgress *progress{nullptr};
+    //std::thread workerThread;
+    //std::atomic<bool> stopThread;
+};
 class MainClockView : public UICardPage {
 public:
     MainClockView();
@@ -82,6 +117,10 @@ private:
     bool InitSweepClock(void);
     bool LoadClockImage(Image *view, uint32_t resId);
     bool LoadClockImage(UIImageView *view, uint32_t resId);
+
+    UIBoxProgress* progress = nullptr;
+    AnimatorCallback* progress1callback_ = nullptr;
+    Animator *progress1animator_ = nullptr;
     UIImageView *curFace{nullptr};
     UILabelButton *stepLabel{nullptr};
     UILabel *batteryLabel{nullptr};
@@ -97,7 +136,7 @@ private:
     UIImageView *heartRate{nullptr};
     Image *hand24Hour{nullptr};
     UIImageView *weekDay{nullptr};
-    UIImageView *progress{nullptr};
+    //UIImageView *progress{nullptr};
     Image *bigCircle{nullptr};
     Image *medCircle{nullptr};
     Image *smallCircle{nullptr};
index a02a39c..5e16365 100755 (executable)
@@ -447,6 +447,28 @@ bool MainClockView::InitSweepClock(void)
     weekLabel->SetAlign(TEXT_ALIGNMENT_CENTER, TEXT_ALIGNMENT_CENTER);
     weekLabel->SetRollSpeed(0);
     weekLabel->SetFont(DEFAULT_VECTOR_FONT_FILENAME, 26);
+
+    // Add BoxProgress to App
+    if (progress == nullptr) {
+        progress = new UIBoxProgress();
+    }
+    progress->SetPosition(182, 58);         // widget's left position and top position
+    progress->Resize(31, 19);          // widget's width and height
+    progress->SetViewId("progress1");// Set style
+    progress->SetStyle(STYLE_BACKGROUND_OPA, 0xff);
+
+    // Set additional property
+    progress->SetValue(50);
+    // Set BackgroundStyle
+    progress->SetBackgroundStyle(STYLE_TEXT_COLOR, 0xff000000);
+    // Set ForegroundStyle
+    progress->SetForegroundStyle(STYLE_TEXT_COLOR, 0xff000000);
+    progress->SetForegroundStyle(STYLE_LINE_COLOR, 0xff000000);
+    progress->SetForegroundStyle(STYLE_LINE_WIDTH, 0);
+    char* progress1ForeImageSrc = "/user/res/Battery_Nomoal.bin";
+    char * progress1BackImageSrc = nullptr;
+    progress->SetImage(progress1ForeImageSrc, progress1BackImageSrc);
+
     /**************************************************************/
     //InitLabelView();
 #if 0
@@ -485,7 +507,7 @@ bool MainClockView::InitView(void)
     if (mainSweepClock == nullptr) {
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "mainSweepClock new fail");
         return false;
-    }
+        }
     if (InitSweepClock() == false) {
         return false;
     }
@@ -494,6 +516,7 @@ bool MainClockView::InitView(void)
     mainSweepClock->Add(weekLabel);
     mainSweepClock->Add(dataLabel);
     mainSweepClock->Add(batteryLabel);
+    mainSweepClock->Add(progress);
 #if 0
     mainSweepClock->Add(stepLabel);
     mainSweepClock->Add(heartRate);
@@ -538,6 +561,9 @@ bool MainClockView::InitView(void)
         WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "clockAnimator new fail");
         return false;
     }
+    progress1callback_ = new ProgressCallback(progress,batteryLabel);
+    progress1animator_ = new Animator(progress1callback_,progress,100,true);
+    progress1animator_->Start();
     clockAnimator->Start();
     SetCoverable(true);
     return true;
@@ -579,6 +605,7 @@ void MainClockView::InitDateTime(void)
 void MainClockView::ClockAnimatorStart(void)
 {
     if (clockAnimator != nullptr) {
+        progress1animator_->Start();
         clockAnimator->Start();
     }
 }
@@ -587,6 +614,7 @@ void MainClockView::ClockAnimatorStop(void)
 {
     if (clockAnimator != nullptr) {
         clockAnimator->Stop();
+        progress1animator_->Stop();
     }
 }
 
diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery-E.bin b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery-E.bin
new file mode 100644 (file)
index 0000000..914c497
Binary files /dev/null and b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery-E.bin differ
diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Limit.bin b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Limit.bin
new file mode 100644 (file)
index 0000000..b37f539
Binary files /dev/null and b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Limit.bin differ
diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Nomoal.bin b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Nomoal.bin
new file mode 100644 (file)
index 0000000..ebab346
Binary files /dev/null and b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Nomoal.bin differ
diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Save.bin b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Save.bin
new file mode 100644 (file)
index 0000000..b132f4e
Binary files /dev/null and b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Battery_Save.bin differ
diff --git a/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Icon.bin b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Icon.bin
new file mode 100644 (file)
index 0000000..20f4e43
Binary files /dev/null and b/code/sdk/build/config/target_config/brandy/mk_fs_image/fs/user/res/Battery/Icon.bin differ
index 27979d0..95ace9a 100755 (executable)
@@ -99,7 +99,7 @@ private:
     UIImageView *heartRate{nullptr};
     Image *hand24Hour{nullptr};
     UIImageView *weekDay{nullptr};
-    UIImageView *progress{nullptr};
+    //UIImageView *progress{nullptr};
     Image *bigCircle{nullptr};
     Image *medCircle{nullptr};
     Image *smallCircle{nullptr};