-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathLoadingIndicatorDrawable.java
More file actions
246 lines (210 loc) · 7.68 KB
/
LoadingIndicatorDrawable.java
File metadata and controls
246 lines (210 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.material.loadingindicator;
import com.google.android.material.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.annotation.VisibleForTesting;
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat;
import com.google.android.material.progressindicator.AnimatorDurationScaleProvider;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
/** This class draws the graphics for a loading indicator. */
public final class LoadingIndicatorDrawable extends Drawable implements Drawable.Callback {
@NonNull private final Context context;
@NonNull private final LoadingIndicatorSpec specs;
@NonNull private LoadingIndicatorDrawingDelegate drawingDelegate;
@NonNull private LoadingIndicatorAnimatorDelegate animatorDelegate;
@NonNull Paint paint;
@NonNull AnimatorDurationScaleProvider animatorDurationScaleProvider;
@IntRange(from = 0, to = 255)
int alpha;
private Drawable staticDummyDrawable;
@NonNull
public static LoadingIndicatorDrawable create(
@NonNull Context context, @NonNull LoadingIndicatorSpec specs) {
LoadingIndicatorDrawable loadingIndicatorDrawable =
new LoadingIndicatorDrawable(
context,
specs,
new LoadingIndicatorDrawingDelegate(specs),
new LoadingIndicatorAnimatorDelegate(specs));
loadingIndicatorDrawable.setStaticDummyDrawable(
VectorDrawableCompat.create(context.getResources(), R.drawable.ic_mtrl_arrow_circle, null));
return loadingIndicatorDrawable;
}
LoadingIndicatorDrawable(
@NonNull Context context,
@NonNull LoadingIndicatorSpec specs,
@NonNull LoadingIndicatorDrawingDelegate drawingDelegate,
@NonNull LoadingIndicatorAnimatorDelegate animatorDelegate) {
this.context = context;
this.specs = specs;
this.drawingDelegate = drawingDelegate;
this.animatorDelegate = animatorDelegate;
animatorDurationScaleProvider = new AnimatorDurationScaleProvider();
this.paint = new Paint();
this.animatorDurationScaleProvider = new AnimatorDurationScaleProvider();
animatorDelegate.registerDrawable(this);
setAlpha(255);
}
// ******************* Overridden methods *******************
@Override
public int getIntrinsicWidth() {
return drawingDelegate.getPreferredWidth();
}
@Override
public int getIntrinsicHeight() {
return drawingDelegate.getPreferredHeight();
}
@Override
public void draw(@NonNull Canvas canvas) {
Rect clipBounds = new Rect();
Rect bounds = getBounds();
if (bounds.isEmpty() || !isVisible() || !canvas.getClipBounds(clipBounds)) {
// Escape if bounds are empty, clip bounds are empty, or currently hidden.
return;
}
if (isSystemAnimatorDisabled() && staticDummyDrawable != null) {
staticDummyDrawable.setBounds(bounds);
staticDummyDrawable.setTint(specs.indicatorColors[0]);
staticDummyDrawable.draw(canvas);
return;
}
canvas.save();
drawingDelegate.adjustCanvas(canvas, bounds);
drawingDelegate.drawContainer(canvas, paint, specs.containerColor, getAlpha());
drawingDelegate.drawIndicator(canvas, paint, animatorDelegate.indicatorState, getAlpha());
canvas.restore();
}
@CanIgnoreReturnValue
@Override
public boolean setVisible(boolean visible, boolean restart) {
return setVisible(visible, restart, /* animate= */ visible);
}
/**
* Changes the visibility with/without triggering the animation callbacks.
*
* @param visible Whether to make the drawable visible.
* @param restart Whether to force starting the animation from the beginning.
* @param animate Whether to change the visibility with animation.
* @return {@code true}, if the visibility changes or will change after the animation; {@code
* false}, otherwise.
* @see #setVisible(boolean, boolean, boolean)
*/
@CanIgnoreReturnValue
public boolean setVisible(boolean visible, boolean restart, boolean animate) {
boolean changed = super.setVisible(visible, restart);
animatorDelegate.cancelAnimatorImmediately();
// Restarts the main animator if it's visible and needs to be animated.
if (visible && animate && !isSystemAnimatorDisabled()) {
animatorDelegate.startAnimator();
}
return changed;
}
@Override
public void setAlpha(int alpha) {
if (this.alpha != alpha) {
this.alpha = alpha;
invalidateSelf();
}
}
@Override
public int getAlpha() {
return alpha;
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
paint.setColorFilter(colorFilter);
invalidateSelf();
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public void invalidateDrawable(@NonNull Drawable drawable) {
Drawable.Callback callback = getCallback();
if (callback != null) {
callback.invalidateDrawable(this);
}
}
@Override
public void scheduleDrawable(@NonNull Drawable who, @NonNull Runnable what, long when) {
Drawable.Callback callback = getCallback();
if (callback != null) {
callback.scheduleDrawable(this, what, when);
}
}
@Override
public void unscheduleDrawable(@NonNull Drawable who, @NonNull Runnable what) {
Drawable.Callback callback = getCallback();
if (callback != null) {
callback.unscheduleDrawable(this, what);
}
}
// ******************* Utility functions *******************
private boolean isSystemAnimatorDisabled() {
float systemAnimatorDurationScale =
animatorDurationScaleProvider.getSystemAnimatorDurationScale(context.getContentResolver());
return systemAnimatorDurationScale == 0;
}
// ******************* Setter and getter *******************
/**
* Returns the drawable that will be used when the system animator is disabled.
*
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
@Nullable
public Drawable getStaticDummyDrawable() {
return staticDummyDrawable;
}
/**
* Sets the drawable that will be used when the system animator is disabled.
*
* @hide
*/
@RestrictTo(Scope.LIBRARY_GROUP)
@VisibleForTesting
public void setStaticDummyDrawable(@Nullable Drawable staticDummyDrawable) {
this.staticDummyDrawable = staticDummyDrawable;
}
@NonNull
LoadingIndicatorAnimatorDelegate getAnimatorDelegate() {
return animatorDelegate;
}
void setAnimatorDelegate(@NonNull LoadingIndicatorAnimatorDelegate animatorDelegate) {
this.animatorDelegate = animatorDelegate;
animatorDelegate.registerDrawable(this);
}
@NonNull
LoadingIndicatorDrawingDelegate getDrawingDelegate() {
return drawingDelegate;
}
void setDrawingDelegate(@NonNull LoadingIndicatorDrawingDelegate drawingDelegate) {
this.drawingDelegate = drawingDelegate;
}
}