1
+ package com.agrawalsuneet.squareloaderspack.basicviews
2
+
3
+ import android.content.Context
4
+ import android.graphics.Canvas
5
+ import android.graphics.Paint
6
+ import android.util.AttributeSet
7
+ import android.view.View
8
+ import com.agrawalsuneet.squareloaderspack.R
9
+
10
+ class CircleView : View {
11
+
12
+ var circleRadius: Int = 30
13
+ var strokeWidth: Int = 0
14
+
15
+ var circleColor: Int = 0
16
+ var drawOnlyStroke: Boolean = false
17
+
18
+ private var xyCordinates: Float = 0.0f
19
+
20
+ private val paint: Paint = Paint ()
21
+
22
+ constructor (context: Context , circleRadius: Int , circleColor: Int ) : super (context) {
23
+ this .circleRadius = circleRadius
24
+ this .circleColor = circleColor
25
+
26
+ initValues()
27
+ }
28
+
29
+ constructor (context: Context , circleRadius: Int , circleColor: Int , drawOnlyStroke: Boolean , strokeWidth: Int ) : super (context) {
30
+ this .circleRadius = circleRadius
31
+ this .circleColor = circleColor
32
+
33
+ this .drawOnlyStroke = drawOnlyStroke
34
+ this .strokeWidth = strokeWidth
35
+
36
+ initValues()
37
+ }
38
+
39
+ constructor (context: Context ) : super (context) {
40
+ initValues()
41
+ }
42
+
43
+ constructor (context: Context , attrs: AttributeSet ) : super (context, attrs) {
44
+ initAttributes(attrs)
45
+ initValues()
46
+ }
47
+
48
+ constructor (context: Context , attrs: AttributeSet , defStyleAttr: Int ) : super (context, attrs, defStyleAttr) {
49
+ initAttributes(attrs)
50
+ initValues()
51
+ }
52
+
53
+
54
+ fun initAttributes (attrs : AttributeSet ) {
55
+
56
+ val typedArray = context.obtainStyledAttributes(attrs, R .styleable.CircleView , 0 , 0 )
57
+
58
+ this .circleRadius = typedArray.getDimensionPixelSize(R .styleable.CircleView_circleRadius , 30 )
59
+ this .circleColor = typedArray.getColor(R .styleable.CircleView_circleColor , 0 )
60
+
61
+ this .drawOnlyStroke = typedArray.getBoolean(R .styleable.CircleView_circleDrawOnlystroke , false )
62
+
63
+ if (drawOnlyStroke) {
64
+ this .strokeWidth = typedArray.getDimensionPixelSize(R .styleable.CircleView_circleStrokeWidth , 0 )
65
+ }
66
+
67
+ typedArray.recycle()
68
+ }
69
+
70
+ override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
71
+ super .onMeasure(widthMeasureSpec, heightMeasureSpec)
72
+
73
+ val widthHeight = (2 * (circleRadius)) + strokeWidth
74
+
75
+ setMeasuredDimension(widthHeight, widthHeight)
76
+ }
77
+
78
+ private fun initValues () {
79
+ paint.isAntiAlias = true
80
+
81
+ if (drawOnlyStroke) {
82
+ paint.style = Paint .Style .STROKE
83
+ paint.strokeWidth = strokeWidth.toFloat()
84
+ } else {
85
+ paint.style = Paint .Style .FILL
86
+ }
87
+ paint.color = circleColor
88
+
89
+ // adding half of strokeWidth because
90
+ // the stroke will be half inside the drawing circle and half outside
91
+ xyCordinates = (circleRadius + (strokeWidth / 2 )).toFloat()
92
+ }
93
+
94
+
95
+ override fun onDraw (canvas : Canvas ) {
96
+ super .onDraw(canvas)
97
+ canvas.drawCircle(xyCordinates, xyCordinates, circleRadius.toFloat(), paint)
98
+ }
99
+
100
+
101
+ }
0 commit comments