Skip to content

Commit 24cddab

Browse files
pekingmedsn5ft
authored andcommitted
[ProgressIndicator] Added APIs for customizing wave amplitude ramping.
PiperOrigin-RevId: 784177043
1 parent 7a189d5 commit 24cddab

File tree

6 files changed

+72
-10
lines changed

6 files changed

+72
-10
lines changed

docs/components/ProgressIndicator.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ Element | Attribute
7878
**Wave amplitude** | `app:waveAmplitude` | `setWaveAmplitude`<br>`getWaveAmplitude` | 0
7979
**Wave speed** | `app:waveSpeed` | `setWaveSpeed`<br>`getWaveSpeed` | 0
8080
**Indeterminate animator duration scale** | `app:indeterminateAnimatorDurationScale` | `setIndeterminateAnimatorDurationScale` | 1
81+
**Wave amplitude ramp up progress** | `app:waveAmplitudeRampProgressMin` | `setWaveAmplitudeRampProgressRange` | 0.1
82+
**Wave amplitude ramp down progress** | `app:waveAmplitudeRampProgressMax` | `setWaveAmplitudeRampProgressRange` | 0.9
8183

8284
### Linear type specific attributes
8385

lib/java/com/google/android/material/progressindicator/BaseProgressIndicator.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,7 @@ public IndeterminateDrawable<S> getIndeterminateDrawable() {
449449
* attached to a window and whether it and its ancestors are visible.
450450
*/
451451
boolean visibleToUser() {
452-
return isAttachedToWindow()
453-
&& getWindowVisibility() == View.VISIBLE
454-
&& isEffectivelyVisible();
452+
return isAttachedToWindow() && getWindowVisibility() == View.VISIBLE && isEffectivelyVisible();
455453
}
456454

457455
/**
@@ -474,8 +472,8 @@ && getWindowVisibility() == View.VISIBLE
474472
* #isShown()} on parent changes that result from {@link
475473
* android.view.ViewGroup#attachViewToParent(View, int, ViewGroup.LayoutParams)}, which *can*
476474
* change our effective visibility. So this method errs on the side of assuming visibility unless
477-
* we can conclusively prove otherwise (but may result in some false positives, if this view
478-
* ends up being attached to a non-visible hierarchy after being detached in a visible state).
475+
* we can conclusively prove otherwise (but may result in some false positives, if this view ends
476+
* up being attached to a non-visible hierarchy after being detached in a visible state).
479477
*/
480478
boolean isEffectivelyVisible() {
481479
View current = this;
@@ -821,6 +819,28 @@ public void setWaveSpeed(@Px int waveSpeed) {
821819
getProgressDrawable().setEnforcedDrawing(spec.waveSpeed != 0);
822820
}
823821

822+
/**
823+
* Sets the progress for ramping up the full wave amplitude. If progress is outside the range,
824+
* track is flat.
825+
*
826+
* @param progress The progress to ramp up wave amplitude.
827+
*/
828+
public void setWaveAmplitudeRampProgressMin(float progress) {
829+
getProgressDrawable().setWaveAmplitudeRampProgressMin(progress);
830+
invalidate();
831+
}
832+
833+
/**
834+
* Sets the progress for ramping down the full wave amplitude. If progress is outside the range,
835+
* track is flat.
836+
*
837+
* @param progress The progress to ramp down wave amplitude.
838+
*/
839+
public void setWaveAmplitudeRampProgressMax(float progress) {
840+
getProgressDrawable().setWaveAmplitudeRampProgressMax(progress);
841+
invalidate();
842+
}
843+
824844
/**
825845
* Returns the show animation behavior used in this progress indicator.
826846
*

lib/java/com/google/android/material/progressindicator/BaseProgressIndicatorSpec.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.google.android.material.R;
2020

21+
import static com.google.android.material.progressindicator.DeterminateDrawable.FULL_AMPLITUDE_PROGRESS_MAX;
22+
import static com.google.android.material.progressindicator.DeterminateDrawable.FULL_AMPLITUDE_PROGRESS_MIN;
2123
import static com.google.android.material.resources.MaterialResources.getDimensionPixelSize;
2224
import static java.lang.Math.abs;
2325
import static java.lang.Math.min;
@@ -102,6 +104,14 @@ public abstract class BaseProgressIndicatorSpec {
102104
@FloatRange(from = 0.1f, to = 10f)
103105
public float indeterminateAnimatorDurationScale;
104106

107+
/** The normalized progress, at which the full wave amplitude will be ramped up. */
108+
@FloatRange(from = 0f, to = 1f)
109+
public float waveAmplitudeRampProgressMin;
110+
111+
/** The normalized progress, at which the full wave amplitude will be ramped down. */
112+
@FloatRange(from = 0f, to = 1f)
113+
public float waveAmplitudeRampProgressMax;
114+
105115
/**
106116
* Instantiates BaseProgressIndicatorSpec.
107117
*
@@ -164,6 +174,14 @@ protected BaseProgressIndicatorSpec(
164174
waveSpeed = a.getDimensionPixelSize(R.styleable.BaseProgressIndicator_waveSpeed, 0);
165175
indeterminateAnimatorDurationScale =
166176
a.getFloat(R.styleable.BaseProgressIndicator_indeterminateAnimatorDurationScale, 1);
177+
waveAmplitudeRampProgressMin =
178+
a.getFloat(
179+
R.styleable.BaseProgressIndicator_waveAmplitudeRampProgressMin,
180+
FULL_AMPLITUDE_PROGRESS_MIN);
181+
waveAmplitudeRampProgressMax =
182+
a.getFloat(
183+
R.styleable.BaseProgressIndicator_waveAmplitudeRampProgressMax,
184+
FULL_AMPLITUDE_PROGRESS_MAX);
167185

168186
loadIndicatorColors(context, a);
169187
loadTrackColor(context, a);
@@ -247,7 +265,7 @@ public boolean hasWavyEffect(boolean isDeterminate) {
247265
&& ((!isDeterminate && wavelengthIndeterminate > 0)
248266
|| (isDeterminate && wavelengthDeterminate > 0));
249267
}
250-
268+
251269
/**
252270
* Returns the track corner radius in pixels.
253271
*

lib/java/com/google/android/material/progressindicator/DeterminateDrawable.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public final class DeterminateDrawable<S extends BaseProgressIndicatorSpec>
4040
// Constants for drawing progress.
4141
private static final int MAX_DRAWABLE_LEVEL = 10000;
4242
// Constants for amplitude animation.
43-
private static final float FULL_AMPLITUDE_FRACTION_MIN = 0.1f;
44-
private static final float FULL_AMPLITUDE_FRACTION_MAX = 0.9f;
43+
static final float FULL_AMPLITUDE_PROGRESS_MIN = 0.1f;
44+
static final float FULL_AMPLITUDE_PROGRESS_MAX = 0.9f;
4545

4646
// The constant for spring force stiffness.
4747
private static final float SPRING_FORCE_STIFFNESS = SpringForce.STIFFNESS_VERY_LOW;
@@ -288,8 +288,8 @@ void setLevelByFraction(float fraction) {
288288
}
289289

290290
private float getAmplitudeFractionFromLevel(int level) {
291-
return level >= FULL_AMPLITUDE_FRACTION_MIN * MAX_DRAWABLE_LEVEL
292-
&& level <= FULL_AMPLITUDE_FRACTION_MAX * MAX_DRAWABLE_LEVEL
291+
return level >= baseSpec.waveAmplitudeRampProgressMin * MAX_DRAWABLE_LEVEL
292+
&& level <= baseSpec.waveAmplitudeRampProgressMax * MAX_DRAWABLE_LEVEL
293293
? 1f
294294
: 0f;
295295
}
@@ -403,6 +403,16 @@ void setEnforcedDrawing(boolean enforced) {
403403
}
404404
}
405405

406+
void setWaveAmplitudeRampProgressMin(float progress) {
407+
baseSpec.waveAmplitudeRampProgressMin = progress;
408+
invalidateSelf();
409+
}
410+
411+
void setWaveAmplitudeRampProgressMax(float progress) {
412+
baseSpec.waveAmplitudeRampProgressMax = progress;
413+
invalidateSelf();
414+
}
415+
406416
// ******************* Properties *******************
407417

408418
private static final FloatPropertyCompat<DeterminateDrawable<?>> INDICATOR_LENGTH_IN_LEVEL =

lib/java/com/google/android/material/progressindicator/res-public/values/public.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
<public name="waveAmplitude" type="attr"/>
3333
<public name="waveSpeed" type="attr"/>
3434
<public name="indeterminateAnimatorDurationScale" type="attr"/>
35+
<public name="waveAmplitudeRampProgressMin" type="attr" />
36+
<public name="waveAmplitudeRampProgressMax" type="attr" />
3537

3638
<!-- LinearProgressIndicator attributes -->
3739
<public name="indeterminateAnimationType" type="attr"/>

lib/java/com/google/android/material/progressindicator/res/values/attrs.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@
119119
This is independent from the animator scale in the System settings.
120120
-->
121121
<attr name="indeterminateAnimatorDurationScale" format="float" />
122+
<!--
123+
Defines the normalized progress [0, 1], at which the full wave amplitude will be ramped up.
124+
When the progress is less than this, the track is shown as flat.
125+
-->
126+
<attr name="waveAmplitudeRampProgressMin" format="float" />
127+
<!--
128+
Defines the normalized progress [0, 1], at which the full wave amplitude will be ramped down.
129+
When the progress is greater than this, the track is shown as flat.
130+
-->
131+
<attr name="waveAmplitudeRampProgressMax" format="float" />
122132
</declare-styleable>
123133

124134
<declare-styleable name="LinearProgressIndicator">

0 commit comments

Comments
 (0)