Last active
October 5, 2017 08:07
-
-
Save Frank1234/6d2b2721cec1b65892d62e05a3b5923a to your computer and use it in GitHub Desktop.
PieGraph, showing progress on a circle chart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<org.mycompany.myapp.ui.common.PieGraph | |
android:layout_width="24dp" | |
android:layout_height="24dp" | |
app:pieBackgroundColor="#ffffff" | |
app:pieProgressColor="#000000" | |
app:pieProgressPercent="25"/> | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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