Last active
May 18, 2018 23:18
-
-
Save DevPicon/eccc2427d3b6b842835bad6d8c546666 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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="StatusCircle"> | |
<attr name="circleColor" format="color" /> | |
<attr name="circleStrokeColor" format="color" /> | |
<attr name="circleStrokeWidth" format="dimension" /> | |
</declare-styleable> | |
</resources> |
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
import android.content.Context; | |
import android.content.res.Resources; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.util.AttributeSet; | |
import android.view.View; | |
public class StatusCircleView extends View { | |
private int width; | |
private int height; | |
private Paint mBackgroundPaint; | |
private Paint mStrokePaint; | |
private float strokeWidth; | |
private int circleColor; | |
private int strokeColor; | |
public StatusCircleView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs); | |
} | |
public StatusCircleView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs); | |
} | |
public static int dpToPx(int dp) { | |
return (int) (dp * Resources.getSystem().getDisplayMetrics().density); | |
} | |
private void init(Context context, AttributeSet attrs) { | |
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.StatusCircle, 0, 0); | |
circleColor = mTypedArray.getColor(R.styleable.StatusCircle_circleColor, Color.RED); | |
strokeColor = mTypedArray.getColor(R.styleable.StatusCircle_circleStrokeColor, Color.WHITE); | |
strokeWidth = mTypedArray.getDimension(R.styleable.StatusCircle_circleStrokeWidth, 0); | |
mBackgroundPaint = new Paint(); | |
mBackgroundPaint.setColor(circleColor); | |
mBackgroundPaint.setStyle(Paint.Style.FILL); | |
mBackgroundPaint.setAntiAlias(true); | |
mStrokePaint = new Paint(); | |
mStrokePaint.setColor(strokeColor); | |
mStrokePaint.setStyle(Paint.Style.STROKE); | |
mStrokePaint.setStrokeWidth(strokeWidth); | |
mStrokePaint.setAntiAlias(true); | |
mTypedArray.recycle(); | |
} | |
public void setCircleColor(int color) { | |
circleColor = color; | |
invalidate(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
int centre = width / 2; | |
float radius = (centre - strokeWidth / 2); | |
//now we draw the circle | |
canvas.drawCircle(centre, centre, radius, mBackgroundPaint); | |
//we draw the stroke | |
canvas.drawCircle(centre, centre, radius, mStrokePaint); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
int widthMode = MeasureSpec.getMode(widthMeasureSpec); | |
int widthSize = MeasureSpec.getSize(widthMeasureSpec); | |
int heightMode = MeasureSpec.getMode(heightMeasureSpec); | |
int heightSize = MeasureSpec.getSize(heightMeasureSpec); | |
if (widthMode == MeasureSpec.AT_MOST) { | |
width = dpToPx(100); | |
} else { | |
width = widthSize; | |
} | |
if (heightMode == MeasureSpec.AT_MOST) { | |
height = dpToPx(100); | |
} else { | |
height = heightSize; | |
} | |
setMeasuredDimension(width, height); | |
} | |
@Override | |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { | |
super.onSizeChanged(w, h, oldw, oldh); | |
width = w; | |
height = h; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment