Skip to content

Instantly share code, notes, and snippets.

@dcow
Last active June 25, 2020 08:53
Show Gist options
  • Save dcow/9493477 to your computer and use it in GitHub Desktop.
Save dcow/9493477 to your computer and use it in GitHub Desktop.
package com.example.android.view;
import android.graphics.Canvas;
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;
/**
* 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 {
Paint mPaint;
RectF mBoundsF;
RectF mInnerBoundsF;
final float START_ANGLE = 0.f;
float mDrawTo;
public PieProgressDrawable() {
super();
mPaint = 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);
}
/**
* @param color you want the pie to be drawn in
*/
public void setColor(int color) {
mPaint.setColor(color);
}
@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());
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);
}
@Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return mPaint.getAlpha();
}
}
@hemantsonu20
Copy link

How to use this drawable as a progress bar drawable.
I am trying like this
pieProgressDrawable = new PieProgressDrawable();
circleProgressBar.setProgressDrawable(pieProgressDrawable);

And also how to increase the progress.

my progress bar xml is

@dcow
Copy link
Author

dcow commented Apr 18, 2014

I've never tried using this in a ProgressView (funny that I'd never thought to try that). I've always used it in an ImageView. Does it work in an ImageView for you? If so, we know the problem is with the ProgressView. It may be that you need a certain scale type or something..

@zeekaran
Copy link

zeekaran commented May 1, 2014

Maybe I just don't understand how custom drawables work, but how do I get the pie to update? The progress level sets correctly upon creation, but that's as far as I get.

EDIT: Ah, invalidate() makes it update. Nevermind! I'm just a newbie.

This is very useful and I thank you for it.

@omid-nazifi
Copy link

Please write an example. I can't draw a pie drawable like an image where you have shown in stackoverflow.

@pmohansivam
Copy link

Hi, can you please write a example project for using this pie chart..?? That would be really helpfull..

@ine-rp
Copy link

ine-rp commented Oct 5, 2016

Very useful class! Thank you. For those who need an example, I had to implement the Pie Progress to show a count down timer's progress. I wrote a small tutorial showing how to implement this class: https://irpdevelop.wordpress.com/2016/10/05/how-to-implement-pie-progress-view-in-android/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment