Skip to content

Instantly share code, notes, and snippets.

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 vincetreur/be30cea297ea78dd820c to your computer and use it in GitHub Desktop.
Save vincetreur/be30cea297ea78dd820c to your computer and use it in GitHub Desktop.
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
public class TimerDrawable extends Drawable {
private static final int MINUTES_PER_HOUR = 60;
private static final int DEGREES_PER_MINUTE = 6;
private static final int START_ANGLE = -90;
public static final int DEFAULT_STROKE_WIDTH= -1;
private static final float DEFAULT_STROKE_DIVIDER = 0.08f;
public static final int FULL_CIRCLE = 360;
@NonNull private final Paint mPaint;
private int mSweep = 20 * DEGREES_PER_MINUTE;
private float mStrokeWidth = DEFAULT_STROKE_WIDTH;
private float mSize = 200f;
private boolean mSkipArc = false;
private float mCenter = mSize / 2;
private float mRadius = mSize / 2;
private RectF mBounds = new RectF(0, 0, mSize, mSize);
public TimerDrawable() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(DEFAULT_STROKE_WIDTH);
setBounds(0, 0, (int) mSize, (int) mSize);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
resize();
}
private void resize() {
Rect bounds = getBounds();
mSize = Math.min(bounds.right - bounds.left, bounds.bottom - bounds.top);
mCenter = mSize / 2;
float padding = resolveAppliedStrokeWith() / 2;
mRadius = (mSize / 2) - padding;
mBounds = new RectF(padding, padding, mSize - padding, mSize - padding);
}
private float resolveAppliedStrokeWith() {
float appliedStrokeWidth;
if (mStrokeWidth == DEFAULT_STROKE_WIDTH) {
appliedStrokeWidth = mSize * DEFAULT_STROKE_DIVIDER;
} else {
appliedStrokeWidth = mStrokeWidth;
}
mPaint.setStrokeWidth(appliedStrokeWidth);
return appliedStrokeWidth;
}
@Override
public void draw(Canvas canvas) {
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(mCenter, mCenter, mRadius, mPaint);
if (!mSkipArc) {
mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(mBounds, START_ANGLE, mSweep, true, mPaint);
}
}
@Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
// Not needed
}
@Override
public int getOpacity() {
// Not needed
return 0;
}
public void setMinutes(int minutes) {
mSweep = (minutes % MINUTES_PER_HOUR) * DEGREES_PER_MINUTE;
mSkipArc = minutes == 0;
if (!mSkipArc && mSweep == 0) {
mSweep = FULL_CIRCLE;
}
invalidateSelf();
}
public void setStrokeWidth(int width) {
mStrokeWidth = width;
resize();
invalidateSelf();
}
public void setTint(@ColorInt int color) {
mPaint.setColor(color);
invalidateSelf();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment