Skip to content

Commit e1d9796

Browse files
pekingmewcshi
authored andcommitted
[ProgressIndicator] Combined attributes show/hideBehaviorLinear and show/hideBehaviorCircular.
PiperOrigin-RevId: 342086777
1 parent 6076715 commit e1d9796

15 files changed

+189
-257
lines changed

catalog/java/io/material/catalog/progressindicator/res/layout/cat_progress_indicator_determinate_fragment.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
android:layout_height="wrap_content"
5252
app:indicatorColor="@array/cat_custom_progress_colors"
5353
app:indicatorDirectionLinear="leftToRight"
54-
app:showBehaviorLinear="downward"
55-
app:hideBehaviorLinear="upward"/>
54+
app:showAnimationBehavior="inward"
55+
app:hideAnimationBehavior="outward"/>
5656

5757
<TextView
5858
android:layout_width="wrap_content"
@@ -63,8 +63,8 @@
6363
android:layout_height="wrap_content"
6464
app:indicatorColor="@array/cat_custom_progress_colors"
6565
app:indicatorDirectionLinear="rightToLeft"
66-
app:showBehaviorLinear="upward"
67-
app:hideBehaviorLinear="downward"/>
66+
app:showAnimationBehavior="outward"
67+
app:hideAnimationBehavior="inward"/>
6868

6969
<LinearLayout
7070
android:layout_width="match_parent"
@@ -121,8 +121,8 @@
121121
android:layout_gravity="center"
122122
style="@style/Widget.MaterialComponents.CircularProgressIndicator"
123123
app:indicatorColor="@array/cat_custom_progress_colors"
124-
app:showBehaviorCircular="inward"
125-
app:hideBehaviorCircular="outward"/>
124+
app:showAnimationBehavior="inward"
125+
app:hideAnimationBehavior="outward"/>
126126
</LinearLayout>
127127

128128
<LinearLayout
@@ -140,8 +140,8 @@
140140
android:layout_gravity="center"
141141
style="@style/Widget.MaterialComponents.CircularProgressIndicator"
142142
app:indicatorColor="@array/cat_custom_progress_colors"
143-
app:showBehaviorCircular="outward"
144-
app:hideBehaviorCircular="inward"/>
143+
app:showAnimationBehavior="outward"
144+
app:hideAnimationBehavior="inward"/>
145145
</LinearLayout>
146146
</LinearLayout>
147147

catalog/java/io/material/catalog/progressindicator/res/layout/cat_progress_indicator_indeterminate_indicators.xml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
android:indeterminate="true"
5050
app:indicatorColor="@array/cat_custom_progress_colors"
5151
app:indicatorDirectionLinear="leftToRight"
52-
app:showBehaviorLinear="downward"
53-
app:hideBehaviorLinear="upward"/>
52+
app:showAnimationBehavior="inward"
53+
app:hideAnimationBehavior="outward"/>
5454

5555
<TextView
5656
android:layout_width="wrap_content"
@@ -63,8 +63,8 @@
6363
app:indicatorColor="@array/cat_custom_progress_colors"
6464
app:indeterminateAnimationType="seamless"
6565
app:indicatorDirectionLinear="rightToLeft"
66-
app:showBehaviorLinear="upward"
67-
app:hideBehaviorLinear="downward"/>
66+
app:showAnimationBehavior="outward"
67+
app:hideAnimationBehavior="inward"/>
6868

6969
<LinearLayout
7070
android:layout_width="match_parent"
@@ -121,8 +121,8 @@
121121
android:indeterminate="true"
122122
style="@style/Widget.MaterialComponents.CircularProgressIndicator"
123123
app:indicatorColor="@array/cat_custom_progress_colors"
124-
app:showBehaviorCircular="inward"
125-
app:hideBehaviorCircular="outward"/>
124+
app:showAnimationBehavior="inward"
125+
app:hideAnimationBehavior="outward"/>
126126
</LinearLayout>
127127

128128
<LinearLayout
@@ -140,8 +140,8 @@
140140
android:indeterminate="true"
141141
style="@style/Widget.MaterialComponents.CircularProgressIndicator"
142142
app:indicatorColor="@array/cat_custom_progress_colors"
143-
app:showBehaviorCircular="outward"
144-
app:hideBehaviorCircular="inward"/>
143+
app:showAnimationBehavior="outward"
144+
app:hideAnimationBehavior="inward"/>
145145
</LinearLayout>
146146
</LinearLayout>
147147
</LinearLayout>

docs/components/ProgressIndicator.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ LinearProgressIndicator progress =
100100
layout.addView(progress, new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
101101
```
102102

103-
By setting the `showBehaviorLinear` and `hideBehaviorLinear`, the indicator can
103+
By setting the `showAnimationBehavior` and `hideAnimationBehavior`, the indicator can
104104
have different animation effects as follows:
105105

106106
<table>
107-
<tr><td colspan=2>showBehaviorLinear<td></tr>
107+
<tr><td colspan=2>showAnimationBehavior<td></tr>
108108
<tr><td>none</td><td>Appear immediately</td></tr>
109109
<tr><td>upward</td><td>Expanding from the bottom edge</td></tr>
110110
<tr><td>downward</td><td>Expanding from the top edge</td></tr>
111-
<tr><td colspan=2>hideBehaviorLinear<td></tr>
111+
<tr><td colspan=2>hideAnimationBehavior<td></tr>
112112
<tr><td>none</td><td>Disappear immediately</td></tr>
113113
<tr><td>upward</td><td>Collapsing to the top edge</td></tr>
114114
<tr><td>downward</td><td>Collapsing to the bottom edge</td></tr>
@@ -205,15 +205,15 @@ CircularProgressIndicator progress =
205205
layout.addView(progress, new LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
206206
```
207207

208-
By setting the `showBehaviorCircular` and `hideBehaviorCircular`, the indicator
208+
By setting the `showAnimationBehavior` and `hideAnimationBehavior`, the indicator
209209
can have different animation effects as follows:
210210

211211
<table>
212-
<tr><td colspan=2>showBehaviorCircular<td></tr>
212+
<tr><td colspan=2>showAnimationBehavior<td></tr>
213213
<tr><td>none</td><td>Appear immediately</td></tr>
214214
<tr><td>inward</td><td>Expanding from the outer edge</td></tr>
215215
<tr><td>outward</td><td>Expanding from the inner edge</td></tr>
216-
<tr><td colspan=2>hideBehaviorCircular<td></tr>
216+
<tr><td colspan=2>hideAnimationBehavior<td></tr>
217217
<tr><td>none</td><td>Disappear immediately</td></tr>
218218
<tr><td>inward</td><td>Collapsing to the inner edge</td></tr>
219219
<tr><td>outward</td><td>Collapsing to the outer edge</td></tr>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
public interface AnimatedVisibilityChangeBehavior {
2020

2121
/** Returns whether the visibility change should be animated while showing. */
22-
boolean shouldAnimateToShow();
22+
boolean isShowAnimationEnabled();
2323

2424
/** Returns whether the visibility change should be animated while hiding. */
25-
boolean shouldAnimateToHide();
25+
boolean isHideAnimationEnabled();
2626
}

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

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@
3333
import android.widget.ProgressBar;
3434
import androidx.annotation.AttrRes;
3535
import androidx.annotation.ColorInt;
36+
import androidx.annotation.IntDef;
3637
import androidx.annotation.NonNull;
3738
import androidx.annotation.Nullable;
3839
import androidx.annotation.Px;
40+
import androidx.annotation.RestrictTo;
41+
import androidx.annotation.RestrictTo.Scope;
3942
import androidx.annotation.VisibleForTesting;
4043
import androidx.vectordrawable.graphics.drawable.Animatable2Compat.AnimationCallback;
4144
import com.google.android.material.color.MaterialColors;
4245
import com.google.android.material.internal.ThemeEnforcement;
46+
import java.lang.annotation.Retention;
47+
import java.lang.annotation.RetentionPolicy;
4348
import java.util.Arrays;
4449

4550
/**
@@ -57,9 +62,19 @@
5762
* <li>{@code indicatorColor}: the color of the indicator.
5863
* <li>{@code trackColor}: the color of the track.
5964
* <li>{@code indicatorCornerRadius}: the radius of the rounded corner of the indicator stroke.
65+
* <li>{@code showAnimationBehavior}: the animation direction to show the indicator and track.
66+
* <li>{@code hideAnimationBehavior}: the animation direction to hide the indicator and track.
6067
* </ul>
6168
*/
6269
public class BaseProgressIndicator extends ProgressBar {
70+
// Constants for show/hide animation behaviors.
71+
public static final int SHOW_NONE = 0;
72+
public static final int SHOW_OUTWARD = 1;
73+
public static final int SHOW_INWARD = 2;
74+
public static final int HIDE_NONE = 0;
75+
public static final int HIDE_OUTWARD = 1;
76+
public static final int HIDE_INWARD = 2;
77+
6378
protected static final int DEF_STYLE_RES = R.style.Widget_MaterialComponents_ProgressIndicator;
6479

6580
protected static final float DEFAULT_OPACITY = 0.2f;
@@ -257,8 +272,8 @@ protected void onWindowVisibilityChanged(int visibility) {
257272
}
258273

259274
/**
260-
* If it changes to visible, the start animation will be started if {@code showBehavior} indicates
261-
* any. If it changes to invisible, hides the drawable immediately.
275+
* If it changes to visible, the start animation will be started if {@code showAnimationBehavior}
276+
* indicates any. If it changes to invisible, hides the drawable immediately.
262277
*
263278
* @param animationDesired Whether to change the visibility with animation.
264279
*/
@@ -619,6 +634,56 @@ public void setIndicatorCornerRadius(@Px int indicatorCornerRadius) {
619634
}
620635
}
621636

637+
/**
638+
* Returns the show animation behavior used in this progress indicator.
639+
*
640+
* @see #setShowAnimationBehavior(int)
641+
* @attr ref
642+
* com.google.android.material.progressindicator.R.stylable#BaseProgressIndicator_showAnimationBehavior
643+
*/
644+
@ShowAnimationBehavior
645+
public int getShowAnimationBehavior() {
646+
return baseSpec.showAnimationBehavior;
647+
}
648+
649+
/**
650+
* Sets the show animation behavior used in this progress indicator.
651+
*
652+
* @param showAnimationBehavior The new behavior of show animation.
653+
* @see #getShowAnimationBehavior()
654+
* @attr ref
655+
* com.google.android.material.progressindicator.R.stylable#BaseProgressIndicator_showAnimationBehavior
656+
*/
657+
public void setShowAnimationBehavior(@ShowAnimationBehavior int showAnimationBehavior) {
658+
baseSpec.showAnimationBehavior = showAnimationBehavior;
659+
invalidate();
660+
}
661+
662+
/**
663+
* Returns the hide animation behavior used in this progress indicator.
664+
*
665+
* @see #setHideAnimationBehavior(int)
666+
* @attr ref
667+
* com.google.android.material.progressindicator.R.stylable#BaseProgressIndicator_hideAnimationBehavior
668+
*/
669+
@HideAnimationBehavior
670+
public int getHideAnimationBehavior() {
671+
return baseSpec.hideAnimationBehavior;
672+
}
673+
674+
/**
675+
* Sets the hide animation behavior used in this progress indicator.
676+
*
677+
* @param hideAnimationBehavior The new behavior of hide animation.
678+
* @see #getHideAnimationBehavior()
679+
* @attr ref
680+
* com.google.android.material.progressindicator.R.stylable#BaseProgressIndicator_hideAnimationBehavior
681+
*/
682+
public void setHideAnimationBehavior(@HideAnimationBehavior int hideAnimationBehavior) {
683+
baseSpec.hideAnimationBehavior = hideAnimationBehavior;
684+
invalidate();
685+
}
686+
622687
/**
623688
* Sets the current progress to the specified value. Does not do anything if the progress bar is
624689
* in indeterminate mode. Animation is not used by default. This default setting is aligned with
@@ -751,4 +816,18 @@ public void onAnimationEnd(Drawable drawable) {
751816
}
752817
}
753818
};
819+
820+
// **************** Interface ****************
821+
822+
/** @hide */
823+
@RestrictTo(Scope.LIBRARY_GROUP)
824+
@IntDef({SHOW_NONE, SHOW_OUTWARD, SHOW_INWARD})
825+
@Retention(RetentionPolicy.SOURCE)
826+
public @interface ShowAnimationBehavior {}
827+
828+
/** @hide */
829+
@RestrictTo(Scope.LIBRARY_GROUP)
830+
@IntDef({HIDE_NONE, HIDE_OUTWARD, HIDE_INWARD})
831+
@Retention(RetentionPolicy.SOURCE)
832+
public @interface HideAnimationBehavior {}
754833
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@
3232
import androidx.annotation.Px;
3333
import com.google.android.material.color.MaterialColors;
3434
import com.google.android.material.internal.ThemeEnforcement;
35+
import com.google.android.material.progressindicator.BaseProgressIndicator.HideAnimationBehavior;
36+
import com.google.android.material.progressindicator.BaseProgressIndicator.ShowAnimationBehavior;
3537

3638
/**
3739
* This class contains the parameters shared between linear type and circular type. The parameters
3840
* reflect the attribtues defined in {@link R.styleable#BaseProgressIndicator}.
3941
*/
40-
public class BaseProgressIndicatorSpec {
42+
public class BaseProgressIndicatorSpec implements AnimatedVisibilityChangeBehavior {
4143
/** The size of the progress track and indicator. */
4244
@Px public int indicatorSize;
4345

@@ -61,6 +63,12 @@ public class BaseProgressIndicatorSpec {
6163
*/
6264
@ColorInt public int trackColor;
6365

66+
/** The animation behavior to show the indicator and track. */
67+
@ShowAnimationBehavior public int showAnimationBehavior;
68+
69+
/** The animation behavior to hide the indicator and track. */
70+
@HideAnimationBehavior public int hideAnimationBehavior;
71+
6472
protected BaseProgressIndicatorSpec(){}
6573

6674
/**
@@ -96,6 +104,14 @@ private void loadSpecFromAttributes(
96104
getDimensionPixelSize(
97105
context, a, R.styleable.BaseProgressIndicator_indicatorCornerRadius, 0),
98106
indicatorSize / 2);
107+
showAnimationBehavior =
108+
a.getInt(
109+
R.styleable.BaseProgressIndicator_showAnimationBehavior,
110+
BaseProgressIndicator.SHOW_NONE);
111+
hideAnimationBehavior =
112+
a.getInt(
113+
R.styleable.BaseProgressIndicator_hideAnimationBehavior,
114+
BaseProgressIndicator.HIDE_NONE);
99115

100116
loadIndicatorColors(context, a);
101117
loadTrackColor(context, a);
@@ -162,4 +178,14 @@ private void loadTrackColor(@NonNull Context context, @NonNull TypedArray typedA
162178
int trackAlpha = (int) (BaseProgressIndicator.MAX_ALPHA * defaultOpacity);
163179
trackColor = MaterialColors.compositeARGBWithAlpha(trackColor, trackAlpha);
164180
}
181+
182+
@Override
183+
public boolean isShowAnimationEnabled() {
184+
return showAnimationBehavior != BaseProgressIndicator.SHOW_NONE;
185+
}
186+
187+
@Override
188+
public boolean isHideAnimationEnabled() {
189+
return hideAnimationBehavior != BaseProgressIndicator.HIDE_NONE;
190+
}
165191
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,17 @@ public void adjustCanvas(
8585
displayedIndicatorSize = baseSpec.indicatorSize * indicatorSizeFraction;
8686
displayedCornerRadius = baseSpec.indicatorCornerRadius * indicatorSizeFraction;
8787
adjustedRadius = spec.indicatorRadius;
88-
if ((drawable.isShowing() && spec.showBehavior == CircularProgressIndicator.SHOW_INWARD)
89-
|| (drawable.isHiding() && spec.hideBehavior == CircularProgressIndicator.HIDE_OUTWARD)) {
88+
if ((drawable.isShowing()
89+
&& baseSpec.showAnimationBehavior == CircularProgressIndicator.SHOW_INWARD)
90+
|| (drawable.isHiding()
91+
&& baseSpec.hideAnimationBehavior == CircularProgressIndicator.HIDE_OUTWARD)) {
9092
// Increases the radius by half of the full size, then reduces it half way of the displayed
9193
// size to match the outer edges of the displayed indicator and the full indicator.
9294
adjustedRadius += (1 - indicatorSizeFraction) * baseSpec.indicatorSize / 2;
93-
} else if ((drawable.isShowing() && spec.showBehavior == CircularProgressIndicator.SHOW_OUTWARD)
94-
|| (drawable.isHiding() && spec.hideBehavior == CircularProgressIndicator.HIDE_INWARD)) {
95+
} else if ((drawable.isShowing()
96+
&& baseSpec.showAnimationBehavior == CircularProgressIndicator.SHOW_OUTWARD)
97+
|| (drawable.isHiding()
98+
&& baseSpec.hideAnimationBehavior == CircularProgressIndicator.HIDE_INWARD)) {
9599
// Decreases the radius by half of the full size, then raises it half way of the displayed
96100
// size to match the inner edges of the displayed indicator and the full indicator.
97101
adjustedRadius -= (1 - indicatorSizeFraction) * baseSpec.indicatorSize / 2;

0 commit comments

Comments
 (0)