@@ -9,26 +9,24 @@ import android.util.AttributeSet
99import android.view.View
1010import android.view.animation.*
1111import androidx.annotation.ColorInt
12+ import androidx.annotation.Dimension
1213import com.sn.lib.Constants.ANIM_DURATION
1314import com.sn.lib.Constants.CIRCLE_RADIUS
1415import com.sn.lib.Constants.COLOR_BLUE
1516import com.sn.lib.Constants.COLOR_LIGHT_BLUE
1617import com.sn.lib.Constants.INNER_ANIM_INTERPOLATOR
17- import com.sn.lib.Constants.INNER_LOADER_POS
1818import com.sn.lib.Constants.INNER_LOADER_LENGTH
1919import com.sn.lib.Constants.INNER_STROKE_WIDTH
20+ import com.sn.lib.Constants.MID_POINT
2021import com.sn.lib.Constants.OUTER_ANIM_INTERPOLATOR
21- import com.sn.lib.Constants.OUTER_LOADER_POS
2222import com.sn.lib.Constants.OUTER_LOADER_LENGTH
2323import com.sn.lib.Constants.OUTER_STROKE_WIDTH
24- import com.sn.lib.Constants.SIZE_FACTOR
25- import java.lang.IllegalArgumentException
26- import kotlin.math.min
27- import kotlin.math.roundToInt
24+ import com.sn.lib.Constants.POSITION_COEFFICIENT
25+ import com.sn.lib.ext.dp
2826
2927/* *
3028 * @author Aydin Emre E.
31- * @version 1.0.1
29+ * @version 1.0.2
3230 * @since 19-02-2022
3331 */
3432
@@ -37,6 +35,10 @@ class NestedProgress @JvmOverloads constructor(
3735 context : Context , attrs : AttributeSet ? = null , defStyleAttr : Int = 0
3836) : View(context, attrs, defStyleAttr) {
3937
38+ /* *
39+ * Default values are defined here. If a new one is not assigned, these values are used.
40+ * @see [Constants]
41+ */
4042
4143 // Animation value of progressions. These values are assigned to the "sweep angle" value in drawArc
4244 private var innerLoaderAnimValue: Int = 0
@@ -54,41 +56,42 @@ class NestedProgress @JvmOverloads constructor(
5456 this .isAntiAlias = true
5557 }
5658
57-
59+ // Animation times for both progresses
5860 var innerLoaderAnimDuration: Int = ANIM_DURATION
5961 var outerLoaderAnimDuration: Int = ANIM_DURATION
6062
63+ // Animation types for both progresses
6164 var innerAnimInterpolator = INNER_ANIM_INTERPOLATOR
6265 var outerAnimInterpolator = OUTER_ANIM_INTERPOLATOR
6366
64- /* * There is no limit value for stroke width, but correct values should be used for a smooth display * */
65- var innerLoaderStrokeWidth: Float = INNER_STROKE_WIDTH
66- var outerLoaderStrokeWidth: Float = OUTER_STROKE_WIDTH
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
6777
6878 @ColorInt
6979 var innerLoaderColor: Int = COLOR_LIGHT_BLUE
7080
7181 @ColorInt
7282 var outerLoaderColor: Int = COLOR_BLUE
7383
74- /* * The maximum angle at which you can see a movement in the animation is 359
75- * -innerLoaderLength
76- * -outerLoaderLength
77- * * * /
84+ /* * You must value between the drawing angles of the circle (1-359), with value of 0 and 360, the animation is not visible.
85+ * - innerLoaderLength
86+ * - outerLoaderLength
87+ */
7888
7989 var innerLoaderLength: Float = INNER_LOADER_LENGTH
8090 var outerLoaderLength: Float = OUTER_LOADER_LENGTH
8191
82- /* * The library ignores the dp value so you should keep the sizeFactor value range 1 and 3.
83- * In case you exceed value you will get IllegalArgumentException
84- * @throws IllegalArgumentException
85- * */
86- var sizeFactor: Float = SIZE_FACTOR
87- set(value) {
88- field =
89- if (value > 3.0f && value < 1.0f ) throw IllegalArgumentException (" sizeFactor Must be range 1.0 and 3.0" ) else value
90- }
91-
92+ /* *
93+ * You can find the attributes from the attrs.xml file.
94+ */
9295 init {
9396 val attributes = context.obtainStyledAttributes(attrs, R .styleable.NestedProgress )
9497
@@ -121,13 +124,13 @@ class NestedProgress @JvmOverloads constructor(
121124 )
122125
123126 innerLoaderStrokeWidth =
124- attributes.getFloat (
127+ attributes.getDimension (
125128 R .styleable.NestedProgress_innerLoaderStrokeWidth ,
126129 this .innerLoaderStrokeWidth
127130 )
128131
129132 outerLoaderStrokeWidth =
130- attributes.getFloat (
133+ attributes.getDimension (
131134 R .styleable.NestedProgress_outerLoaderStrokeWidth ,
132135 this .outerLoaderStrokeWidth
133136 )
@@ -144,8 +147,6 @@ class NestedProgress @JvmOverloads constructor(
144147 this .outerAnimInterpolator
145148 )
146149
147- sizeFactor = attributes.getFloat(R .styleable.NestedProgress_sizeFactor , this .sizeFactor)
148-
149150 attributes.recycle()
150151
151152 innerLoaderAnimation()
@@ -156,21 +157,23 @@ class NestedProgress @JvmOverloads constructor(
156157 override fun onDraw (canvas : Canvas ) {
157158 super .onDraw(canvas)
158159
159- val centerW: Float = width / 2f
160- val centerH: Float = height / 2f
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
161164
162165 outerLoadingRect.set(
163- centerW - (OUTER_LOADER_POS * sizeFactor) ,
164- centerH - (OUTER_LOADER_POS * sizeFactor) ,
165- centerW + (OUTER_LOADER_POS * sizeFactor) ,
166- centerH + (OUTER_LOADER_POS * sizeFactor)
166+ centerW - (position / MID_POINT ) + distanceCircles ,
167+ centerH - (position / MID_POINT ) + distanceCircles ,
168+ centerW + (position / MID_POINT ) + distanceCircles ,
169+ centerH + (position / MID_POINT ) + distanceCircles,
167170 )
168171
169172 innerLoadingRect.set(
170- centerW - ( INNER_LOADER_POS * sizeFactor) ,
171- centerH - ( INNER_LOADER_POS * sizeFactor) ,
172- centerW + ( INNER_LOADER_POS * sizeFactor) ,
173- centerH + ( INNER_LOADER_POS * sizeFactor)
173+ centerW - position / MID_POINT ,
174+ centerH - position / MID_POINT ,
175+ centerW + position / MID_POINT ,
176+ centerH + position / MID_POINT ,
174177 )
175178
176179 updatePaint(outerLoaderColor, outerLoaderStrokeWidth)
@@ -193,48 +196,13 @@ class NestedProgress @JvmOverloads constructor(
193196
194197 }
195198
196- override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
197- super .onMeasure(widthMeasureSpec, heightMeasureSpec)
198- val desiredWidth = (250 * sizeFactor).roundToInt()
199- val desiredHeight = (250 * sizeFactor).roundToInt()
200-
201- val widthMode = MeasureSpec .getMode(widthMeasureSpec)
202- val widthSize = MeasureSpec .getSize(widthMeasureSpec)
203- val heightMode = MeasureSpec .getMode(heightMeasureSpec)
204- val heightSize = MeasureSpec .getSize(heightMeasureSpec)
205-
206- val width = when (widthMode) {
207- MeasureSpec .EXACTLY -> {
208- widthSize
209- }
210- MeasureSpec .AT_MOST -> {
211- min(desiredWidth, widthSize)
212- }
213- else -> {
214- desiredWidth
215- }
216- }
217-
218- val height = when (heightMode) {
219- MeasureSpec .EXACTLY -> {
220- heightSize
221- }
222- MeasureSpec .AT_MOST -> {
223- min(desiredHeight, heightSize)
224- }
225- else -> {
226- desiredHeight
227- }
228- }
229-
230- setMeasuredDimension(width, height)
231- }
232-
199+ // Starts the animation when the screen is attached
233200 override fun onAttachedToWindow () {
234201 super .onAttachedToWindow()
235202 startAnimation()
236203 }
237204
205+ // Stops the animation when the screen is detached
238206 override fun onDetachedFromWindow () {
239207 super .onDetachedFromWindow()
240208 stopAnimation()
@@ -250,11 +218,21 @@ class NestedProgress @JvmOverloads constructor(
250218 outerLoaderAnimator.end()
251219 }
252220
221+ /* *
222+ * Both progresses use the same paint object, which is updated with this function before being drawn.
223+ * @param color [Int]
224+ * @param strokeWidth [Float]
225+ */
253226 private fun updatePaint (color : Int , strokeWidth : Float ) {
254227 paint.color = color
255228 paint.strokeWidth = strokeWidth
256229 }
257230
231+ /* *
232+ * Returns an interpolator back based on the integer value
233+ * sorting is the same as enum in attrs.xml.
234+ * @param interpolator [Int]
235+ */
258236 private fun getAnimation (interpolator : Int ): Interpolator {
259237 return when (interpolator) {
260238 0 -> AccelerateInterpolator ()
@@ -268,6 +246,11 @@ class NestedProgress @JvmOverloads constructor(
268246 }
269247 }
270248
249+ /* *
250+ * Animation continues until progress is removed. Inherits its properties from defined variables.
251+ * - innerLoaderAnimation
252+ * - outerLoaderAnimation
253+ */
271254 private fun innerLoaderAnimation () {
272255 innerLoaderAnimator.interpolator = getAnimation(innerAnimInterpolator)
273256 innerLoaderAnimator.duration = innerLoaderAnimDuration.toLong()
@@ -288,5 +271,4 @@ class NestedProgress @JvmOverloads constructor(
288271 }
289272 }
290273
291-
292274}
0 commit comments