forked from zhanghai/MaterialProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaterialProgressBar.java
346 lines (303 loc) · 12.6 KB
/
MaterialProgressBar.java
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright (c) 2015 Zhang Hai <[email protected]>
* All Rights Reserved.
*/
package me.zhanghai.android.materialprogressbar;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ProgressBar;
import me.zhanghai.android.materialprogressbar.internal.DrawableCompat;
/**
* A {@link ProgressBar} subclass that handles tasks related to backported progress drawable.
*/
public class MaterialProgressBar extends ProgressBar {
private static final String TAG = MaterialProgressBar.class.getSimpleName();
public static final int PROGRESS_STYLE_CIRCULAR = 0;
public static final int PROGRESS_STYLE_HORIZONTAL = 1;
private int mProgressStyle;
private TintInfo mProgressTint = new TintInfo();
public MaterialProgressBar(Context context) {
super(context);
init(context, null, 0, 0);
}
public MaterialProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0, 0);
}
public MaterialProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public MaterialProgressBar(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MaterialProgressBar,
defStyleAttr, defStyleRes);
mProgressStyle = a.getInt(R.styleable.MaterialProgressBar_mpb_progressStyle,
PROGRESS_STYLE_CIRCULAR);
boolean setBothDrawables = a.getBoolean(
R.styleable.MaterialProgressBar_mpb_setBothDrawables, false);
boolean useIntrinsicPadding = a.getBoolean(
R.styleable.MaterialProgressBar_mpb_useIntrinsicPadding, true);
boolean showTrack = a.getBoolean(R.styleable.MaterialProgressBar_mpb_showTrack,
mProgressStyle == PROGRESS_STYLE_HORIZONTAL);
if (a.hasValue(R.styleable.MaterialProgressBar_android_tint)) {
mProgressTint.mTintList = a.getColorStateList(
R.styleable.MaterialProgressBar_android_tint);
mProgressTint.mHasTintList = true;
}
if (a.hasValue(R.styleable.MaterialProgressBar_mpb_tintMode)) {
mProgressTint.mTintMode = DrawableCompat.parseTintMode(a.getInt(
R.styleable.MaterialProgressBar_mpb_tintMode, -1), null);
mProgressTint.mHasTintMode = true;
}
a.recycle();
switch (mProgressStyle) {
case PROGRESS_STYLE_CIRCULAR:
if (!isIndeterminate() || setBothDrawables) {
throw new UnsupportedOperationException(
"Determinate circular drawable is not yet supported");
} else {
setIndeterminateDrawable(new IndeterminateProgressDrawable(context));
}
break;
case PROGRESS_STYLE_HORIZONTAL:
if (isIndeterminate() || setBothDrawables) {
setIndeterminateDrawable(new IndeterminateHorizontalProgressDrawable(context));
}
if (!isIndeterminate() || setBothDrawables) {
setProgressDrawable(new HorizontalProgressDrawable(context));
}
break;
default:
throw new IllegalArgumentException("Unknown progress style: " + mProgressStyle);
}
setUseIntrinsicPadding(useIntrinsicPadding);
setShowTrack(showTrack);
}
/**
* Get the style of current progress drawable.
*
* @return The style of current progress drawable.
*/
public int getProgressStyle() {
return mProgressStyle;
}
/**
* Get the progress drawable of this ProgressBar.
*
* @return The progress drawable.
*/
public Drawable getDrawable() {
return isIndeterminate() ? getIndeterminateDrawable() : getProgressDrawable();
}
/**
* Get whether the progress drawable is using an intrinsic padding. The default is {@code true}.
*
* @return Whether the progress drawable is using an intrinsic padding.
* @throws IllegalStateException If the progress drawable does not implement
* {@link IntrinsicPaddingDrawable}.
*/
public boolean getUseIntrinsicPadding() {
Drawable drawable = getDrawable();
if (drawable instanceof IntrinsicPaddingDrawable) {
return ((IntrinsicPaddingDrawable) drawable).getUseIntrinsicPadding();
} else {
throw new IllegalStateException("Drawable does not implement IntrinsicPaddingDrawable");
}
}
/**
* Set whether the progress drawable should use an intrinsic padding. The default is {@code true}.
*
* @param useIntrinsicPadding Whether the progress drawable should use its intrinsic padding.
* @throws IllegalStateException If the progress drawable does not implement
* {@link IntrinsicPaddingDrawable}.
*/
public void setUseIntrinsicPadding(boolean useIntrinsicPadding) {
Drawable drawable = getDrawable();
if (drawable instanceof IntrinsicPaddingDrawable) {
((IntrinsicPaddingDrawable) drawable).setUseIntrinsicPadding(useIntrinsicPadding);
}
Drawable indeterminateDrawable = getIndeterminateDrawable();
if (indeterminateDrawable instanceof IntrinsicPaddingDrawable) {
((IntrinsicPaddingDrawable) indeterminateDrawable)
.setUseIntrinsicPadding(useIntrinsicPadding);
}
}
/**
* Get whether the progress drawable is showing a track. The default is {@code true}.
*
* @return Whether the progress drawable is showing a track, or {@code false} if the drawable
* does not implement {@link ShowTrackDrawable}.
*/
public boolean getShowTrack() {
Drawable drawable = getDrawable();
if (drawable instanceof ShowTrackDrawable) {
return ((ShowTrackDrawable) drawable).getShowTrack();
} else {
return false;
}
}
/**
* Set whether the progress drawable should show a track. The default is {@code true}.
*
* @param showTrack Whether track should be shown. When {@code false}, does nothing if the
* progress drawable does not implement {@link ShowTrackDrawable}. When
* {@code true}, throws {@link IllegalStateException}.
* @throws IllegalStateException If {@code showTrack} is {@code true} but the progress drawable
* does not implement {@link ShowTrackDrawable}.
*/
public void setShowTrack(boolean showTrack) {
Drawable drawable = getDrawable();
if (drawable instanceof ShowTrackDrawable) {
((ShowTrackDrawable) drawable).setShowTrack(showTrack);
}
Drawable indeterminateDrawable = getIndeterminateDrawable();
if (indeterminateDrawable instanceof ShowTrackDrawable) {
((ShowTrackDrawable) indeterminateDrawable).setShowTrack(showTrack);
}
}
@Override
public void setProgressDrawable(Drawable d) {
super.setProgressDrawable(d);
// mProgressTint can be null during super class initialization.
if (mProgressTint != null) {
applyDeterminateProgressTint();
}
}
@Override
public void setIndeterminateDrawable(Drawable d) {
super.setIndeterminateDrawable(d);
// mProgressTint can be null during super class initialization.
if (mProgressTint != null) {
applyIndeterminateProgressTint();
}
}
/**
* Return the tint applied to the progress drawable, if specified.
*
* @return The tint applied to the progress drawable.
* @see #setProgressTintList(ColorStateList)
*/
@Nullable
@Override
public ColorStateList getProgressTintList() {
return mProgressTint.mTintList;
}
/**
* Applies a tint to the progress drawable. Does not modify the current tint mode, which is
* {@link PorterDuff.Mode#SRC_IN} by default.
* <p>
* Subsequent calls to {@link #setBackground(Drawable)} will automatically mutate the drawable
* and apply the specified tint and tint mode using
* {@link Drawable#setTintList(ColorStateList)}.
*
* @param tint The tint to apply, may be {@code null} to clear tint.
*
* @see #getProgressTintList()
* @see Drawable#setTintList(ColorStateList)
*/
@Override
public void setProgressTintList(@Nullable ColorStateList tint) {
mProgressTint.mTintList = tint;
mProgressTint.mHasTintList = true;
applyProgressTint();
}
/**
* Return the blending mode used to apply the tint to the progress drawable, if specified.
*
* @return The blending mode used to apply the tint to the progress drawable.
* @see #setBackgroundTintMode(PorterDuff.Mode)
*/
@Nullable
@Override
public PorterDuff.Mode getProgressTintMode() {
return mProgressTint.mTintMode;
}
/**
* Specifies the blending mode used to apply the tint specified by
* {@link #setProgressTintList(ColorStateList)}} to the progress drawable. The default mode is
* {@link PorterDuff.Mode#SRC_IN}.
*
* @param tintMode The blending mode used to apply the tint, may be {@code null} to clear tint
* @see #getProgressTintMode()
* @see Drawable#setTintMode(PorterDuff.Mode)
*/
@Override
public void setProgressTintMode(@Nullable PorterDuff.Mode tintMode) {
mProgressTint.mTintMode = tintMode;
mProgressTint.mHasTintMode = true;
applyProgressTint();
}
private void applyProgressTint() {
applyDeterminateProgressTint();
applyIndeterminateProgressTint();
}
private void applyDeterminateProgressTint() {
if (mProgressTint.mHasTintList || mProgressTint.mHasTintMode) {
Drawable drawable = getProgressDrawable();
if (drawable != null) {
applyTintForDrawable(drawable, mProgressTint);
}
}
}
private void applyIndeterminateProgressTint() {
if (mProgressTint.mHasTintList || mProgressTint.mHasTintMode) {
Drawable drawable = getIndeterminateDrawable();
if (drawable != null) {
applyTintForDrawable(drawable, mProgressTint);
}
}
}
// Progress drawables in this library has already rewritten tint related methods for
// compatibility.
@SuppressLint("NewApi")
private void applyTintForDrawable(Drawable drawable, TintInfo tint) {
if (tint.mHasTintList || tint.mHasTintMode) {
if (tint.mHasTintList) {
if (drawable instanceof TintableDrawable) {
((TintableDrawable) drawable).setTintList(tint.mTintList);
} else {
Log.w(TAG, "drawable did not implement TintableDrawable, it won't be tinted below Lollipop");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable.setTintList(tint.mTintList);
}
}
}
if (tint.mHasTintMode) {
if (drawable instanceof TintableDrawable) {
((TintableDrawable) drawable).setTintMode(tint.mTintMode);
} else {
Log.w(TAG, "drawable did not implement TintableDrawable, it won't be tinted below Lollipop");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawable.setTintMode(tint.mTintMode);
}
}
}
// The drawable (or one of its children) may not have been
// stateful before applying the tint, so let's try again.
if (drawable.isStateful()) {
drawable.setState(getDrawableState());
}
}
}
private static class TintInfo {
boolean mHasTintList;
ColorStateList mTintList;
boolean mHasTintMode;
PorterDuff.Mode mTintMode;
}
}