11package com.devzone.fillprogresslayout
22
3+ import android.animation.TimeInterpolator
34import android.animation.ValueAnimator
45import android.content.Context
56import android.content.res.TypedArray
@@ -24,6 +25,7 @@ class FillProgressLayout : LinearLayout {
2425 const val RIGHT_TO_LEFT = 1
2526 const val TOP_TO_BOTTOM = 2
2627 const val BOTTOM_TO_TOP = 3
28+
2729 // specific for gradient direction
2830 const val TOP_LEFT_TO_BOTTOM_RIGHT = 4
2931 const val TOP_RIGHT_TO_BOTTOM_LEFT = 5
@@ -42,6 +44,7 @@ class FillProgressLayout : LinearLayout {
4244 private val defIsRestart = false
4345 private val defIsRounded = false
4446 private val defGradientMovement = false
47+ private val defAnimInterpolator = AccelerateDecelerateInterpolator ()
4548
4649 // customisable values
4750 private var isRounded = defIsRounded
@@ -56,6 +59,8 @@ class FillProgressLayout : LinearLayout {
5659 private var mGradientDirection = defDirection
5760 private var mGradientColors = intArrayOf()
5861
62+ private var animInterpolator: TimeInterpolator = defAnimInterpolator
63+
5964 private var oldProgress = 0
6065 private var currentProgress = 0
6166
@@ -136,7 +141,10 @@ class FillProgressLayout : LinearLayout {
136141 setGradientDirection(gradDirection)
137142
138143 val gradMovement =
139- array.getBoolean(R .styleable.FillProgressLayout_fpl_gradientMovement , defGradientMovement)
144+ array.getBoolean(
145+ R .styleable.FillProgressLayout_fpl_gradientMovement ,
146+ defGradientMovement
147+ )
140148 setGradientMovement(gradMovement)
141149
142150 try {
@@ -253,21 +261,29 @@ class FillProgressLayout : LinearLayout {
253261
254262// ---------------------public setters--------------------------------------------------------------------//
255263
256- fun setProgress (inputProgress : Int ,animated : Boolean =true) {
264+ /* *
265+ * This method is used to set the current progress value externally
266+ * with or without animation
267+ * @param inputProgress current progress value
268+ * @param animated should animate filling
269+ */
270+ fun setProgress (inputProgress : Int , animated : Boolean = true) {
257271 if (inputProgress in 0 .. maxProgress) {
258272 clearAnimation()
259- if (animated) {
273+ if (animated) {
260274 val animator = ValueAnimator .ofInt(oldProgress, inputProgress)
261- animator.interpolator = AccelerateDecelerateInterpolator ()
275+ animator.interpolator = animInterpolator
262276 animator.addUpdateListener { anm ->
263277 currentProgress = anm.animatedValue as Int
264278 updateRect(rectF = progressRectF)
265279 ViewCompat .postInvalidateOnAnimation(this )
266280 }
267- animator.doOnEnd { doOnProgressEnd?.invoke(this ); if (! isRestart) oldProgress = inputProgress }
281+ animator.doOnEnd {
282+ doOnProgressEnd?.invoke(this ); if (! isRestart) oldProgress = inputProgress
283+ }
268284 animator.setDuration(((kotlin.math.abs(inputProgress - oldProgress)) * mDurationFactor).toLong())
269285 .start()
270- }else {
286+ } else {
271287 currentProgress = inputProgress
272288 updateRect(rectF = progressRectF)
273289 doOnProgressEnd?.invoke(this )
@@ -276,20 +292,46 @@ class FillProgressLayout : LinearLayout {
276292 }
277293 }
278294
295+ /* *
296+ * This method is used to set progress animation interpolator
297+ * for different filling effects
298+ * @see TimeInterpolator
299+ * @param interpolator interpolator for animation
300+ * default value is
301+ * @see AccelerateDecelerateInterpolator
302+ */
303+ fun setAnimationInterpolator (interpolator : TimeInterpolator ) {
304+ animInterpolator = interpolator
305+ }
306+
307+ /* *
308+ * This method is used to set the background color
309+ * @param resId color resource id
310+ */
279311 fun setProgressBackgroundColor (@ColorRes resId : Int ) {
280312 if (isValidRes(resId)) {
281313 mBackgroundColor = ContextCompat .getColor(context, resId)
282314 initPaint()
283315 }
284316 }
285317
318+ /* *
319+ * This method is used to set the foreground/progress color
320+ * @param resId color resource id
321+ */
286322 fun setProgressColor (@ColorRes resId : Int ) {
287323 if (isValidRes(resId)) {
288324 mProgressColor = ContextCompat .getColor(context, resId)
289325 initPaint()
290326 }
291327 }
292328
329+ /* *
330+ * This method is used to set multiple colors for gradient effect
331+ * @param resIds array of color resource ids
332+ * @param extractResColor flag for color extraction
333+ * @see ContextCompat.getColor
334+ */
293335 fun setProgressColors (@ColorRes resIds : IntArray , extractResColor : Boolean = true) {
294336 try {
295337 val filtered = resIds.filter { isValidRes(it) }
@@ -304,41 +346,82 @@ class FillProgressLayout : LinearLayout {
304346 }
305347 }
306348
307-
349+ /* *
350+ * This method is used to make progress layout corners rounded
351+ * and roundness is adjusted with a float value
352+ * @param radius corner radius for progress layout
353+ */
308354 fun setCornerRadius (radius : Float ) {
309355 if (radius in 0f .. maxProgress.toFloat()) {
310356 setRoundedCorners(true )
311357 this .mCornerRadius = radius
312358 }
313359 }
314360
315-
361+ /* *
362+ * This method is used to make progress layout corners rounded
363+ * @param isRounded flag for rounded corners support
364+ */
316365 fun setRoundedCorners (isRounded : Boolean ) {
317366 this .isRounded = isRounded
318367 }
319368
369+ /* *
370+ * This method is used to adjust the movement of progress gradient
371+ * if true the gradient will move will progress and
372+ * if false the gradient will stay as background paint and will reveals with progress
373+ * @param gradMovement flag for gradient movement
374+ */
320375 fun setGradientMovement (gradMovement : Boolean ) {
321376 this .gradientMovement = gradMovement
322377 }
323378
379+ /* *
380+ * @param duration should be in millis i.e 2000 for 2 seconds
381+ */
324382 fun setDuration (duration : Long ) {
325383 if (duration.toInt() == 0 || duration < 0 ) return
326384 mDurationFactor = (duration / 100 ).toInt()
327385 }
328386
387+ /* *
388+ * This method is used to make progress restart or resume from old position
389+ * if true the gradient will restart from zero to progress value
390+ * if false the gradient will resume from old to new progress value
391+ * @param isRestart flag for gradient movement
392+ */
329393 fun shouldStartFromZero (isRestart : Boolean ) {
330394 this .isRestart = isRestart
331395 }
332396
397+ /* *
398+ * This method is used to set the direction of fill
399+ * from values LEFT_TO_RIGHT to BOTTOM_TO_TOP
400+ * default value is #LEFT_TO_RIGHT
401+ * @param direction value for fill direction
402+ * @see LEFT_TO_RIGHT
403+ */
333404 fun setFillDirection (direction : Int ) {
334405 mDirection = if (direction in LEFT_TO_RIGHT .. BOTTOM_TO_TOP ) direction else defDirection
335406 }
336407
408+ /* *
409+ * This method is used to set the direction of gradient fill
410+ * from values LEFT_TO_RIGHT to BOTTOM_LEFT_TO_TOP_RIGHT
411+ * default value is #LEFT_TO_RIGHT
412+ * @param direction value for fill direction
413+ * @see LEFT_TO_RIGHT
414+ */
337415 fun setGradientDirection (direction : Int ) {
338416 mGradientDirection =
339417 if (direction in LEFT_TO_RIGHT .. BOTTOM_LEFT_TO_TOP_RIGHT ) direction else defDirection
340418 }
341419
420+ /* *
421+ * This method is used to set a listener to get callback
422+ * when progress animation ends
423+ * @param listener a lambda function to invoke after progress animation ends
424+ */
342425 fun setDoOnProgressEnd (listener : ((v: View ) -> Unit )) {
343426 doOnProgressEnd = listener
344427 }
0 commit comments