forked from zhanghai/MaterialProgressBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HorizontalProgressDrawable.java
127 lines (111 loc) · 4 KB
/
HorizontalProgressDrawable.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
/*
* Copyright (c) 2015 Zhang Hai <[email protected]>
* All Rights Reserved.
*/
package me.zhanghai.android.materialprogressbar;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import me.zhanghai.android.materialprogressbar.internal.ThemeUtils;
/**
* A backported {@code Drawable} for determinate horizontal {@code ProgressBar}.
*/
public class HorizontalProgressDrawable extends LayerDrawable
implements IntrinsicPaddingDrawable, ShowTrackDrawable, TintableDrawable {
private int mSecondaryAlpha;
private SingleHorizontalProgressDrawable mTrackDrawable;
private SingleHorizontalProgressDrawable mSecondaryProgressDrawable;
private SingleHorizontalProgressDrawable mProgressDrawable;
/**
* Create a new {@code HorizontalProgressDrawable}.
*
* @param context the {@code Context} for retrieving style information.
*/
public HorizontalProgressDrawable(Context context) {
super(new Drawable[] {
new SingleHorizontalProgressDrawable(context),
new SingleHorizontalProgressDrawable(context),
new SingleHorizontalProgressDrawable(context)
});
setId(0, android.R.id.background);
mTrackDrawable = (SingleHorizontalProgressDrawable) getDrawable(0);
setId(1, android.R.id.secondaryProgress);
mSecondaryProgressDrawable = (SingleHorizontalProgressDrawable) getDrawable(1);
float disabledAlpha = ThemeUtils.getFloatFromAttrRes(android.R.attr.disabledAlpha, context);
mSecondaryAlpha = Math.round(disabledAlpha * 0xFF);
mSecondaryProgressDrawable.setAlpha(mSecondaryAlpha);
mSecondaryProgressDrawable.setShowTrack(false);
setId(2, android.R.id.progress);
mProgressDrawable = (SingleHorizontalProgressDrawable) getDrawable(2);
mProgressDrawable.setShowTrack(false);
}
/**
* {@inheritDoc}
*/
public boolean getShowTrack() {
return mTrackDrawable.getShowTrack();
}
/**
* {@inheritDoc}
*/
@Override
public void setShowTrack(boolean showTrack) {
if (mTrackDrawable.getShowTrack() != showTrack) {
mTrackDrawable.setShowTrack(showTrack);
// HACK: Make alpha act as if composited.
mSecondaryProgressDrawable.setAlpha(showTrack ? mSecondaryAlpha : 2 * mSecondaryAlpha);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean getUseIntrinsicPadding() {
return mTrackDrawable.getUseIntrinsicPadding();
}
/**
* {@inheritDoc}
*/
@Override
public void setUseIntrinsicPadding(boolean useIntrinsicPadding) {
mTrackDrawable.setUseIntrinsicPadding(useIntrinsicPadding);
mSecondaryProgressDrawable.setUseIntrinsicPadding(useIntrinsicPadding);
mProgressDrawable.setUseIntrinsicPadding(useIntrinsicPadding);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressLint("NewApi")
public void setTint(@ColorInt int tintColor) {
mTrackDrawable.setTint(tintColor);
mSecondaryProgressDrawable.setTint(tintColor);
mProgressDrawable.setTint(tintColor);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressLint("NewApi")
public void setTintList(@Nullable ColorStateList tint) {
mTrackDrawable.setTintList(tint);
mSecondaryProgressDrawable.setTintList(tint);
mProgressDrawable.setTintList(tint);
}
/**
* {@inheritDoc}
*/
@Override
@SuppressLint("NewApi")
public void setTintMode(@NonNull PorterDuff.Mode tintMode) {
mTrackDrawable.setTintMode(tintMode);
mSecondaryProgressDrawable.setTintMode(tintMode);
mProgressDrawable.setTintMode(tintMode);
}
}