@@ -9,6 +9,7 @@ import android.util.AttributeSet
99import android.view.View
1010import android.view.animation.LinearInterpolator
1111import android.view.animation.OvershootInterpolator
12+ import androidx.annotation.ColorInt
1213import com.sn.lib.Constants.ANIM_DURATION
1314import com.sn.lib.Constants.CIRCLE_RADIUS
1415import com.sn.lib.Constants.COLOR_BLUE
@@ -21,41 +22,87 @@ import com.sn.lib.Constants.OUTER_ANIM_INTERPOLATOR
2122import com.sn.lib.Constants.OUTER_LOADER_POS
2223import com.sn.lib.Constants.OUTER_LOADER_LENGTH
2324import com.sn.lib.Constants.OUTER_STROKE_WIDTH
25+ import com.sn.lib.Constants.SIZE_FACTOR
26+ import java.lang.IllegalArgumentException
2427import kotlin.math.min
28+ import kotlin.math.roundToInt
29+
30+ /* *
31+ * @author Aydin Emre E.
32+ * @version 1.0.1
33+ * @since 19-02-2022
34+ */
2535
2636class NestedProgress @JvmOverloads constructor(
2737 context : Context , attrs : AttributeSet ? = null , defStyleAttr : Int = 0
2838) : View(context, attrs, defStyleAttr) {
2939
3040
31- private var innerLoaderAnimDuration: Int = ANIM_DURATION
32- private var outerLoaderAnimDuration: Int = ANIM_DURATION
33-
41+ // Animation value of progressions. These values are assigned to the "sweep angle" value in drawArc
3442 private var innerLoaderAnimValue: Int = 0
3543 private var outerLoaderAnimValue: Int = 0
3644
37- private var innerAnimInterpolator = INNER_ANIM_INTERPOLATOR
38- private var outerAnimInterpolator = OUTER_ANIM_INTERPOLATOR
39-
45+ // Animation always starts at 1 and goes full round until 360
4046 private val innerLoaderAnimator = ValueAnimator .ofInt(1 , CIRCLE_RADIUS )
4147 private val outerLoaderAnimator = ValueAnimator .ofInt(1 , CIRCLE_RADIUS )
4248
43- private var innerLoaderLength: Float = INNER_LOADER_LENGTH
44- private var outerLoaderLength: Float = OUTER_LOADER_LENGTH
45-
46- private var innerLoaderStrokeWidth: Float = INNER_STROKE_WIDTH
47- private var outerLoaderStrokeWidth: Float = OUTER_STROKE_WIDTH
48-
49- private var innerLoaderColor: Int = COLOR_BLUE
50- private var outerLoaderColor: Int = COLOR_LIGHT_BLUE
49+ private val innerLoadingRect: RectF = RectF ()
50+ private val outerLoadingRect: RectF = RectF ()
5151
5252 private val paint = Paint ().apply {
5353 this .style = Paint .Style .STROKE
5454 this .isAntiAlias = true
5555 }
5656
57- private val innerLoadingRect: RectF = RectF ()
58- private val outerLoadingRect: RectF = RectF ()
57+
58+ @Suppress(" UNUSED_VARIABLE" )
59+ private var innerLoaderAnimDuration: Int = ANIM_DURATION
60+
61+ @Suppress(" UNUSED_VARIABLE" )
62+ private var outerLoaderAnimDuration: Int = ANIM_DURATION
63+
64+ @Suppress(" UNUSED_VARIABLE" )
65+ var innerAnimInterpolator = INNER_ANIM_INTERPOLATOR
66+
67+ @Suppress(" UNUSED_VARIABLE" )
68+ var outerAnimInterpolator = OUTER_ANIM_INTERPOLATOR
69+
70+ /* * There is no limit value for stroke width, but correct values should be used for a smooth display * */
71+ @Suppress(" UNUSED_VARIABLE" )
72+ var innerLoaderStrokeWidth: Float = INNER_STROKE_WIDTH
73+
74+ @Suppress(" UNUSED_VARIABLE" )
75+ var outerLoaderStrokeWidth: Float = OUTER_STROKE_WIDTH
76+
77+ @ColorInt
78+ @Suppress(" UNUSED_VARIABLE" )
79+ var innerLoaderColor: Int = COLOR_BLUE
80+
81+ @ColorInt
82+ @Suppress(" UNUSED_VARIABLE" )
83+ var outerLoaderColor: Int = COLOR_LIGHT_BLUE
84+
85+ /* * The maximum angle at which you can see a movement in the animation is 359
86+ * -innerLoaderLength
87+ * -outerLoaderLength
88+ * **/
89+
90+ @Suppress(" UNUSED_VARIABLE" )
91+ var innerLoaderLength: Float = INNER_LOADER_LENGTH
92+
93+ @Suppress(" UNUSED_VARIABLE" )
94+ var outerLoaderLength: Float = OUTER_LOADER_LENGTH
95+
96+ /* * The library ignores the dp value so you should keep the sizeFactor value range 1 and 3.
97+ * In case you exceed value you will get IllegalArgumentException
98+ * @throws IllegalArgumentException
99+ * */
100+ @Suppress(" UNUSED_VARIABLE" )
101+ var sizeFactor: Float = SIZE_FACTOR
102+ set(value) {
103+ field =
104+ if (value > 3.0f && value < 1.0f ) throw IllegalArgumentException (" sizeFactor Must be range 1.0 and 3.0" ) else value
105+ }
59106
60107 init {
61108 val attributes = context.obtainStyledAttributes(attrs, R .styleable.NestedProgress )
@@ -112,6 +159,7 @@ class NestedProgress @JvmOverloads constructor(
112159 this .outerAnimInterpolator
113160 )
114161
162+ sizeFactor = attributes.getFloat(R .styleable.NestedProgress_sizeFactor , this .sizeFactor)
115163
116164 attributes.recycle()
117165
@@ -127,16 +175,17 @@ class NestedProgress @JvmOverloads constructor(
127175 val centerH: Float = height / 2f
128176
129177 innerLoadingRect.set(
130- centerW - INNER_LOADER_POS ,
131- centerH - INNER_LOADER_POS ,
132- centerW + INNER_LOADER_POS ,
133- centerH + INNER_LOADER_POS
178+ centerW - ( INNER_LOADER_POS * sizeFactor) ,
179+ centerH - ( INNER_LOADER_POS * sizeFactor) ,
180+ centerW + ( INNER_LOADER_POS * sizeFactor) ,
181+ centerH + ( INNER_LOADER_POS * sizeFactor)
134182 )
183+
135184 outerLoadingRect.set(
136- centerW - OUTER_LOADER_POS ,
137- centerH - OUTER_LOADER_POS ,
138- centerW + OUTER_LOADER_POS ,
139- centerH + OUTER_LOADER_POS
185+ centerW - ( OUTER_LOADER_POS * sizeFactor) ,
186+ centerH - ( OUTER_LOADER_POS * sizeFactor) ,
187+ centerW + ( OUTER_LOADER_POS * sizeFactor) ,
188+ centerH + ( OUTER_LOADER_POS * sizeFactor)
140189 )
141190
142191 updatePaint(outerLoaderColor, outerLoaderStrokeWidth)
@@ -161,8 +210,8 @@ class NestedProgress @JvmOverloads constructor(
161210
162211 override fun onMeasure (widthMeasureSpec : Int , heightMeasureSpec : Int ) {
163212 super .onMeasure(widthMeasureSpec, heightMeasureSpec)
164- val desiredWidth = 300
165- val desiredHeight = 300
213+ val desiredWidth = ( 300 * sizeFactor).roundToInt()
214+ val desiredHeight = ( 300 * sizeFactor).roundToInt()
166215
167216 val widthMode = MeasureSpec .getMode(widthMeasureSpec)
168217 val widthSize = MeasureSpec .getSize(widthMeasureSpec)
0 commit comments