Skip to content

Commit e95ddcb

Browse files
committed
added common methof
1 parent ac2fb53 commit e95ddcb

File tree

2 files changed

+37
-54
lines changed

2 files changed

+37
-54
lines changed

.idea/encodings.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.

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

Lines changed: 37 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import android.util.AttributeSet
55
import android.view.Gravity
66
import android.view.View
77
import android.view.ViewTreeObserver
8-
import android.view.animation.AccelerateDecelerateInterpolator
9-
import android.view.animation.Animation
10-
import android.view.animation.LinearInterpolator
11-
import android.view.animation.RotateAnimation
8+
import android.view.animation.*
129
import android.widget.LinearLayout
1310
import android.widget.RelativeLayout
1411
import com.agrawalsuneet.squareloaderspack.R
@@ -21,6 +18,9 @@ class UsainBoltLoader : LinearLayout, LoaderContract {
2118
var rectangleColor = resources.getColor(android.R.color.darker_gray)
2219
var rectangleWidth: Int = 60
2320

21+
var animDuration: Int = 500
22+
var interpolator: Interpolator = LinearInterpolator()
23+
2424
private var shouldAnimate: Boolean = true
2525

2626
private var calWidth: Int = 0
@@ -218,77 +218,64 @@ class UsainBoltLoader : LinearLayout, LoaderContract {
218218

219219
private fun startLoading() {
220220
//left leg animation
221-
val leftLegAnim = getLeftLegAnimation()
221+
val leftLegAnim = getAnimation(0)
222222
leftLegRectangleView?.startAnimation(leftLegAnim)
223223

224224
//right leg animation
225-
val rightLegAnim = getRightLegAnimation()
225+
val rightLegAnim = getAnimation(1)
226226
rightLegLShapeView?.startAnimation(rightLegAnim)
227227

228228
//left hand animation
229-
val leftHandAnim = getLeftHandAnimation()
229+
val leftHandAnim = getAnimation(2)
230230
leftHandLShapeView?.startAnimation(leftHandAnim)
231231

232232
//right hand animation
233-
val rightHandAnim = getRightHandAnimation()
233+
val rightHandAnim = getAnimation(3)
234234
rightHandLShapeView?.startAnimation(rightHandAnim)
235235

236236
}
237237

238-
private fun getLeftLegAnimation(): RotateAnimation {
239-
val pivotX = (0.5 * rectangleWidth).toFloat()
240-
val pivotY = (-rectangleWidth).toFloat()
241-
242-
val rotateAnimation = RotateAnimation(0f, -90f, pivotX, pivotY)
243-
244-
rotateAnimation.duration = 10000
245-
rotateAnimation.repeatCount = Animation.INFINITE
246-
rotateAnimation.repeatMode = Animation.REVERSE
247-
rotateAnimation.interpolator = LinearInterpolator()
248-
249-
return rotateAnimation
250-
}
251-
252-
private fun getRightLegAnimation(): RotateAnimation {
253-
val pivotX = (1.5 * rectangleWidth).toFloat()
254-
val pivotY = 0f
238+
private fun getAnimation(bodyPart: Int): RotateAnimation {
239+
val rotateAnimation: RotateAnimation = when (bodyPart) {
240+
0 -> {
241+
//left leg
242+
val pivotX = (0.5 * rectangleWidth).toFloat()
243+
val pivotY = (-rectangleWidth).toFloat()
255244

256-
val rotateAnimation = RotateAnimation(0f, 90f, pivotX, pivotY)
257-
258-
rotateAnimation.duration = 10000
259-
rotateAnimation.repeatCount = Animation.INFINITE
260-
rotateAnimation.repeatMode = Animation.REVERSE
261-
rotateAnimation.interpolator = LinearInterpolator()
245+
RotateAnimation(0f, -90f, pivotX, pivotY)
246+
}
262247

263-
return rotateAnimation
264-
}
248+
1 -> {
249+
//right leg
250+
val pivotX = (1.5 * rectangleWidth).toFloat()
251+
val pivotY = 0f
265252

266-
private fun getLeftHandAnimation(): RotateAnimation {
267-
val pivotX = (3 * rectangleWidth).toFloat()
268-
val pivotY = (0.5 * rectangleWidth).toFloat()
253+
RotateAnimation(0f, 90f, pivotX, pivotY)
254+
}
269255

270-
val rotateAnimation = RotateAnimation(0f, -160f, pivotX, pivotY)
256+
2 -> {
257+
//left hand
258+
val pivotX = (3 * rectangleWidth).toFloat()
259+
val pivotY = (0.5 * rectangleWidth).toFloat()
271260

272-
rotateAnimation.duration = 10000
273-
rotateAnimation.repeatCount = Animation.INFINITE
274-
rotateAnimation.repeatMode = Animation.REVERSE
275-
rotateAnimation.interpolator = LinearInterpolator()
261+
RotateAnimation(0f, -160f, pivotX, pivotY)
262+
}
276263

277-
return rotateAnimation
278-
}
264+
else -> {
265+
//right hand
266+
val pivotX = (0.5 * rectangleWidth).toFloat()
267+
val pivotY = (1.5 * rectangleWidth).toFloat()
279268

280-
private fun getRightHandAnimation(): RotateAnimation {
281-
val pivotX = (0.5 * rectangleWidth).toFloat()
282-
val pivotY = (1.5 * rectangleWidth).toFloat()
269+
RotateAnimation(0f, 170f, pivotX, pivotY)
270+
}
271+
}
283272

284-
val rotateAnimation = RotateAnimation(0f, 170f, pivotX, pivotY)
285273

286-
rotateAnimation.duration = 10000
274+
rotateAnimation.duration = animDuration.toLong()
287275
rotateAnimation.repeatCount = Animation.INFINITE
288276
rotateAnimation.repeatMode = Animation.REVERSE
289-
rotateAnimation.interpolator = LinearInterpolator()
277+
rotateAnimation.interpolator = interpolator
290278

291279
return rotateAnimation
292280
}
293-
294281
}

0 commit comments

Comments
 (0)