Skip to content

Commit 22e054b

Browse files
pekingmedsn5ft
authored andcommittedNov 14, 2023
[ProgressIndicator] Added static drawable for Circular type when system animator is disabled.
PiperOrigin-RevId: 582068647
1 parent 9b9449c commit 22e054b

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed
 

‎lib/java/com/google/android/material/progressindicator/IndeterminateDrawable.java

+60-9
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,26 @@
1515
*/
1616
package com.google.android.material.progressindicator;
1717

18+
import com.google.android.material.R;
19+
1820
import static com.google.android.material.progressindicator.LinearProgressIndicator.INDETERMINATE_ANIMATION_TYPE_CONTIGUOUS;
1921

2022
import android.animation.ObjectAnimator;
2123
import android.content.Context;
2224
import android.graphics.Canvas;
2325
import android.graphics.Color;
2426
import android.graphics.Rect;
27+
import android.graphics.drawable.Drawable;
2528
import android.os.Build.VERSION;
2629
import android.os.Build.VERSION_CODES;
30+
import android.util.Log;
2731
import androidx.annotation.NonNull;
32+
import androidx.annotation.Nullable;
33+
import androidx.annotation.RestrictTo;
34+
import androidx.annotation.RestrictTo.Scope;
35+
import androidx.annotation.VisibleForTesting;
36+
import androidx.core.graphics.drawable.DrawableCompat;
37+
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
2838
import com.google.android.material.color.MaterialColors;
2939

3040
/** This class draws the graphics for indeterminate mode. */
@@ -36,6 +46,8 @@ public final class IndeterminateDrawable<S extends BaseProgressIndicatorSpec>
3646
// Animator delegate object.
3747
private IndeterminateAnimatorDelegate<ObjectAnimator> animatorDelegate;
3848

49+
private Drawable staticDummyDrawable;
50+
3951
IndeterminateDrawable(
4052
@NonNull Context context,
4153
@NonNull BaseProgressIndicatorSpec baseSpec,
@@ -76,11 +88,15 @@ public static IndeterminateDrawable<LinearProgressIndicatorSpec> createLinearDra
7688
@NonNull
7789
public static IndeterminateDrawable<CircularProgressIndicatorSpec> createCircularDrawable(
7890
@NonNull Context context, @NonNull CircularProgressIndicatorSpec spec) {
79-
return new IndeterminateDrawable<>(
80-
context,
81-
/* baseSpec= */ spec,
82-
new CircularDrawingDelegate(spec),
83-
new CircularIndeterminateAnimatorDelegate(spec));
91+
IndeterminateDrawable<CircularProgressIndicatorSpec> indeterminateDrawable =
92+
new IndeterminateDrawable<>(
93+
context,
94+
/* baseSpec= */ spec,
95+
new CircularDrawingDelegate(spec),
96+
new CircularIndeterminateAnimatorDelegate(spec));
97+
indeterminateDrawable.setStaticDummyDrawable(
98+
VectorDrawableCompat.create(context.getResources(), R.drawable.indeterminate_static, null));
99+
return indeterminateDrawable;
84100
}
85101

86102
// ******************* Overridden methods *******************
@@ -101,17 +117,18 @@ public static IndeterminateDrawable<CircularProgressIndicatorSpec> createCircula
101117
boolean setVisibleInternal(boolean visible, boolean restart, boolean animate) {
102118
boolean changed = super.setVisibleInternal(visible, restart, animate);
103119

120+
if (isSystemAnimatorDisabled() && staticDummyDrawable != null) {
121+
return staticDummyDrawable.setVisible(visible, restart);
122+
}
123+
104124
// Unless it's showing or hiding, cancels the main animator.
105125
if (!isRunning()) {
106126
animatorDelegate.cancelAnimatorImmediately();
107127
}
108128
// Restarts the main animator if it's visible and needs to be animated.
109-
float systemAnimatorDurationScale =
110-
animatorDurationScaleProvider.getSystemAnimatorDurationScale(context.getContentResolver());
111129
if (visible
112130
&& (animate
113-
|| (VERSION.SDK_INT <= VERSION_CODES.LOLLIPOP_MR1
114-
&& systemAnimatorDurationScale > 0))) {
131+
|| (VERSION.SDK_INT <= VERSION_CODES.LOLLIPOP_MR1 && !isSystemAnimatorDisabled()))) {
115132
animatorDelegate.startAnimator();
116133
}
117134

@@ -140,6 +157,14 @@ public void draw(@NonNull Canvas canvas) {
140157
return;
141158
}
142159

160+
if (isSystemAnimatorDisabled() && staticDummyDrawable != null) {
161+
staticDummyDrawable.setBounds(getBounds());
162+
Log.d("ProgressIndicator", "bounds: " + staticDummyDrawable.getBounds());
163+
DrawableCompat.setTint(staticDummyDrawable, baseSpec.indicatorColors[0]);
164+
staticDummyDrawable.draw(canvas);
165+
return;
166+
}
167+
143168
canvas.save();
144169
drawingDelegate.validateSpecAndAdjustCanvas(canvas, getBounds(), getGrowFraction());
145170

@@ -205,8 +230,34 @@ private void drawTrackIndicators(@NonNull Canvas canvas, int segmentIndex) {
205230
}
206231
}
207232

233+
// ******************* Utility functions *******************
234+
235+
private boolean isSystemAnimatorDisabled() {
236+
if (animatorDurationScaleProvider != null) {
237+
float systemAnimatorDurationScale =
238+
animatorDurationScaleProvider.getSystemAnimatorDurationScale(
239+
context.getContentResolver());
240+
return systemAnimatorDurationScale == 0;
241+
}
242+
return false;
243+
}
244+
208245
// ******************* Setter and getter *******************
209246

247+
/** @hide */
248+
@RestrictTo(Scope.LIBRARY_GROUP)
249+
@Nullable
250+
public Drawable getStaticDummyDrawable() {
251+
return staticDummyDrawable;
252+
}
253+
254+
/** @hide */
255+
@RestrictTo(Scope.LIBRARY_GROUP)
256+
@VisibleForTesting
257+
public void setStaticDummyDrawable(@Nullable Drawable staticDummyDrawable) {
258+
this.staticDummyDrawable = staticDummyDrawable;
259+
}
260+
210261
@NonNull
211262
IndeterminateAnimatorDelegate<ObjectAnimator> getAnimatorDelegate() {
212263
return animatorDelegate;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright (C) 2023 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:tools="http://schemas.android.com/tools"
19+
tools:ignore="NewApi"
20+
android:width="24.0dp"
21+
android:height="24.0dp"
22+
android:viewportWidth="24.0"
23+
android:viewportHeight="24.0">
24+
<path
25+
android:fillColor="@android:color/white"
26+
android:pathData="M17.65,6.35C16.2,4.9 14.21,4.0 12.0,4.0c-4.42,0.0 -7.99,3.58 -7.99,8.0s3.57,8.0 7.99,8.0c3.73,0.0 6.84,-2.55 7.73,-6.0l-2.08,0.0c-0.82,2.33 -3.04,4.0 -5.65,4.0 -3.31,0.0 -6.0,-2.69 -6.0,-6.0s2.69,-6.0 6.0,-6.0c1.66,0.0 3.1,0.69 4.22,1.78L13.0,11.0l7.0,0.0L20.0,4.0l-2.35,2.35z"/>
27+
</vector>

0 commit comments

Comments
 (0)
Please sign in to comment.