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

Added Circular pb Gradient option #90

Open
wants to merge 4 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
##Description
Fork of castorflex smooth progress bar lib with the addition of a GRADIENT option that I have created for the the indeterminate circular progress bar. This is something I have seen people ask about on stack overflow but I could not find a solution posted anywhere online.

**VIDEO OF GRADIENT CIRCULAR PROGRESS BAR: https://youtu.be/z6bYy9BLe8o

Commit: https://github.com/mochi-logic/SmoothProgressBar/commit/44241884cc5b97120bb9308a3e00ae0d85c2f8a0

Feel free to message me with any questions.

Castorflex original description:

Small library allowing you to make a smooth indeterminate progress bar. You can either user your progress bars and set this drawable or use directly the `SmoothProgressBarView`.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public CircularProgressBar(Context context, AttributeSet attrs, int defStyle) {
final int colorsId = a.getResourceId(R.styleable.CircularProgressBar_cpb_colors, 0);
final int minSweepAngle = a.getInteger(R.styleable.CircularProgressBar_cpb_min_sweep_angle, res.getInteger(R.integer.cpb_default_min_sweep_angle));
final int maxSweepAngle = a.getInteger(R.styleable.CircularProgressBar_cpb_max_sweep_angle, res.getInteger(R.integer.cpb_default_max_sweep_angle));
final boolean useGradient = a.getBoolean(R.styleable.CircularProgressBar_cpb_use_gradient, false);
a.recycle();

int[] colors = null;
Expand All @@ -50,7 +51,8 @@ public CircularProgressBar(Context context, AttributeSet attrs, int defStyle) {
.rotationSpeed(rotationSpeed)
.strokeWidth(strokeWidth)
.minSweepAngle(minSweepAngle)
.maxSweepAngle(maxSweepAngle);
.maxSweepAngle(maxSweepAngle)
.useGradient(useGradient);

if (colors != null && colors.length > 0)
builder.colors(colors);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public static class Builder {
private int mMaxSweepAngle;
@CircularProgressDrawable.Style int mStyle;
private PowerManager mPowerManager;
private boolean mUseGradient = false;

public Builder(@NonNull Context context) {
this(context, false);
Expand Down Expand Up @@ -261,6 +262,12 @@ public Builder angleInterpolator(Interpolator interpolator) {
return this;
}

public Builder useGradient(boolean useGradient)
{
mUseGradient = useGradient;
return this;
}

public CircularProgressDrawable build() {
return new CircularProgressDrawable(
mPowerManager,
Expand All @@ -272,7 +279,8 @@ public CircularProgressDrawable build() {
mRotationSpeed,
mMinSweepAngle,
mMaxSweepAngle,
mStyle));
mStyle,
mUseGradient));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.SweepGradient;
import android.support.annotation.NonNull;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
Expand Down Expand Up @@ -41,6 +43,7 @@ class DefaultDelegate implements PBDelegate {
private float mRotationSpeed;
private int mMinSweepAngle;
private int mMaxSweepAngle;
private boolean mUseGradient;

private CircularProgressDrawable mParent;
private CircularProgressDrawable.OnEndListener mOnEndListener;
Expand All @@ -57,6 +60,7 @@ public DefaultDelegate(@NonNull CircularProgressDrawable parent,
mRotationSpeed = options.rotationSpeed;
mMinSweepAngle = options.minSweepAngle;
mMaxSweepAngle = options.maxSweepAngle;
mUseGradient = options.useGradient;

setupAnimations();
}
Expand All @@ -80,7 +84,26 @@ public void draw(Canvas canvas, Paint paint) {
startAngle = (startAngle + (sweepAngle - newSweepAngle)) % 360;
sweepAngle = newSweepAngle;
}
canvas.drawArc(mParent.getDrawableBounds(), startAngle, sweepAngle, false, paint);
if(mUseGradient)
{
canvas.rotate(startAngle, mParent.getDrawableBounds().centerX(),
mParent.getDrawableBounds().centerY());
int leadingColor;
if(mColors.length > 1)
leadingColor = mColors[1];
else
leadingColor = Color.BLACK;
int[] gradientColors = {mColors[0], leadingColor};
float[] positions = {0, sweepAngle / 360f};
SweepGradient gradient = new SweepGradient(mParent.getDrawableBounds().centerX(),
mParent.getDrawableBounds().centerY(), gradientColors, positions);
paint.setShader(gradient);
paint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawArc(mParent.getDrawableBounds(), 6, sweepAngle, false, paint);
}
else {
canvas.drawArc(mParent.getDrawableBounds(), startAngle, sweepAngle, false, paint);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Options {
final float rotationSpeed;
final int minSweepAngle;
final int maxSweepAngle;
final boolean useGradient;
@CircularProgressDrawable.Style final int style;

public Options(Interpolator angleInterpolator,
Expand All @@ -23,7 +24,8 @@ public Options(Interpolator angleInterpolator,
float rotationSpeed,
int minSweepAngle,
int maxSweepAngle,
@CircularProgressDrawable.Style int style) {
@CircularProgressDrawable.Style int style,
boolean useGradient) {
this.angleInterpolator = angleInterpolator;
this.sweepInterpolator = sweepInterpolator;
this.borderWidth = borderWidth;
Expand All @@ -33,6 +35,7 @@ public Options(Interpolator angleInterpolator,
this.minSweepAngle = minSweepAngle;
this.maxSweepAngle = maxSweepAngle;
this.style = style;
this.useGradient = useGradient;
}


Expand Down
1 change: 1 addition & 0 deletions library-circular/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<attr name="cpb_max_sweep_angle" format="integer"/>
<attr name="cpb_sweep_speed" format="float"/>
<attr name="cpb_rotation_speed" format="float"/>
<attr name="cpb_use_gradient" format="boolean"/>
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions library-circular/src/main/res/values/defaults.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<integer name="cpb_default_max_sweep_angle">300</integer>
<item name="cpb_default_sweep_speed" format="float" type="string">1</item>
<item name="cpb_default_rotation_speed" format="float" type="string">1</item>
<item name="cpb_default_use_gradient" format="boolean" type="string">false</item>
</resources>
1 change: 1 addition & 0 deletions library-circular/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<item name="cpb_max_sweep_angle">@integer/cpb_default_max_sweep_angle</item>
<item name="cpb_sweep_speed">@string/cpb_default_sweep_speed</item>
<item name="cpb_rotation_speed">@string/cpb_default_rotation_speed</item>
<item name="cpb_use_gradient">@string/cpb_default_use_gradient</item>
</style>

</resources>
12 changes: 12 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@
android:layout_gravity="center_horizontal"
app:cpb_colors="@array/gplus_colors"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Circular Gradient"/>

<fr.castorflex.android.circularprogressbar.CircularProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:cpb_colors="@array/gplus_colors"
app:cpb_use_gradient="true"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down