Guest User

Untitled

a guest
May 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.res.Resources;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9.  
  10. public class StatusCircleView extends View {
  11.  
  12. private int width;
  13. private int height;
  14.  
  15. private Paint mBackgroundPaint;
  16. private Paint mStrokePaint;
  17.  
  18. private float strokeWidth;
  19.  
  20. private int circleColor;
  21. private int strokeColor;
  22.  
  23. public StatusCircleView(Context context, AttributeSet attrs) {
  24. super(context, attrs);
  25. init(context, attrs);
  26. }
  27.  
  28. public StatusCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
  29. super(context, attrs, defStyleAttr);
  30. init(context, attrs);
  31. }
  32.  
  33. public static int dpToPx(int dp) {
  34. return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
  35. }
  36.  
  37. private void init(Context context, AttributeSet attrs) {
  38. TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.StatusCircle, 0, 0);
  39.  
  40. circleColor = mTypedArray.getColor(R.styleable.StatusCircle_circleColor, Color.RED);
  41. strokeColor = mTypedArray.getColor(R.styleable.StatusCircle_circleStrokeColor, Color.WHITE);
  42. strokeWidth = mTypedArray.getDimension(R.styleable.StatusCircle_circleStrokeWidth, 0);
  43.  
  44. mBackgroundPaint = new Paint();
  45. mBackgroundPaint.setColor(circleColor);
  46. mBackgroundPaint.setStyle(Paint.Style.FILL);
  47. mBackgroundPaint.setAntiAlias(true);
  48.  
  49. mStrokePaint = new Paint();
  50. mStrokePaint.setColor(strokeColor);
  51. mStrokePaint.setStyle(Paint.Style.STROKE);
  52. mStrokePaint.setStrokeWidth(strokeWidth);
  53. mStrokePaint.setAntiAlias(true);
  54.  
  55. mTypedArray.recycle();
  56. }
  57.  
  58. public void setCircleColor(int color) {
  59. circleColor = color;
  60. invalidate();
  61. }
  62.  
  63. @Override
  64. protected void onDraw(Canvas canvas) {
  65. super.onDraw(canvas);
  66.  
  67. int centre = width / 2;
  68. float radius = (centre - strokeWidth / 2);
  69.  
  70. //now we draw the circle
  71. canvas.drawCircle(centre, centre, radius, mBackgroundPaint);
  72.  
  73. //we draw the stroke
  74. canvas.drawCircle(centre, centre, radius, mStrokePaint);
  75. }
  76.  
  77. @Override
  78. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  79. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  80.  
  81. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  82. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  83. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  84. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  85.  
  86. if (widthMode == MeasureSpec.AT_MOST) {
  87. width = dpToPx(100);
  88. } else {
  89. width = widthSize;
  90. }
  91.  
  92. if (heightMode == MeasureSpec.AT_MOST) {
  93. height = dpToPx(100);
  94. } else {
  95. height = heightSize;
  96. }
  97.  
  98. setMeasuredDimension(width, height);
  99. }
  100.  
  101. @Override
  102. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  103. super.onSizeChanged(w, h, oldw, oldh);
  104. width = w;
  105. height = h;
  106. }
Add Comment
Please, Sign In to add comment