Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Library has a configurable stroke witdth for the idle and the progress state #72

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 45 additions & 32 deletions library/src/main/java/com/dd/CircularProgressButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dd.circular.progress.button.R;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
Expand Down Expand Up @@ -38,17 +39,18 @@ public class CircularProgressButton extends Button {

private StateManager mStateManager;
private State mState;
private String mIdleText;
private String mCompleteText;
private String mErrorText;
private String mProgressText;
private CharSequence mIdleText;
private CharSequence mCompleteText;
private CharSequence mErrorText;
private CharSequence mProgressText;

private int mColorProgress;
private int mColorIndicator;
private int mColorIndicatorBackground;
private int mIconComplete;
private int mIconError;
private int mStrokeWidth;
private int mIdleStrokeWidth;
private int mProgressStrokeWidth;
private int mPaddingProgress;
private float mCornerRadius;
private boolean mIndeterminateProgressMode;
Expand All @@ -65,23 +67,27 @@ private enum State {

public CircularProgressButton(Context context) {
super(context);
init(context, null);
init(context, null, 0, 0);
}

public CircularProgressButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
init(context, attrs, 0, 0);
}

public CircularProgressButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
public CircularProgressButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}

private void init(Context context, AttributeSet attributeSet) {
mStrokeWidth = (int) getContext().getResources().getDimension(R.dimen.cpb_stroke_width);
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CircularProgressButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs, defStyleAttr, defStyleRes);
}

initAttributes(context, attributeSet);
private void init(Context context, AttributeSet attributeSet, int defStyleAttr, int defStyleRes) {
initAttributes(context, attributeSet, defStyleAttr, defStyleRes);

mMaxProgress = 100;
mState = State.IDLE;
Expand Down Expand Up @@ -155,7 +161,7 @@ private StrokeGradientDrawable createDrawable(int color) {
drawable.setCornerRadius(mCornerRadius);
StrokeGradientDrawable strokeGradientDrawable = new StrokeGradientDrawable(drawable);
strokeGradientDrawable.setStrokeColor(color);
strokeGradientDrawable.setStrokeWidth(mStrokeWidth);
strokeGradientDrawable.setStrokeWidth(mIdleStrokeWidth);

return strokeGradientDrawable;
}
Expand All @@ -178,23 +184,34 @@ protected void drawableStateChanged() {
}
}

private void initAttributes(Context context, AttributeSet attributeSet) {
TypedArray attr = getTypedArray(context, attributeSet, R.styleable.CircularProgressButton);
private void initAttributes(Context context, AttributeSet attributeSet, int defStyleAttr, int defStyleRes) {
TypedArray attr = context.obtainStyledAttributes(attributeSet, R.styleable.CircularProgressButton, defStyleAttr, defStyleRes);
if (attr == null) {
return;
}

try {

mIdleText = attr.getString(R.styleable.CircularProgressButton_cpb_textIdle);
mCompleteText = attr.getString(R.styleable.CircularProgressButton_cpb_textComplete);
mErrorText = attr.getString(R.styleable.CircularProgressButton_cpb_textError);
mProgressText = attr.getString(R.styleable.CircularProgressButton_cpb_textProgress);
final int defaultIdleStrokeWidth = getResources().getDimensionPixelSize(R.dimen.cpb_idle_stroke_width);
mIdleStrokeWidth = attr.getDimensionPixelSize(R.styleable.CircularProgressButton_cpb_idleStrokeWidth, defaultIdleStrokeWidth);

final int defaultProgressStrokeWidth = getResources().getDimensionPixelSize(R.dimen.cpb_progress_stroke_width);
mProgressStrokeWidth = attr.getDimensionPixelSize(R.styleable.CircularProgressButton_cpb_progressStrokeWidth, defaultProgressStrokeWidth);

mIdleText = attr.getText(R.styleable.CircularProgressButton_cpb_textIdle);
mCompleteText = attr.getText(R.styleable.CircularProgressButton_cpb_textComplete);
mErrorText = attr.getText(R.styleable.CircularProgressButton_cpb_textError);
mProgressText = attr.getText(R.styleable.CircularProgressButton_cpb_textProgress);

if (mIdleText == null) { // Use native text attribute if 'cpb_textIdle' is empty
mIdleText = getText();
}

mIconComplete = attr.getResourceId(R.styleable.CircularProgressButton_cpb_iconComplete, 0);
mIconError = attr.getResourceId(R.styleable.CircularProgressButton_cpb_iconError, 0);
mCornerRadius = attr.getDimension(R.styleable.CircularProgressButton_cpb_cornerRadius, 0);
mPaddingProgress = attr.getDimensionPixelSize(R.styleable.CircularProgressButton_cpb_paddingProgress, 0);
mIndeterminateProgressMode = attr.getBoolean(R.styleable.CircularProgressButton_cpb_progressIndeterminate, false);

int blue = getColor(R.color.cpb_blue);
int white = getColor(R.color.cpb_white);
Expand Down Expand Up @@ -225,10 +242,6 @@ protected int getColor(int id) {
return getResources().getColor(id);
}

protected TypedArray getTypedArray(Context context, AttributeSet attributeSet, int[] attr) {
return context.obtainStyledAttributes(attributeSet, attr, 0, 0);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Expand All @@ -245,7 +258,7 @@ protected void onDraw(Canvas canvas) {
private void drawIndeterminateProgress(Canvas canvas) {
if (mAnimatedDrawable == null) {
int offset = (getWidth() - getHeight()) / 2;
mAnimatedDrawable = new CircularAnimatedDrawable(mColorIndicator, mStrokeWidth);
mAnimatedDrawable = new CircularAnimatedDrawable(mColorIndicator, mProgressStrokeWidth);
int left = offset + mPaddingProgress;
int right = getWidth() - offset - mPaddingProgress;
int bottom = getHeight() - mPaddingProgress;
Expand All @@ -262,7 +275,7 @@ private void drawProgress(Canvas canvas) {
if (mProgressDrawable == null) {
int offset = (getWidth() - getHeight()) / 2;
int size = getHeight() - mPaddingProgress * 2;
mProgressDrawable = new CircularProgressDrawable(size, mStrokeWidth, mColorIndicator);
mProgressDrawable = new CircularProgressDrawable(size, mProgressStrokeWidth, mColorIndicator);
int left = offset + mPaddingProgress;
mProgressDrawable.setBounds(left, mPaddingProgress, left, mPaddingProgress);
}
Expand Down Expand Up @@ -587,27 +600,27 @@ public void setStrokeColor(int color) {
background.setStrokeColor(color);
}

public String getIdleText() {
public CharSequence getIdleText() {
return mIdleText;
}

public String getCompleteText() {
public CharSequence getCompleteText() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please help me. what's wrong with String ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CharSequence is more flexible.

E.g. https://developer.android.com/reference/android/text/Spannable implements CharSequence interface, so you can make text with different color, size, style, etc.

return mCompleteText;
}

public String getErrorText() {
public CharSequence getErrorText() {
return mErrorText;
}

public void setIdleText(String text) {
public void setIdleText(CharSequence text) {
mIdleText = text;
}

public void setCompleteText(String text) {
public void setCompleteText(CharSequence text) {
mCompleteText = text;
}

public void setErrorText(String text) {
public void setErrorText(CharSequence text) {
mErrorText = text;
}

Expand Down
2 changes: 1 addition & 1 deletion library/src/main/res/drawable/cpb_background.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="@dimen/cpb_stroke_width"
android:width="@dimen/cpb_idle_stroke_width"
android:color="@color/cpb_blue"/>
<solid android:color="@color/cpb_blue"/>
</shape>
3 changes: 2 additions & 1 deletion library/src/main/res/values/dimen.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="cpb_stroke_width">4dp</dimen>
<dimen name="cpb_idle_stroke_width">4dp</dimen>
<dimen name="cpb_progress_stroke_width">4dp</dimen>
</resources>
12 changes: 8 additions & 4 deletions library/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
<attr name="cpb_selectorIdle" format="reference"/>
<attr name="cpb_selectorComplete" format="reference"/>
<attr name="cpb_selectorError" format="reference"/>
<attr name="cpb_textComplete" format="string"/>
<attr name="cpb_textIdle" format="string"/>
<attr name="cpb_textError" format="string"/>
<attr name="cpb_textProgress" format="string"/>
<attr name="cpb_textComplete" format="string" localization="suggested"/>
<attr name="cpb_textIdle" format="string" localization="suggested"/>
<attr name="cpb_textError" format="string" localization="suggested"/>
<attr name="cpb_textProgress" format="string" localization="suggested"/>
<attr name="cpb_colorProgress" format="color"/>
<attr name="cpb_colorIndicator" format="color"/>
<attr name="cpb_colorIndicatorBackground" format="color"/>
<attr name="cpb_iconError" format="reference"/>
<attr name="cpb_iconComplete" format="reference"/>
<attr name="cpb_cornerRadius" format="dimension"/>
<attr name="cpb_paddingProgress" format="dimension"/>
<attr name="cpb_background" format="reference"/>
<attr name="cpb_idleStrokeWidth" format="dimension"/>
<attr name="cpb_progressStrokeWidth" format="dimension"/>
<attr name="cpb_progressIndeterminate" format="boolean"/>
</declare-styleable>

</resources>