Skip to content

Commit af919a3

Browse files
committed
added head
1 parent e95ddcb commit af919a3

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
}

squareloaderspack/src/main/java/com/agrawalsuneet/squareloaderspack/loaders/UsainBoltLoader.kt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import android.view.animation.*
99
import android.widget.LinearLayout
1010
import android.widget.RelativeLayout
1111
import com.agrawalsuneet.squareloaderspack.R
12+
import com.agrawalsuneet.squareloaderspack.basicviews.CircleView
1213
import com.agrawalsuneet.squareloaderspack.basicviews.LShapeView
1314
import com.agrawalsuneet.squareloaderspack.basicviews.LoaderContract
1415
import com.agrawalsuneet.squareloaderspack.basicviews.RectangleView
1516

1617
class UsainBoltLoader : LinearLayout, LoaderContract {
1718

1819
var rectangleColor = resources.getColor(android.R.color.darker_gray)
19-
var rectangleWidth: Int = 60
20+
var rectangleWidth: Int = 40
2021

2122
var animDuration: Int = 500
2223
var interpolator: Interpolator = LinearInterpolator()
@@ -37,6 +38,9 @@ class UsainBoltLoader : LinearLayout, LoaderContract {
3738
private var leftHandContainerLL: LinearLayout? = null
3839
private var rightHandContainerLL: LinearLayout? = null
3940

41+
private var headCircleView: CircleView? = null
42+
private var headContainer: LinearLayout? = null
43+
4044

4145
constructor(context: Context) : super(context) {
4246
initView()
@@ -186,7 +190,7 @@ class UsainBoltLoader : LinearLayout, LoaderContract {
186190
baseRectHeight = rectangleWidth,
187191
verticalRectWidth = rectangleWidth,
188192
verticalRectHeight = 2 * rectangleWidth,
189-
color = resources.getColor(android.R.color.holo_red_light))
193+
color = rectangleColor)
190194

191195
rightHandLShapeView?.rotation = -45f
192196

@@ -204,6 +208,26 @@ class UsainBoltLoader : LinearLayout, LoaderContract {
204208
relativeLayout?.addView(rightHandContainerLL, rightHandContainerParam)
205209
rightHandContainerLL?.addView(rightHandLShapeView)
206210

211+
//head circle view
212+
headCircleView = CircleView(context = context,
213+
circleRadius = (rectangleWidth / 1.5).toInt(),
214+
circleColor = rectangleColor)
215+
216+
217+
//head container
218+
headContainer = LinearLayout(context)
219+
headContainer?.clipChildren = false
220+
headContainer?.clipToPadding = false
221+
headContainer?.setPadding((2.75 * rectangleWidth).toInt(), (0.25 * rectangleWidth).toInt(), 0, 0)
222+
223+
val headContainerParam = RelativeLayout.LayoutParams(
224+
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
225+
226+
headContainerParam.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE)
227+
228+
relativeLayout?.addView(headContainer, headContainerParam)
229+
headContainer?.addView(headCircleView)
230+
207231
viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
208232
override fun onGlobalLayout() {
209233

squareloaderspack/src/main/res/values/attrs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212
<attr name="rectColor" format="color" />
1313
</declare-styleable>
1414

15+
<declare-styleable name="CircleView">
16+
<attr name="circleRadius" format="dimension" />
17+
<attr name="circleColor" format="color" />
18+
<attr name="circleDrawOnlystroke" format="boolean" />
19+
<attr name="circleStrokeWidth" format="dimension" />
20+
</declare-styleable>
21+
1522
<declare-styleable name="LShapeView">
1623
<attr name="baseRectWidth" format="dimension" />
1724
<attr name="baseRectHeight" format="dimension" />

0 commit comments

Comments
 (0)