Skip to content
This repository was archived by the owner on Aug 22, 2020. It is now read-only.

Commit 1a661d2

Browse files
committed
StepperView: New attr lineColor & errorHighlightColor
Signed-off-by: Fung <fython@163.com>
1 parent 5b3ae6a commit 1a661d2

File tree

6 files changed

+181
-18
lines changed

6 files changed

+181
-18
lines changed

library/src/main/java/moe/feng/common/stepperview/IStepperView.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ interface IStepperView {
4040
*/
4141
@ColorInt int getActivatedColor();
4242

43+
/**
44+
* Get line color
45+
*
46+
* @return Line Color
47+
*/
48+
@ColorInt int getLineColor();
49+
50+
/**
51+
* Get error highlight color
52+
*
53+
* @return Error Highlight Color
54+
*/
55+
@ColorInt int getErrorColor();
56+
4357
/**
4458
* Get animation duration
4559
*

library/src/main/java/moe/feng/common/stepperview/VerticalStepperItemView.java

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import android.annotation.SuppressLint;
77
import android.content.Context;
88
import android.content.res.TypedArray;
9+
import android.graphics.PorterDuff;
910
import android.graphics.drawable.Drawable;
11+
import android.os.Build;
1012
import android.os.Bundle;
1113
import android.os.Parcelable;
1214
import android.support.annotation.*;
@@ -51,7 +53,7 @@ public class VerticalStepperItemView extends FrameLayout {
5153
* View attributes
5254
*/
5355
private int mAnimationDuration;
54-
private int mNormalColor, mActivatedColor;
56+
private int mNormalColor, mActivatedColor, mLineColor, mErrorColor;
5557
private Drawable mDoneIcon;
5658
private boolean mAnimationEnabled = true;
5759

@@ -77,14 +79,11 @@ public VerticalStepperItemView(Context context, AttributeSet attrs, int defStyle
7779

7880
prepareViews(context);
7981

80-
mNormalColor = ViewUtils.getColorFromAttr(context, android.R.attr.textColorSecondary);
81-
mActivatedColor = ViewUtils.getColorFromAttr(context, R.attr.colorPrimary);
82-
mDoneIcon = getResources().getDrawable(R.drawable.ic_done_white_16dp);
83-
mAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
8482
DP = getResources().getDimensionPixelSize(R.dimen.dp1);
8583

8684
if (attrs != null) {
87-
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalStepperItemView, defStyleAttr, 0);
85+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalStepperItemView,
86+
defStyleAttr, R.style.Widget_Stepper);
8887

8988
mTitle = a.getString(R.styleable.VerticalStepperItemView_step_title);
9089
mSummary = a.getString(R.styleable.VerticalStepperItemView_step_summary);
@@ -95,6 +94,8 @@ public VerticalStepperItemView(Context context, AttributeSet attrs, int defStyle
9594
mActivatedColor = a.getColor(R.styleable.VerticalStepperItemView_step_activated_color, mActivatedColor);
9695
mAnimationDuration = a.getInt(R.styleable.VerticalStepperItemView_step_animation_duration, mAnimationDuration);
9796
mAnimationEnabled = a.getBoolean(R.styleable.VerticalStepperItemView_step_enable_animation, true);
97+
mLineColor = a.getColor(R.styleable.VerticalStepperItemView_step_line_color, mLineColor);
98+
mErrorColor = a.getColor(R.styleable.VerticalStepperItemView_step_error_highlight_color, mErrorColor);
9899

99100
if (a.hasValue(R.styleable.VerticalStepperItemView_step_done_icon)) {
100101
mDoneIcon = a.getDrawable(R.styleable.VerticalStepperItemView_step_done_icon);
@@ -110,6 +111,8 @@ public VerticalStepperItemView(Context context, AttributeSet attrs, int defStyle
110111
setIsLastStep(isLastStep);
111112
setDoneIcon(mDoneIcon);
112113
setAnimationEnabled(mAnimationEnabled);
114+
setLineColor(mLineColor);
115+
setErrorColor(mErrorColor);
113116
}
114117

115118
@Override
@@ -216,13 +219,13 @@ public synchronized void setState(@State int state) {
216219
if (mErrorText != null) {
217220
mTitleColorAnimator = ObjectAnimator
218221
.ofArgb(mTitleText, "textColor",
219-
lastTitleTextColor, getResources().getColor(R.color.material_red_500));
222+
lastTitleTextColor, mErrorColor);
220223
mTitleColorAnimator.setDuration(mAnimationDuration);
221224
mTitleColorAnimator.start();
222225
if (mSummaryColorAnimator != null) mSummaryColorAnimator.cancel();
223226
mSummaryColorAnimator = ObjectAnimator
224227
.ofArgb(mSummaryText, "textColor",
225-
mSummaryText.getCurrentTextColor(), getResources().getColor(R.color.material_red_500));
228+
mSummaryText.getCurrentTextColor(), mErrorColor);
226229
mSummaryColorAnimator.setDuration(mAnimationDuration);
227230
mSummaryColorAnimator.start();
228231

@@ -241,7 +244,7 @@ public synchronized void setState(@State int state) {
241244
if (mSummaryColorAnimator != null) mSummaryColorAnimator.cancel();
242245
mSummaryColorAnimator = ObjectAnimator
243246
.ofArgb(mSummaryText, "textColor",
244-
mSummaryText.getCurrentTextColor(), getResources().getColor(R.color.material_grey_500));
247+
mSummaryText.getCurrentTextColor(), mLineColor);
245248
mSummaryColorAnimator.setDuration(mAnimationDuration);
246249
mSummaryColorAnimator.start();
247250

@@ -606,6 +609,72 @@ public void setActivatedColorResource(@ColorRes int colorRes) {
606609
setActivatedColor(getResources().getColor(colorRes));
607610
}
608611

612+
/**
613+
* Get line color
614+
*
615+
* @return Line Color
616+
*/
617+
public @ColorInt int getLineColor() {
618+
return mLineColor;
619+
}
620+
621+
/**
622+
* Set line color
623+
*
624+
* @param color Line Color
625+
*/
626+
public void setLineColor(@ColorInt int color) {
627+
mLineColor = color;
628+
mLineView.setBackgroundColor(color);
629+
}
630+
631+
/**
632+
* Set line color
633+
*
634+
* @param colorRes Line Color resource
635+
*/
636+
public void setLineColorResource(@ColorRes int colorRes) {
637+
setLineColor(getResources().getColor(colorRes));
638+
}
639+
640+
/**
641+
* Get error highlight color
642+
*
643+
* @return Error Highlight Color
644+
*/
645+
public @ColorInt int getErrorColor() {
646+
return mErrorColor;
647+
}
648+
649+
/**
650+
* Set error highlight color
651+
*
652+
* @param color Error Highlight Color
653+
*/
654+
public void setErrorColor(@ColorInt int color) {
655+
if (isPreLollipop()) {
656+
mErrorIconView.getDrawable().setColorFilter(color, PorterDuff.Mode.DST_IN);
657+
} else {
658+
mErrorIconView.getDrawable().setTint(color);
659+
}
660+
if (mErrorText != null && color != mErrorColor) {
661+
if (mTitleColorAnimator != null && mTitleColorAnimator.isRunning()) mTitleColorAnimator.cancel();
662+
if (mSummaryColorAnimator != null && mSummaryColorAnimator.isRunning()) mSummaryColorAnimator.cancel();
663+
mTitleText.setTextColor(color);
664+
mSummaryText.setTextColor(color);
665+
}
666+
mErrorColor = color;
667+
}
668+
669+
/**
670+
* Set error highlight color
671+
*
672+
* @param colorRes Error Highlight Color resource
673+
*/
674+
public void setErrorColorResource(@ColorRes int colorRes) {
675+
setErrorColor(getResources().getColor(colorRes));
676+
}
677+
609678
/**
610679
* Get activated point color
611680
*
@@ -634,6 +703,8 @@ public Parcelable onSaveInstanceState() {
634703
state.activatedColor = mActivatedColor;
635704
state.doneIcon = mDoneIcon;
636705
state.errorText = mErrorText;
706+
state.lineColor = mLineColor;
707+
state.errorColor = mErrorColor;
637708
bundle.putParcelable(ItemViewState.STATE, state);
638709
return bundle;
639710
}
@@ -654,6 +725,8 @@ public void onRestoreInstanceState(Parcelable state) {
654725
setActivatedColor(viewState.activatedColor);
655726
setDoneIcon(viewState.doneIcon);
656727
setErrorText(viewState.errorText);
728+
setLineColor(viewState.lineColor);
729+
setErrorColor(viewState.errorColor);
657730
return;
658731
}
659732
super.onRestoreInstanceState(BaseSavedState.EMPTY_STATE);
@@ -670,7 +743,7 @@ protected static class ItemViewState extends BaseSavedState {
670743
String errorText;
671744

672745
int animationDuration;
673-
int normalColor, activatedColor;
746+
int normalColor, activatedColor, lineColor, errorColor;
674747
Drawable doneIcon;
675748

676749
ItemViewState(Parcelable superState) {
@@ -679,4 +752,8 @@ protected static class ItemViewState extends BaseSavedState {
679752

680753
}
681754

755+
private static boolean isPreLollipop() {
756+
return Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;
757+
}
758+
682759
}

library/src/main/java/moe/feng/common/stepperview/VerticalStepperView.java

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class VerticalStepperView extends FrameLayout implements IStepperView {
3434
*/
3535
private boolean mAnimationEnabled;
3636
private int mAnimationDuration;
37-
private int mNormalColor, mActivatedColor;
37+
private int mNormalColor, mActivatedColor, mLineColor, mErrorColor;
3838
private Drawable mDoneIcon;
3939

4040
public VerticalStepperView(Context context) {
@@ -48,20 +48,18 @@ public VerticalStepperView(Context context, AttributeSet attrs) {
4848
public VerticalStepperView(Context context, AttributeSet attrs, int defStyleAttr) {
4949
super(context, attrs, defStyleAttr);
5050

51-
mNormalColor = getResources().getColor(R.color.material_grey_500);
52-
mActivatedColor = ViewUtils.getColorFromAttr(context, R.attr.colorPrimary);
53-
mDoneIcon = getResources().getDrawable(R.drawable.ic_done_white_16dp);
54-
mAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
55-
5651
prepareListView(context);
5752

5853
if (attrs != null) {
59-
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalStepperView, defStyleAttr, 0);
54+
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalStepperView,
55+
defStyleAttr, R.style.Widget_Stepper);
6056

6157
mNormalColor = a.getColor(R.styleable.VerticalStepperView_step_normal_color, mNormalColor);
6258
mActivatedColor = a.getColor(R.styleable.VerticalStepperView_step_activated_color, mActivatedColor);
6359
mAnimationDuration = a.getInt(R.styleable.VerticalStepperView_step_animation_duration, mAnimationDuration);
6460
mAnimationEnabled = a.getBoolean(R.styleable.VerticalStepperView_step_enable_animation, true);
61+
mLineColor = a.getColor(R.styleable.VerticalStepperView_step_line_color, mLineColor);
62+
mErrorColor = a.getColor(R.styleable.VerticalStepperView_step_error_highlight_color, mErrorColor);
6563

6664
if (a.hasValue(R.styleable.VerticalStepperView_step_done_icon)) {
6765
mDoneIcon = a.getDrawable(R.styleable.VerticalStepperView_step_done_icon);
@@ -280,6 +278,64 @@ public int getActivatedColor() {
280278
return mActivatedColor;
281279
}
282280

281+
/**
282+
* Set error highlight color
283+
*
284+
* @param color Error Highlight Color
285+
*/
286+
public void setErrorColor(@ColorInt int color) {
287+
mErrorColor = color;
288+
mAdapter.notifyDataSetChanged();
289+
}
290+
291+
/**
292+
* Set error highlight color
293+
*
294+
* @param colorRes Error Highlight Color resource
295+
*/
296+
public void setErrorColorResource(@ColorRes int colorRes) {
297+
setErrorColor(getResources().getColor(colorRes));
298+
}
299+
300+
/**
301+
* Get error highlight color
302+
*
303+
* @return Error Highlight Color
304+
*/
305+
@Override
306+
public int getErrorColor() {
307+
return mErrorColor;
308+
}
309+
310+
/**
311+
* Set line color
312+
*
313+
* @param color Line Color
314+
*/
315+
public void setLineColor(@ColorInt int color) {
316+
mLineColor = color;
317+
mAdapter.notifyDataSetChanged();
318+
}
319+
320+
/**
321+
* Set Line color
322+
*
323+
* @param colorRes Line Color resource
324+
*/
325+
public void setLineColorResource(@ColorRes int colorRes) {
326+
setLineColor(getResources().getColor(colorRes));
327+
}
328+
329+
/**
330+
* Get Line color
331+
*
332+
* @return Line Color
333+
*/
334+
@Override
335+
public int getLineColor() {
336+
return mLineColor;
337+
}
338+
283339
/**
284340
* Get animation duration
285341
*
@@ -356,6 +412,8 @@ public void onBindViewHolder(ItemHolder holder, int position) {
356412
holder.mItemView.setAnimationDuration(mAnimationDuration);
357413
holder.mItemView.setDoneIcon(mDoneIcon);
358414
holder.mItemView.setAnimationEnabled(mAnimationEnabled);
415+
holder.mItemView.setLineColor(mLineColor);
416+
holder.mItemView.setErrorColor(mErrorColor);
359417
holder.mItemView.setErrorText(mErrorTexts[position]);
360418
if (getCurrentStep() > position) {
361419
holder.mItemView.setState(VerticalStepperItemView.STATE_DONE);

library/src/main/res/layout/vertical_stepper_item_view_layout.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
android:layout_width="wrap_content"
9797
android:layout_height="wrap_content"
9898
android:textAppearance="@android:style/TextAppearance.Material.Body1"
99-
android:textColor="@color/material_grey_500"
10099
android:text="Summary"/>
101100

102101
<FrameLayout

library/src/main/res/values/attrs.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
<attr name="step_done_icon" format="reference"/>
77
<attr name="step_animation_duration" format="integer"/>
88
<attr name="step_enable_animation" format="boolean"/>
9+
<attr name="step_line_color" format="color"/>
10+
<attr name="step_error_highlight_color" format="color"/>
911

1012
<declare-styleable name="VerticalStepperView">
1113
<attr name="step_normal_color"/>
1214
<attr name="step_activated_color"/>
1315
<attr name="step_done_icon"/>
1416
<attr name="step_animation_duration"/>
1517
<attr name="step_enable_animation"/>
18+
<attr name="step_line_color"/>
19+
<attr name="step_error_highlight_color"/>
1620
</declare-styleable>
1721

1822
<declare-styleable name="VerticalStepperItemView">
@@ -30,6 +34,8 @@
3034
<attr name="step_done_icon"/>
3135
<attr name="step_animation_duration"/>
3236
<attr name="step_enable_animation"/>
37+
<attr name="step_line_color"/>
38+
<attr name="step_error_highlight_color"/>
3339
</declare-styleable>
3440

3541
</resources>

library/src/main/res/values/styles.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@
1313
<item name="android:textColor">?android:textColorSecondary</item>
1414
</style>
1515

16+
<style name="Widget.Stepper" parent="">
17+
<item name="step_normal_color">@color/material_grey_500</item>
18+
<item name="step_activated_color">?attr/colorPrimary</item>
19+
<item name="step_done_icon">@drawable/ic_done_white_16dp</item>
20+
<item name="step_animation_duration">@android:integer/config_shortAnimTime</item>
21+
<item name="step_line_color">@color/material_grey_500</item>
22+
<item name="step_error_highlight_color">@color/material_red_500</item>
23+
</style>
24+
1625
</resources>

0 commit comments

Comments
 (0)