-
-
Save ketzalv/536f32454b45b054d5d147625e6270bb to your computer and use it in GitHub Desktop.
This file contains 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
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment