Skip to content

Instantly share code, notes, and snippets.

@cstengel88
Created February 4, 2016 09:52
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 cstengel88/327b8837e186c4fc75d0 to your computer and use it in GitHub Desktop.
Save cstengel88/327b8837e186c4fc75d0 to your computer and use it in GitHub Desktop.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by christianstengel on 16.07.15.
*/
public class ResultCircleView extends View {
private Paint solidPaint = new Paint();
private Paint ringPaint = new Paint();
private boolean isColorSet = false;
public ResultCircleView(Context context) {
super(context);
init();
}
private void init() {
ringPaint.setColor(Color.WHITE);
ringPaint.setStrokeWidth(2 * getResources().getDisplayMetrics().density);
ringPaint.setStyle(Paint.Style.STROKE);
ringPaint.setAntiAlias(true);
ringPaint.setStrokeCap(Paint.Cap.BUTT);
}
public ResultCircleView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ResultCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void setColor(int id) {
isColorSet = true;
solidPaint.setColor(id);
solidPaint.setStrokeWidth(6);
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float width = (float) getWidth();
float height = (float) getHeight();
float radius = height / 2 - ringPaint.getStrokeWidth() / 2;
float center_x = width / 2;
float center_y = height / 2;
final RectF rect = new RectF();
rect.set(center_x - radius,
center_y - radius,
center_x + radius,
center_y + radius);
canvas.drawArc(rect, 0, 360, false, ringPaint);
if (isColorSet) {
canvas.drawCircle(width / 2, height / 2, height / 2 - ringPaint.getStrokeWidth() * 3, solidPaint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment