Created
January 30, 2018 19:41
-
-
Save genderquery/f029ab203d0ca1385505f8d94b6608e5 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.github.genderquery; | |
import android.graphics.Bitmap; | |
import android.graphics.Bitmap.Config; | |
import android.graphics.Canvas; | |
import android.graphics.ColorFilter; | |
import android.graphics.Paint; | |
import android.graphics.Paint.Style; | |
import android.graphics.PixelFormat; | |
import android.graphics.Rect; | |
import android.graphics.RectF; | |
import android.graphics.drawable.Drawable; | |
import android.support.annotation.ColorInt; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
public class DoughnutDrawable extends Drawable { | |
private static final float STROKE_RATIO = 12f; | |
private static final float PADDING_RATIO = 8f; | |
private static final int MAX_LEVEL = 10000; | |
private Paint primaryPaint; | |
private Paint secondaryPaint; | |
private RectF drawRect; | |
public DoughnutDrawable() { | |
primaryPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); | |
primaryPaint.setStyle(Style.STROKE); | |
primaryPaint.setColor(0xff000000); | |
secondaryPaint = new Paint(primaryPaint); | |
secondaryPaint.setColor(0x80000000); | |
} | |
@Override | |
public int getOpacity() { | |
return PixelFormat.TRANSLUCENT; | |
} | |
@ColorInt | |
public int getPrimaryColor() { | |
return primaryPaint.getColor(); | |
} | |
public void setPrimaryColor(@ColorInt int color) { | |
primaryPaint.setColor(color); | |
invalidateSelf(); | |
} | |
@ColorInt | |
public int getSecondaryColor() { | |
return secondaryPaint.getColor(); | |
} | |
public void setSecondaryColor(@ColorInt int color) { | |
secondaryPaint.setColor(color); | |
invalidateSelf(); | |
} | |
@Override | |
public void setAlpha(int alpha) { | |
primaryPaint.setAlpha(alpha); | |
secondaryPaint.setAlpha(alpha); | |
} | |
@Override | |
public void setColorFilter(@Nullable ColorFilter colorFilter) { | |
primaryPaint.setColorFilter(colorFilter); | |
secondaryPaint.setColorFilter(colorFilter); | |
} | |
@Override | |
public void draw(@NonNull Canvas canvas) { | |
int level = getLevel(); | |
float sweepAngle = (float) level / (float) MAX_LEVEL * 360f; | |
canvas.drawOval(drawRect, secondaryPaint); | |
canvas.drawArc(drawRect, -90, sweepAngle, false, primaryPaint); | |
} | |
public void setPercent(int percent) { | |
// 100 - 10000 | |
setLevel(percent * 100); | |
} | |
public Bitmap createBitmap(int width, int height) { | |
setBounds(0, 0, width, height); | |
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); | |
Canvas canvas = new Canvas(bitmap); | |
draw(canvas); | |
return bitmap; | |
} | |
@Override | |
protected void onBoundsChange(Rect bounds) { | |
drawRect = new RectF(bounds); | |
float width = drawRect.width(); | |
float height = drawRect.height(); | |
float size = Math.min(width, height); | |
float padding = size / PADDING_RATIO; | |
// center and pad | |
drawRect.inset((width - size) / 2f, (height - size) / 2f); | |
drawRect.inset(padding, padding); | |
float stroke = size / STROKE_RATIO; | |
primaryPaint.setStrokeWidth(stroke); | |
secondaryPaint.setStrokeWidth(stroke / 2f); | |
} | |
@Override | |
protected boolean onLevelChange(int level) { | |
invalidateSelf(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment