@@ -5,10 +5,7 @@ import android.util.AttributeSet
5
5
import android.view.Gravity
6
6
import android.view.View
7
7
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.*
12
9
import android.widget.LinearLayout
13
10
import android.widget.RelativeLayout
14
11
import com.agrawalsuneet.squareloaderspack.R
@@ -21,6 +18,9 @@ class UsainBoltLoader : LinearLayout, LoaderContract {
21
18
var rectangleColor = resources.getColor(android.R .color.darker_gray)
22
19
var rectangleWidth: Int = 60
23
20
21
+ var animDuration: Int = 500
22
+ var interpolator: Interpolator = LinearInterpolator ()
23
+
24
24
private var shouldAnimate: Boolean = true
25
25
26
26
private var calWidth: Int = 0
@@ -218,77 +218,64 @@ class UsainBoltLoader : LinearLayout, LoaderContract {
218
218
219
219
private fun startLoading () {
220
220
// left leg animation
221
- val leftLegAnim = getLeftLegAnimation( )
221
+ val leftLegAnim = getAnimation( 0 )
222
222
leftLegRectangleView?.startAnimation(leftLegAnim)
223
223
224
224
// right leg animation
225
- val rightLegAnim = getRightLegAnimation( )
225
+ val rightLegAnim = getAnimation( 1 )
226
226
rightLegLShapeView?.startAnimation(rightLegAnim)
227
227
228
228
// left hand animation
229
- val leftHandAnim = getLeftHandAnimation( )
229
+ val leftHandAnim = getAnimation( 2 )
230
230
leftHandLShapeView?.startAnimation(leftHandAnim)
231
231
232
232
// right hand animation
233
- val rightHandAnim = getRightHandAnimation( )
233
+ val rightHandAnim = getAnimation( 3 )
234
234
rightHandLShapeView?.startAnimation(rightHandAnim)
235
235
236
236
}
237
237
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()
255
244
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
+ }
262
247
263
- return rotateAnimation
264
- }
248
+ 1 -> {
249
+ // right leg
250
+ val pivotX = (1.5 * rectangleWidth).toFloat()
251
+ val pivotY = 0f
265
252
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
+ }
269
255
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()
271
260
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
+ }
276
263
277
- return rotateAnimation
278
- }
264
+ else -> {
265
+ // right hand
266
+ val pivotX = (0.5 * rectangleWidth).toFloat()
267
+ val pivotY = (1.5 * rectangleWidth).toFloat()
279
268
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
+ }
283
272
284
- val rotateAnimation = RotateAnimation (0f , 170f , pivotX, pivotY)
285
273
286
- rotateAnimation.duration = 10000
274
+ rotateAnimation.duration = animDuration.toLong()
287
275
rotateAnimation.repeatCount = Animation .INFINITE
288
276
rotateAnimation.repeatMode = Animation .REVERSE
289
- rotateAnimation.interpolator = LinearInterpolator ()
277
+ rotateAnimation.interpolator = interpolator
290
278
291
279
return rotateAnimation
292
280
}
293
-
294
281
}
0 commit comments