Skip to content

Commit

Permalink
Merge pull request #895
Browse files Browse the repository at this point in the history
BatteryBarView: improved animation
  • Loading branch information
siavash79 authored Sep 29, 2024
2 parents f71551a + 725b156 commit 2423726
Showing 1 changed file with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class BatteryBarView extends FrameLayout {
private final ShapeDrawable mDrawable = new ShapeDrawable();
FrameLayout maskLayout;
private final ImageView chargingIndicatorView;
private final ImageView chargingIndicatorViewForCenter;
private boolean colorful = false;
private int alphaPct = 100;
private int singleColorTone = Color.WHITE;
Expand Down Expand Up @@ -106,6 +107,7 @@ public void refreshLayout() {
maskLayout.setLayoutParams(maskLayoutParams());
barView.setLayoutParams(barLayoutParams());
chargingIndicatorView.setLayoutParams(charginLayoutParams());
chargingIndicatorViewForCenter.setLayoutParams(charginLayoutParams());

refreshColors(barView.getWidth(), barView.getHeight());
mDrawable.invalidateSelf();
Expand Down Expand Up @@ -152,28 +154,42 @@ private void stopChargingAnimation() {
if (chargingAnimationRunnable != null) {
animationHandler.removeCallbacks(chargingAnimationRunnable);
chargingIndicatorView.post(() -> chargingIndicatorView.setVisibility(GONE));
chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(GONE));
chargingAnimationRunnable = null;
}
}

private void animateChargingIndicator() {
chargingIndicatorView.post(() -> chargingIndicatorView.setVisibility(VISIBLE));
int screenWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
float startX, endX;

chargingIndicatorView.post(() -> chargingIndicatorView.setVisibility(VISIBLE));
int secondaryVisiblity = (isCenterBased) ? GONE : VISIBLE;
chargingIndicatorViewForCenter.post(() -> chargingIndicatorViewForCenter.setVisibility(secondaryVisiblity));

float startX, startXCenter, endX;

if (RTL) {
startX = 0;
startXCenter = getWidth();
endX = getWidth() - Math.round(getWidth() * getCurrentLevel() / 100f);
} else {
startX = screenWidth;
startXCenter = 0;
endX = Math.round(getWidth() * getCurrentLevel() / 100f);
}
if (isCenterBased) endX = getWidth() / 2f + chargingIndicatorView.getWidth() / 2f;
if (isCenterBased) endX = getWidth() / 2f;

TranslateAnimation animation = new TranslateAnimation(startX, endX, 0, 0);
animation.setDuration(ANIM_DURATION);
animation.setInterpolator(new LinearInterpolator());
chargingIndicatorView.startAnimation(animation);

if (isCenterBased) {
TranslateAnimation animationCenter = new TranslateAnimation(startXCenter, endX, 0, 0);
animationCenter.setDuration(ANIM_DURATION);
animationCenter.setInterpolator(new LinearInterpolator());
chargingIndicatorViewForCenter.startAnimation(animationCenter);
}
}

public BatteryBarView(Context context) {
Expand All @@ -192,12 +208,17 @@ public BatteryBarView(Context context) {
chargingIndicatorView.setLayoutParams(new LayoutParams(20, barHeight));
chargingIndicatorView.setBackgroundColor(singleColorTone);

chargingIndicatorViewForCenter = new ImageView(context);
chargingIndicatorViewForCenter.setLayoutParams(new LayoutParams(20, barHeight));
chargingIndicatorViewForCenter.setBackgroundColor(singleColorTone);

maskLayout = new FrameLayout(context);
maskLayout.addView(barView);
maskLayout.setClipChildren(true);

this.addView(maskLayout);
this.addView(chargingIndicatorView);
this.addView(chargingIndicatorViewForCenter);
this.setClipChildren(true);

RTL = (TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == LAYOUT_DIRECTION_RTL);
Expand Down Expand Up @@ -230,8 +251,7 @@ private LayoutParams charginLayoutParams() {
int pixels = (int) (metrics.density * dp + 0.5f);
LayoutParams result = new LayoutParams(pixels, barHeight);

result.gravity = (RTL) ? Gravity.CENTER : Gravity.START;

result.gravity = (RTL) ? Gravity.END : Gravity.START;
result.gravity |= (onTop) ? Gravity.TOP : Gravity.BOTTOM;

return result;
Expand Down Expand Up @@ -262,10 +282,12 @@ public void refreshColors(int lenX, int lenY) {
if (isFastCharging() && indicateFastCharging) //fast charging color
{
chargingIndicatorView.setBackgroundColor(fastChargingColor);
chargingIndicatorViewForCenter.setBackgroundColor(fastChargingColor);
mPaint.setColor(fastChargingColor);
} else if (isCharging() && indicateCharging) //normal charging color
{
chargingIndicatorView.setBackgroundColor(chargingColor);
chargingIndicatorViewForCenter.setBackgroundColor(chargingColor);
mPaint.setColor(chargingColor);
} else if (isPowerSaving() && indicatePowerSave) //power saving color
{
Expand Down

0 comments on commit 2423726

Please sign in to comment.