Skip to content

Instantly share code, notes, and snippets.

@Frank1234
Last active October 5, 2017 08:07
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 Frank1234/6d2b2721cec1b65892d62e05a3b5923a to your computer and use it in GitHub Desktop.
Save Frank1234/6d2b2721cec1b65892d62e05a3b5923a to your computer and use it in GitHub Desktop.
PieGraph, showing progress on a circle chart.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PieGraph">
<attr name="pieProgressPercent" format="integer"/>
<attr name="pieBackgroundColor" format="color"/>
<attr name="pieProgressColor" format="color"/>
</declare-styleable>
</resources>
...
<org.mycompany.myapp.ui.common.PieGraph
android:layout_width="24dp"
android:layout_height="24dp"
app:pieBackgroundColor="#ffffff"
app:pieProgressColor="#000000"
app:pieProgressPercent="25"/>
...
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
* Shows progress on a circle chart.
*/
public class PieGraph extends View {
private Paint backgroundPaint = new Paint(), progressPaint = new Paint();
private RectF drawRectF;
private int progressRadius;
public PieGraph(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
parseValuesFromAttributes(context, attrs);
}
/**
* Sets the progress percentage of this pie graph. Same as using 'pieProgressPercent' in xml.
*/
public void setProgressPercent(int progressPercent) {
this.progressRadius = (int) ((360f / 100f) * progressPercent);
invalidate();
}
private void initPaint() {
backgroundPaint.setAntiAlias(true);
backgroundPaint.setDither(true);
backgroundPaint.setStyle(Paint.Style.FILL);
progressPaint.setAntiAlias(true);
progressPaint.setDither(true);
progressPaint.setStyle(Paint.Style.FILL);
}
private void parseValuesFromAttributes(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PieGraph);
try {
backgroundPaint.setColor(a.getColor(R.styleable.PieGraph_pieBackgroundColor, 0));
progressPaint.setColor(a.getColor(R.styleable.PieGraph_pieProgressColor, 0));
setProgressPercent(a.getInt(R.styleable.PieGraph_pieProgressPercent, 0));
} finally {
a.recycle();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
drawRectF = new RectF(0, 0, h, h);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawArc(drawRectF, -90, progressRadius, true, progressPaint);
canvas.drawArc(drawRectF, -90 + progressRadius, 360 - progressRadius, true, backgroundPaint);
}
}
/**
* Binding adapters for PieGraph. Only needed when you use data binding.
*/
public class PieGraphBindingAdapters {
@BindingAdapter("pieProgressPercent")
public static void setPieProgressPercent(PieGraph view, int pieProgressPercent) {
view.setProgressPercent(pieProgressPercent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment