Skip to content

Instantly share code, notes, and snippets.

@nayan-dabhi
Forked from Liron-K/PieProgressDrawable.java
Created October 24, 2018 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nayan-dabhi/c0eee318de45146d71fc7f1a9a21fa82 to your computer and use it in GitHub Desktop.
Save nayan-dabhi/c0eee318de45146d71fc7f1a9a21fa82 to your computer and use it in GitHub Desktop.
package com.tinkeringinc.androidcommon;
/**
* Created by liron on 1/12/15.
* from https://gist.github.com/dcow/9493477
*/
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.widget.ScrollView;
/**
* 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;
Paint mBackgroundPaint;
Paint mBorderPaint;
RectF mBoundsF;
RectF mInnerBoundsF;
final float START_ANGLE = -90;
float mDrawTo;
public PieProgressDrawable() {
super();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint.setColor(Color.TRANSPARENT);
mBorderPaint = 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;
mBorderPaint.setStrokeWidth(borderWidth);
}
/**
* @param color you want the pie to be drawn in
*/
public void setBorderColor(int color) {
mBorderPaint.setColor(color);
}
/**
* @param color you want the pie to be drawn in
*/
public void setBackgroundColor(int color) {
mBackgroundPaint.setColor(color);
}
/**
* @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(0, getBounds().centerX(), getBounds().centerY());
mBackgroundPaint.setStyle(Paint.Style.FILL);
canvas.drawOval(mBoundsF, mBackgroundPaint);
mBorderPaint.setStyle(Paint.Style.STROKE);
canvas.drawOval(mBoundsF, mBorderPaint);
mPaint.setStyle(Paint.Style.FILL);
if(Math.abs(mDrawTo) > 0.0001)
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) (mBorderPaint.getStrokeWidth()/2f + 0.5f);
mInnerBoundsF.inset(halfBorder, halfBorder);
}
@Override
protected boolean onLevelChange(int level) {
final float drawTo = ((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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment