From b8567f705a2e807ca096833478d8e358b9828ef4 Mon Sep 17 00:00:00 2001 From: Siavash Date: Mon, 30 Sep 2024 16:29:23 +0330 Subject: [PATCH] CHANGELOG: Added charging animation option to the battery bar (Thanks to: OnePlus Customizer dev: @DHD2280) --- .github/edit2MakeNewCanary | 2 +- .../utils/batteryStyles/BatteryBarView.java | 56 ++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/.github/edit2MakeNewCanary b/.github/edit2MakeNewCanary index 04ad0604c..65aeddb0b 100644 --- a/.github/edit2MakeNewCanary +++ b/.github/edit2MakeNewCanary @@ -1 +1 @@ -Editing this file will trigger the build script for a new canary.. \ No newline at end of file +Editing this file will trigger the build script for a new canary. \ No newline at end of file diff --git a/app/src/main/java/sh/siava/pixelxpert/modpacks/utils/batteryStyles/BatteryBarView.java b/app/src/main/java/sh/siava/pixelxpert/modpacks/utils/batteryStyles/BatteryBarView.java index 7171080e3..0d6d74d97 100644 --- a/app/src/main/java/sh/siava/pixelxpert/modpacks/utils/batteryStyles/BatteryBarView.java +++ b/app/src/main/java/sh/siava/pixelxpert/modpacks/utils/batteryStyles/BatteryBarView.java @@ -15,11 +15,13 @@ import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.RectShape; import android.os.Handler; +import android.os.Looper; import android.text.TextUtils; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.ViewGroup; -import android.view.animation.LinearInterpolator; +import android.view.animation.AccelerateInterpolator; +import android.view.animation.BaseInterpolator; import android.view.animation.TranslateAnimation; import android.widget.FrameLayout; import android.widget.ImageView; @@ -65,7 +67,7 @@ public class BatteryBarView extends FrameLayout { private static boolean animateCharging = false; private static final int ANIM_DURATION = 1000; private static final int ANIM_DELAY = 2000; - private final Handler animationHandler = new Handler(); + private final Handler animationHandler = new Handler(Looper.getMainLooper()); private Runnable chargingAnimationRunnable; public static void setStaticColor(List batteryLevels, int[] batteryColors, boolean indicateCharging, int chargingColor, boolean indicateFastCharging, int fastChargingColor, boolean indicatePowerSave, int powerSaveColor, boolean transitColors, boolean animate) { @@ -111,12 +113,6 @@ public void refreshLayout() { refreshColors(barView.getWidth(), barView.getHeight()); mDrawable.invalidateSelf(); - - if (isCharging() && animateCharging) { - startChargingAnimation(); - } else { - stopChargingAnimation(); - } } private LayoutParams maskLayoutParams() { @@ -133,11 +129,6 @@ public void onSizeChanged(int w, int h, int oldw, int oldh) { } private void startChargingAnimation() { - if (!isCharging() || !animateCharging) { - stopChargingAnimation(); - return; - } - if (chargingAnimationRunnable == null) { chargingAnimationRunnable = new Runnable() { @Override @@ -151,20 +142,25 @@ public void run() { } private void stopChargingAnimation() { - if (chargingAnimationRunnable != null) { - animationHandler.removeCallbacks(chargingAnimationRunnable); - chargingIndicatorView.post(() -> chargingIndicatorView.setVisibility(GONE)); - chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(GONE)); - chargingAnimationRunnable = null; - } + animationHandler.removeCallbacks(chargingAnimationRunnable); + + post(() -> { + chargingIndicatorView.setVisibility(GONE); + chargingIndicatorView.clearAnimation(); + + chargingIndicatorViewForCenter.setVisibility(GONE); + chargingIndicatorViewForCenter.clearAnimation(); + }); + + chargingAnimationRunnable = null; } private void animateChargingIndicator() { int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels; chargingIndicatorView.post(() -> chargingIndicatorView.setVisibility(VISIBLE)); - int secondaryVisiblity = (isCenterBased) ? GONE : VISIBLE; - chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(secondaryVisiblity)); + int secondaryVisibility = (isCenterBased) ? GONE : VISIBLE; + chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(secondaryVisibility)); float startX, startXCenter, endX; @@ -177,17 +173,20 @@ private void animateChargingIndicator() { startXCenter = 0; endX = Math.round(getWidth() * getCurrentLevel() / 100f); } - if (isCenterBased) endX = getWidth() / 2f; + + if (isCenterBased) endX = getWidth() * (1 - ((100 - getCurrentLevel()) / 200f)); + + BaseInterpolator interpolator = new AccelerateInterpolator(); TranslateAnimation animation = new TranslateAnimation(startX, endX, 0, 0); animation.setDuration(ANIM_DURATION); - animation.setInterpolator(new LinearInterpolator()); + animation.setInterpolator(interpolator); chargingIndicatorView.startAnimation(animation); if (isCenterBased) { - TranslateAnimation animationCenter = new TranslateAnimation(startXCenter, endX, 0, 0); + TranslateAnimation animationCenter = new TranslateAnimation(startXCenter, getWidth() - endX, 0, 0); animationCenter.setDuration(ANIM_DURATION); - animationCenter.setInterpolator(new LinearInterpolator()); + animationCenter.setInterpolator(interpolator); chargingIndicatorViewForCenter.startAnimation(animationCenter); } } @@ -360,8 +359,15 @@ public void setVisible(boolean visible) { private void refreshVisibility() { if (!mIsEnabled || mIsHidden || (onlyWhileCharging && !isCharging())) { barView.setVisibility(GONE); + stopChargingAnimation(); } else { barView.setVisibility(VISIBLE); + + if(isCharging() && animateCharging) { + startChargingAnimation(); + } else { + stopChargingAnimation(); + } } }