Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
Add Comment
Please, Sign In to add comment