Skip to content

Commit c67b824

Browse files
committed
1.0.2 done.
1 parent afb1ef0 commit c67b824

File tree

4 files changed

+109
-31
lines changed

4 files changed

+109
-31
lines changed

lib/src/main/java/com/sn/lib/Constants.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@ object Constants {
66
val COLOR_BLUE = Color.parseColor("#2666CF")
77
val COLOR_LIGHT_BLUE = Color.parseColor("#D3DEDC")
88

9-
const val SIZE_FACTOR = 1.0F
109
const val CIRCLE_RADIUS = 360
1110
const val ANIM_DURATION = 1000
1211
const val INNER_ANIM_INTERPOLATOR = 6
1312
const val OUTER_ANIM_INTERPOLATOR = 5
1413
const val INNER_LOADER_LENGTH = 260F
1514
const val OUTER_LOADER_LENGTH = 320F
1615
const val INNER_STROKE_WIDTH = 4F
17-
const val OUTER_STROKE_WIDTH = 5F
16+
const val OUTER_STROKE_WIDTH = 4.5F
1817
const val MID_POINT = 2F
19-
const val POSITION_COEFFICIENT = 12F
18+
const val START_POINT = 0F
19+
const val DESIRED_WH = 70F
20+
const val MAX_TOTAL_STROKE = 21F
21+
const val MIN_STOKE = 0F
22+
const val MAX_STROKE = 10F
23+
const val SPACE_BETWEEN_CIRCLES = 3F
2024
}

lib/src/main/java/com/sn/lib/NestedProgress.kt

Lines changed: 97 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,22 @@ import com.sn.lib.Constants.ANIM_DURATION
1414
import com.sn.lib.Constants.CIRCLE_RADIUS
1515
import com.sn.lib.Constants.COLOR_BLUE
1616
import com.sn.lib.Constants.COLOR_LIGHT_BLUE
17+
import com.sn.lib.Constants.DESIRED_WH
1718
import com.sn.lib.Constants.INNER_ANIM_INTERPOLATOR
1819
import com.sn.lib.Constants.INNER_LOADER_LENGTH
1920
import com.sn.lib.Constants.INNER_STROKE_WIDTH
21+
import com.sn.lib.Constants.MAX_STROKE
22+
import com.sn.lib.Constants.MAX_TOTAL_STROKE
2023
import com.sn.lib.Constants.MID_POINT
24+
import com.sn.lib.Constants.MIN_STOKE
2125
import com.sn.lib.Constants.OUTER_ANIM_INTERPOLATOR
2226
import com.sn.lib.Constants.OUTER_LOADER_LENGTH
2327
import com.sn.lib.Constants.OUTER_STROKE_WIDTH
24-
import com.sn.lib.Constants.POSITION_COEFFICIENT
28+
import com.sn.lib.Constants.SPACE_BETWEEN_CIRCLES
29+
import com.sn.lib.Constants.START_POINT
2530
import com.sn.lib.ext.dp
31+
import kotlin.math.min
32+
import kotlin.math.roundToInt
2633

2734
/**
2835
* @author Aydin Emre E.
@@ -64,17 +71,6 @@ class NestedProgress @JvmOverloads constructor(
6471
var innerAnimInterpolator = INNER_ANIM_INTERPOLATOR
6572
var outerAnimInterpolator = OUTER_ANIM_INTERPOLATOR
6673

67-
/** There is no limit value for stroke width, but correct values should be used for a smooth display
68-
* - innerLoaderStrokeWidth
69-
* - outerLoaderStrokeWidth
70-
* @see [dp] An extensions written for float. It is used to convert the default value to dp.
71-
*/
72-
@Dimension
73-
var innerLoaderStrokeWidth: Float = INNER_STROKE_WIDTH.dp
74-
75-
@Dimension
76-
var outerLoaderStrokeWidth: Float = OUTER_STROKE_WIDTH.dp
77-
7874
@ColorInt
7975
var innerLoaderColor: Int = COLOR_LIGHT_BLUE
8076

@@ -85,10 +81,48 @@ class NestedProgress @JvmOverloads constructor(
8581
* - innerLoaderLength
8682
* - outerLoaderLength
8783
*/
88-
8984
var innerLoaderLength: Float = INNER_LOADER_LENGTH
9085
var outerLoaderLength: Float = OUTER_LOADER_LENGTH
9186

87+
/** Stroke can be in the range 0 to 10, otherwise the illegal argument exception error is thrown.
88+
* @throws IllegalArgumentException
89+
* - innerLoaderStrokeWidth
90+
* - outerLoaderStrokeWidth
91+
* @see [dp] An extensions written for float. It is used to convert the default value to dp.
92+
*/
93+
@Dimension
94+
var innerLoaderStrokeWidth: Float = INNER_STROKE_WIDTH.dp
95+
set(value) {
96+
field =
97+
if (value > MAX_STROKE.dp || value < MIN_STOKE.dp) throw IllegalArgumentException(
98+
resources.getString(
99+
R.string.stroke_range_error
100+
)
101+
) else value
102+
}
103+
104+
@Dimension
105+
var outerLoaderStrokeWidth: Float = OUTER_STROKE_WIDTH.dp
106+
set(value) {
107+
field =
108+
if (value > MAX_STROKE.dp || value < MIN_STOKE.dp) throw IllegalArgumentException(
109+
resources.getString(
110+
R.string.stroke_range_error
111+
)
112+
) else value
113+
}
114+
115+
@Dimension
116+
var spaceBetweenCircles = SPACE_BETWEEN_CIRCLES.dp
117+
set(value) {
118+
field =
119+
if (value > MAX_STROKE.dp || value < MIN_STOKE.dp) throw IllegalArgumentException(
120+
resources.getString(
121+
R.string.stroke_range_error
122+
)
123+
) else value
124+
}
125+
92126
/**
93127
* You can find the attributes from the attrs.xml file.
94128
*/
@@ -147,35 +181,71 @@ class NestedProgress @JvmOverloads constructor(
147181
this.outerAnimInterpolator
148182
)
149183

184+
spaceBetweenCircles =
185+
attributes.getDimension(
186+
R.styleable.NestedProgress_spaceBetweenCircles,
187+
this.spaceBetweenCircles
188+
)
189+
150190
attributes.recycle()
151191

152192
innerLoaderAnimation()
153193
outerLoaderAnimation()
154194
}
155195

196+
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
197+
val widthMode = MeasureSpec.getMode(widthMeasureSpec)
198+
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
199+
val heightMode = MeasureSpec.getMode(heightMeasureSpec)
200+
val heightSize = MeasureSpec.getSize(heightMeasureSpec)
201+
val desired = DESIRED_WH.dp.roundToInt()
202+
203+
val width = when (widthMode) {
204+
MeasureSpec.EXACTLY -> {
205+
widthSize
206+
}
207+
MeasureSpec.AT_MOST -> {
208+
min(desired, widthSize)
209+
}
210+
else -> {
211+
desired
212+
}
213+
}
156214

157-
override fun onDraw(canvas: Canvas) {
158-
super.onDraw(canvas)
215+
val height = when (heightMode) {
216+
MeasureSpec.EXACTLY -> {
217+
heightSize
218+
}
219+
MeasureSpec.AT_MOST -> {
220+
min(desired, heightSize)
221+
}
222+
else -> {
223+
desired
224+
}
225+
}
159226

160-
val centerW: Float = width / MID_POINT
161-
val centerH: Float = height / MID_POINT
162-
val position: Float = width / POSITION_COEFFICIENT
163-
val distanceCircles: Float = (innerLoaderStrokeWidth + outerLoaderStrokeWidth) / MID_POINT
227+
setMeasuredDimension(width, height)
228+
229+
val highStroke = outerLoaderStrokeWidth + innerLoaderStrokeWidth
230+
val expansion = MAX_TOTAL_STROKE.dp - highStroke
164231

165232
outerLoadingRect.set(
166-
centerW - (position / MID_POINT) + distanceCircles,
167-
centerH - (position / MID_POINT) + distanceCircles,
168-
centerW + (position / MID_POINT) + distanceCircles,
169-
centerH + (position / MID_POINT) + distanceCircles,
233+
START_POINT + expansion + (highStroke / MID_POINT),
234+
START_POINT + expansion + (highStroke / MID_POINT),
235+
width - (expansion + (highStroke / MID_POINT)),
236+
width - (expansion + (highStroke / MID_POINT))
170237
)
171238

172239
innerLoadingRect.set(
173-
centerW - position / MID_POINT,
174-
centerH - position / MID_POINT,
175-
centerW + position / MID_POINT,
176-
centerH + position / MID_POINT,
240+
START_POINT + (expansion + highStroke + spaceBetweenCircles),
241+
START_POINT + (expansion + highStroke + spaceBetweenCircles),
242+
width - (expansion + highStroke + spaceBetweenCircles),
243+
width - (expansion + highStroke + spaceBetweenCircles)
177244
)
245+
}
178246

247+
override fun onDraw(canvas: Canvas) {
248+
super.onDraw(canvas)
179249
updatePaint(outerLoaderColor, outerLoaderStrokeWidth)
180250
canvas.drawArc(
181251
outerLoadingRect,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<attr name="innerLoaderColor" format="color" />
1010
<attr name="innerLoaderStrokeWidth" format="dimension" />
1111
<attr name="outerLoaderStrokeWidth" format="dimension" />
12-
<attr name="sizeFactor" format="float" />
12+
<attr name="spaceBetweenCircles" format="dimension" />
1313
<attr name="innerAnimInterpolator" format="enum">
1414
<enum name="accelerate" value="0" />
1515
<enum name="decelerate" value="1" />

lib/src/main/res/values/string.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<string name="stroke_range_error">Stroke can take values range 1 and 10</string>
4+
</resources>

0 commit comments

Comments
 (0)