Skip to content

Commit

Permalink
[ProgressIndicator] Added circular indeterminate animation options - …
Browse files Browse the repository at this point in the history
…advance (M3) and retreat (new).

PiperOrigin-RevId: 627941610
  • Loading branch information
pekingme authored and leticiarossi committed Apr 26, 2024
1 parent cf143d0 commit 373008a
Show file tree
Hide file tree
Showing 12 changed files with 433 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,7 @@ protected BaseProgressIndicator(
// ******************** Initialization **********************

private void registerAnimationCallbacks() {
if (getProgressDrawable() != null && getIndeterminateDrawable() != null) {
// Registers the animation callback to switch indeterminate mode at the end of indeterminate
// animation.
getIndeterminateDrawable()
.getAnimatorDelegate()
.registerAnimatorsCompleteCallback(switchIndeterminateModeCallback);
}
registerSwitchIndeterminateModeCallback();

// Registers the hide animation callback to determinate drawable.
if (getProgressDrawable() != null) {
Expand All @@ -182,6 +176,16 @@ private void registerAnimationCallbacks() {
}
}

void registerSwitchIndeterminateModeCallback() {
if (getProgressDrawable() != null && getIndeterminateDrawable() != null) {
// Registers the animation callback to switch indeterminate mode at the end of indeterminate
// animation.
getIndeterminateDrawable()
.getAnimatorDelegate()
.registerAnimatorsCompleteCallback(switchIndeterminateModeCallback);
}
}

private void unregisterAnimationCallbacks() {
if (getIndeterminateDrawable() != null) {
getIndeterminateDrawable().unregisterAnimationCallback(hideAnimationCallback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ void fillIndicator(
@NonNull ActiveIndicator activeIndicator,
@IntRange(from = 0, to = 255) int drawableAlpha) {
int color = MaterialColors.compositeARGBWithAlpha(activeIndicator.color, drawableAlpha);
canvas.save();
canvas.rotate(activeIndicator.rotationDegree);
drawArc(
canvas,
paint,
Expand All @@ -183,6 +185,7 @@ void fillIndicator(
activeIndicator.amplitudeFraction,
activeIndicator.phaseFraction,
/* shouldDrawActiveIndicator= */ true);
canvas.restore();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2020 The Android Open Source Project
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,9 +29,10 @@

/**
* This is the implementation class for drawing progress indicator in the circular indeterminate
* mode.
* mode with an animation that grows the active segment by modifying the end point and shrinks it by
* modifying the start point.
*/
final class CircularIndeterminateAnimatorDelegate
final class CircularIndeterminateAdvanceAnimatorDelegate
extends IndeterminateAnimatorDelegate<ObjectAnimator> {

// Constants for animation timing.
Expand Down Expand Up @@ -63,7 +64,7 @@ final class CircularIndeterminateAnimatorDelegate
private float completeEndFraction;
AnimationCallback animatorCompleteCallback = null;

public CircularIndeterminateAnimatorDelegate(@NonNull CircularProgressIndicatorSpec spec) {
public CircularIndeterminateAdvanceAnimatorDelegate(@NonNull CircularProgressIndicatorSpec spec) {
super(/* indicatorCount= */ 1);

baseSpec = spec;
Expand Down Expand Up @@ -189,7 +190,7 @@ private void maybeUpdateSegmentColors(int playtime) {
for (int cycleIndex = 0; cycleIndex < TOTAL_CYCLES; cycleIndex++) {
float timeFraction =
getFractionInRange(playtime, DELAY_TO_FADE_IN_MS[cycleIndex], DURATION_TO_FADE_IN_MS);
if (timeFraction >= 0 && timeFraction <= 1) {
if (timeFraction > 0 && timeFraction < 1) {
int startColorIndex =
(cycleIndex + indicatorColorIndexOffset) % baseSpec.indicatorColors.length;
int endColorIndex = (startColorIndex + 1) % baseSpec.indicatorColors.length;
Expand Down Expand Up @@ -237,30 +238,32 @@ private void setCompleteEndFraction(float fraction) {

// ******************* Properties *******************

private static final Property<CircularIndeterminateAnimatorDelegate, Float> ANIMATION_FRACTION =
new Property<CircularIndeterminateAnimatorDelegate, Float>(Float.class, "animationFraction") {
@Override
public Float get(CircularIndeterminateAnimatorDelegate delegate) {
return delegate.getAnimationFraction();
}
private static final Property<CircularIndeterminateAdvanceAnimatorDelegate, Float>
ANIMATION_FRACTION =
new Property<CircularIndeterminateAdvanceAnimatorDelegate, Float>(
Float.class, "animationFraction") {
@Override
public Float get(CircularIndeterminateAdvanceAnimatorDelegate delegate) {
return delegate.getAnimationFraction();
}

@Override
public void set(CircularIndeterminateAnimatorDelegate delegate, Float value) {
delegate.setAnimationFraction(value);
}
};
@Override
public void set(CircularIndeterminateAdvanceAnimatorDelegate delegate, Float value) {
delegate.setAnimationFraction(value);
}
};

private static final Property<CircularIndeterminateAnimatorDelegate, Float>
private static final Property<CircularIndeterminateAdvanceAnimatorDelegate, Float>
COMPLETE_END_FRACTION =
new Property<CircularIndeterminateAnimatorDelegate, Float>(
new Property<CircularIndeterminateAdvanceAnimatorDelegate, Float>(
Float.class, "completeEndFraction") {
@Override
public Float get(CircularIndeterminateAnimatorDelegate delegate) {
public Float get(CircularIndeterminateAdvanceAnimatorDelegate delegate) {
return delegate.getCompleteEndFraction();
}

@Override
public void set(CircularIndeterminateAnimatorDelegate delegate, Float value) {
public void set(CircularIndeterminateAdvanceAnimatorDelegate delegate, Float value) {
delegate.setCompleteEndFraction(value);
}
};
Expand Down
Loading

0 comments on commit 373008a

Please sign in to comment.