Skip to content

Commit afb1ef0

Browse files
committed
responsive
1 parent 20583c6 commit afb1ef0

File tree

4 files changed

+68
-118
lines changed

4 files changed

+68
-118
lines changed

app/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,13 @@
1010
android:id="@+id/pb1"
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
13-
android:layout_marginStart="60dp"
14-
android:layout_marginTop="60dp"
1513
app:innerAnimInterpolator="linear"
14+
app:innerLoaderStrokeWidth="4dp"
1615
app:layout_constraintStart_toStartOf="parent"
1716
app:layout_constraintTop_toTopOf="parent"
1817
app:outerAnimInterpolator="linear"
19-
app:outerLoaderAnimDuration="1500" />
20-
21-
<com.sn.lib.NestedProgress
22-
android:id="@+id/pb2"
23-
android:layout_width="wrap_content"
24-
android:layout_height="wrap_content"
25-
app:innerAnimInterpolator="linear"
26-
app:innerLoaderAnimDuration="500"
27-
app:innerLoaderColor="@color/purple_700"
28-
app:innerLoaderLength="180"
29-
app:layout_constraintStart_toEndOf="@+id/pb1"
30-
app:layout_constraintTop_toTopOf="@id/pb1"
31-
app:outerAnimInterpolator="linear"
32-
app:outerLoaderAnimDuration="2000"
33-
app:outerLoaderColor="@color/blue"
34-
app:outerLoaderLength="300"
35-
app:outerLoaderStrokeWidth="20" />
36-
37-
<com.sn.lib.NestedProgress
38-
android:id="@+id/pb3"
39-
android:layout_width="wrap_content"
40-
android:layout_height="wrap_content"
41-
app:innerAnimInterpolator="overshoot"
42-
app:innerLoaderAnimDuration="2000"
43-
app:innerLoaderColor="@color/red"
44-
app:innerLoaderLength="320"
45-
app:layout_constraintStart_toEndOf="@+id/pb2"
46-
app:layout_constraintTop_toTopOf="@id/pb2"
47-
app:outerAnimInterpolator="anticipateOvershoot"
48-
app:outerLoaderAnimDuration="1000"
49-
app:outerLoaderColor="@color/red_soft"
50-
app:outerLoaderLength="200"
51-
app:outerLoaderStrokeWidth="25" />
18+
app:outerLoaderAnimDuration="1500"
19+
app:outerLoaderStrokeWidth="5dp" />
5220

5321

5422
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ object Constants {
88

99
const val SIZE_FACTOR = 1.0F
1010
const val CIRCLE_RADIUS = 360
11-
const val INNER_LOADER_POS = 60
12-
const val OUTER_LOADER_POS = 80
1311
const val ANIM_DURATION = 1000
1412
const val INNER_ANIM_INTERPOLATOR = 6
1513
const val OUTER_ANIM_INTERPOLATOR = 5
1614
const val INNER_LOADER_LENGTH = 260F
1715
const val OUTER_LOADER_LENGTH = 320F
18-
const val INNER_STROKE_WIDTH = 12F
19-
const val OUTER_STROKE_WIDTH = 14F
16+
const val INNER_STROKE_WIDTH = 4F
17+
const val OUTER_STROKE_WIDTH = 5F
18+
const val MID_POINT = 2F
19+
const val POSITION_COEFFICIENT = 12F
2020
}

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

Lines changed: 59 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ import android.util.AttributeSet
99
import android.view.View
1010
import android.view.animation.*
1111
import androidx.annotation.ColorInt
12+
import androidx.annotation.Dimension
1213
import com.sn.lib.Constants.ANIM_DURATION
1314
import com.sn.lib.Constants.CIRCLE_RADIUS
1415
import com.sn.lib.Constants.COLOR_BLUE
1516
import com.sn.lib.Constants.COLOR_LIGHT_BLUE
1617
import com.sn.lib.Constants.INNER_ANIM_INTERPOLATOR
17-
import com.sn.lib.Constants.INNER_LOADER_POS
1818
import com.sn.lib.Constants.INNER_LOADER_LENGTH
1919
import com.sn.lib.Constants.INNER_STROKE_WIDTH
20+
import com.sn.lib.Constants.MID_POINT
2021
import com.sn.lib.Constants.OUTER_ANIM_INTERPOLATOR
21-
import com.sn.lib.Constants.OUTER_LOADER_POS
2222
import com.sn.lib.Constants.OUTER_LOADER_LENGTH
2323
import 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
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
<attr name="innerLoaderLength" format="float" />
88
<attr name="outerLoaderColor" format="color" />
99
<attr name="innerLoaderColor" format="color" />
10-
<attr name="innerLoaderStrokeWidth" format="float" />
11-
<attr name="outerLoaderStrokeWidth" format="float" />
10+
<attr name="innerLoaderStrokeWidth" format="dimension" />
11+
<attr name="outerLoaderStrokeWidth" format="dimension" />
1212
<attr name="sizeFactor" format="float" />
1313
<attr name="innerAnimInterpolator" format="enum">
1414
<enum name="accelerate" value="0" />

0 commit comments

Comments
 (0)