Skip to content

Commit 53e9347

Browse files
committed
support setColorSchemeColors and update demo
1 parent 15b87c2 commit 53e9347

2 files changed

Lines changed: 62 additions & 18 deletions

File tree

  • materialloadingprogressbardemo/src/main/java/com/lsjwzh/materialloadingprogressbardemo
  • materialloadingprogressbar/src/main/java/com/lsjwzh/widget/materialloadingprogressbar

materialloadingprogressbar/src/main/java/com/lsjwzh/widget/materialloadingprogressbar/CircleProgressBar.java

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public class CircleProgressBar extends ImageView {
7474
private MaterialProgressDrawable mProgressDrawable;
7575
private ShapeDrawable mBgCircle;
7676
private boolean mCircleBackgroundEnabled;
77+
private int[] mColors;
7778

7879
public CircleProgressBar(Context context) {
7980
super(context);
@@ -154,6 +155,8 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
154155
mTextPaint.setTextSize(mTextSize);
155156
mTextPaint.setAntiAlias(true);
156157
a.recycle();
158+
mProgressDrawable = new MaterialProgressDrawable(getContext(), this);
159+
super.setImageDrawable(mProgressDrawable);
157160
}
158161

159162

@@ -201,23 +204,21 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
201204
mBgCircle.getPaint().setColor(mBackGroundColor);
202205
setBackgroundDrawable(mBgCircle);
203206
}
204-
if(getDrawable()==null) {
205-
mProgressDrawable = new MaterialProgressDrawable(getContext(), this);
206-
mProgressDrawable.setBackgroundColor(mBackGroundColor);
207-
// mProgressDrawable.setStartEndTrim(0, 180);
208-
mProgressDrawable.setSizeParameters(mDiameter, mDiameter,
209-
mInnerRadius <= 0 ? (mDiameter - mProgressStokeWidth * 2) / 4 : mInnerRadius,
210-
mProgressStokeWidth,
211-
mArrowWidth < 0 ? mProgressStokeWidth * 4 : mArrowWidth,
212-
mArrowHeight < 0 ? mProgressStokeWidth * 2 : mArrowHeight);
213-
if(isShowArrow()){
214-
mProgressDrawable.setArrowScale(1f);
215-
mProgressDrawable.showArrow(true);
216-
}
217-
super.setImageDrawable(mProgressDrawable);
218-
mProgressDrawable.setAlpha(255);
219-
mProgressDrawable.start();
207+
mProgressDrawable.setBackgroundColor(mBackGroundColor);
208+
mProgressDrawable.setColorSchemeColors(mColors);
209+
mProgressDrawable.setSizeParameters(mDiameter, mDiameter,
210+
mInnerRadius <= 0 ? (mDiameter - mProgressStokeWidth * 2) / 4 : mInnerRadius,
211+
mProgressStokeWidth,
212+
mArrowWidth < 0 ? mProgressStokeWidth * 4 : mArrowWidth,
213+
mArrowHeight < 0 ? mProgressStokeWidth * 2 : mArrowHeight);
214+
if(isShowArrow()){
215+
mProgressDrawable.setArrowScale(1f);
216+
mProgressDrawable.showArrow(true);
220217
}
218+
super.setImageDrawable(null);
219+
super.setImageDrawable(mProgressDrawable);
220+
mProgressDrawable.setAlpha(255);
221+
mProgressDrawable.start();
221222
}
222223

223224
@Override
@@ -276,6 +277,36 @@ public void onAnimationEnd() {
276277
}
277278
}
278279

280+
281+
/**
282+
* Set the color resources used in the progress animation from color resources.
283+
* The first color will also be the color of the bar that grows in response
284+
* to a user swipe gesture.
285+
*
286+
* @param colorResIds
287+
*/
288+
public void setColorSchemeResources(int... colorResIds) {
289+
final Resources res = getResources();
290+
int[] colorRes = new int[colorResIds.length];
291+
for (int i = 0; i < colorResIds.length; i++) {
292+
colorRes[i] = res.getColor(colorResIds[i]);
293+
}
294+
setColorSchemeColors(colorRes);
295+
}
296+
297+
/**
298+
* Set the colors used in the progress animation. The first
299+
* color will also be the color of the bar that grows in response to a user
300+
* swipe gesture.
301+
*
302+
* @param colors
303+
*/
304+
public void setColorSchemeColors(int... colors) {
305+
mColors = colors;
306+
if(mProgressDrawable!=null) {
307+
mProgressDrawable.setColorSchemeColors(colors);
308+
}
309+
}
279310
/**
280311
* Update the background color of the mBgCircle image view.
281312
*/

materialloadingprogressbardemo/src/main/java/com/lsjwzh/materialloadingprogressbardemo/MainActivity.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@
1313
public class MainActivity extends ActionBarActivity {
1414
int progress = 0;
1515
private Handler handler;
16+
CircleProgressBar progress1;
17+
CircleProgressBar progress2;
18+
CircleProgressBar progressWithArrow;
19+
CircleProgressBar progressWithoutBg;
1620

1721
@Override
1822
protected void onCreate(Bundle savedInstanceState) {
1923
super.onCreate(savedInstanceState);
2024
setContentView(R.layout.activity_main);
21-
((CircleProgressBar) findViewById(R.id.progress1)).setCircleBackgroundEnabled(false);
22-
final CircleProgressBar progress2 = (CircleProgressBar) findViewById(R.id.progress2);
25+
progress1 = (CircleProgressBar) findViewById(R.id.progress1);
26+
progress2 = (CircleProgressBar) findViewById(R.id.progress2);
27+
progressWithArrow = (CircleProgressBar) findViewById(R.id.progressWithArrow);
28+
progressWithoutBg = (CircleProgressBar) findViewById(R.id.progressWithoutBg);
29+
30+
31+
progress1.setColorSchemeResources(android.R.color.holo_blue_bright);
32+
progress2.setColorSchemeResources(android.R.color.holo_green_light);
33+
34+
progressWithArrow.setColorSchemeResources(android.R.color.holo_orange_light);
35+
progressWithoutBg.setColorSchemeResources(android.R.color.holo_red_light);
2336

2437
handler = new Handler();
2538
for (int i = 0; i < 10; i++) {

0 commit comments

Comments
 (0)