Skip to content

Instantly share code, notes, and snippets.

@vaibhav-jani
Forked from dcow/PieProgressDrawable.java
Last active December 28, 2016 11:06
Show Gist options
  • Save vaibhav-jani/aa4e0fae1ddac714dc103b2d4f091c5a to your computer and use it in GitHub Desktop.
Save vaibhav-jani/aa4e0fae1ddac714dc103b2d4f091c5a to your computer and use it in GitHub Desktop.
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
/**
PieProgressDrawable progressDrawable = new PieProgressDrawable();
progressDrawable.setColor(subjectColor);
progressDrawable.setBorderWidth(progressStrokeWidth);
progressDrawable.setColor(subjectColor);
int progress = 10 * (10 - position);
if(progress == 100) {
holder.ivProgress.setImageResource(R.drawable.white_tick);
} else {
holder.ivProgress.setImageResource(android.R.color.transparent);
}
progressDrawable.setLevel(progress);
imageViewProgress.setBackground(progressDrawable);
imageViewProgress.invalidate();
* A PieProgressDrawable does this:
* <a href="http://stackoverflow.com/questions/12458476/how-to-create-circular-progress-barpie-chart-like-indicator-android">Circular Progress Bar Android</a>
*/
public class PieProgressDrawable extends Drawable {
private Paint mPaint;
private Paint mPaintBg;
private RectF mBoundsF;
private RectF mInnerBoundsF;
private final float START_ANGLE = 0.0f;
private float mDrawTo;
public PieProgressDrawable() {
super();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaintBg = new Paint(Paint.ANTI_ALIAS_FLAG);
}
/**
* Set the border width.
*
* @param widthDp in dip for the pie border
*/
public void setBorderWidth(float widthDp, DisplayMetrics dm) {
float borderWidth = widthDp * dm.density;
mPaint.setStrokeWidth(borderWidth);
}
/**
* Set the border width.
*
* @param widthPx in pix for the pie border
*/
public void setBorderWidth(float widthPx) {
mPaint.setStrokeWidth(widthPx);
}
/**
* @param color you want the pie to be drawn in
*/
public void setColor(int color) {
mPaint.setColor(color);
int colorBg = blendColors(color, Color.WHITE, 0.02f);
mPaintBg.setColor(colorBg);
}
@Override
public void draw(Canvas canvas) {
// Rotate the canvas around the center of the pie by 90 degrees
// counter clockwise so the pie stars at 12 o'clock.
canvas.rotate(-90f, getBounds().centerX(), getBounds().centerY());
canvas.drawArc(mInnerBoundsF, START_ANGLE, 360, true, mPaintBg);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawOval(mBoundsF, mPaint);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(mInnerBoundsF, START_ANGLE, mDrawTo, true, mPaint);
// Draw inner oval and text on top of the pie (or add any other
// decorations such as a stroke) here..
// Don't forget to rotate the canvas back if you plan to add text!
// ...
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mBoundsF = mInnerBoundsF = new RectF(bounds);
final int halfBorder = (int) (mPaint.getStrokeWidth() / 2f + 0.5f);
mInnerBoundsF.inset(halfBorder, halfBorder);
}
@Override
protected boolean onLevelChange(int level) {
final float drawTo = START_ANGLE + ((float) 360 * level) / 100f;
boolean update = drawTo != mDrawTo;
mDrawTo = drawTo;
return update;
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
public void setProgress(int level) {
onLevelChange(level);
}
@Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
}
private int blendColors(int color1, int color2, float ratio) {
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
@SuppressWarnings("WrongConstant")
@Override
public int getOpacity() {
return mPaint.getAlpha();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment