1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-23 08:30:44 +00:00

Add reusable classes extending AnimatorListenerAdapter.

This commit is contained in:
Isira Seneviratne 2022-07-30 01:50:23 +05:30
parent 630558ed4f
commit 8024b437e9

View File

@ -90,7 +90,9 @@ fun View.animate(
*/ */
fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @ColorInt colorEnd: Int) { fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @ColorInt colorEnd: Int) {
if (MainActivity.DEBUG) { if (MainActivity.DEBUG) {
Log.d(TAG, "animateBackgroundColor() called with: view = [$this], duration = [$duration], " + Log.d(
TAG,
"animateBackgroundColor() called with: view = [$this], duration = [$duration], " +
"colorStart = [$colorStart], colorEnd = [$colorEnd]" "colorStart = [$colorStart], colorEnd = [$colorEnd]"
) )
} }
@ -147,20 +149,13 @@ private fun View.animateAlpha(enterOrExit: Boolean, duration: Long, delay: Long,
if (enterOrExit) { if (enterOrExit) {
animate().setInterpolator(FastOutSlowInInterpolator()).alpha(1f) animate().setInterpolator(FastOutSlowInInterpolator()).alpha(1f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(ExecOnEndListener(execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
execOnEnd?.run()
}
}).start()
} else { } else {
animate().setInterpolator(FastOutSlowInInterpolator()).alpha(0f) animate().setInterpolator(FastOutSlowInInterpolator()).alpha(0f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(HideAndExecOnEndListener(this, execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
isGone = true
execOnEnd?.run()
}
}).start()
} }
} }
@ -172,11 +167,8 @@ private fun View.animateScaleAndAlpha(enterOrExit: Boolean, duration: Long, dela
.setInterpolator(FastOutSlowInInterpolator()) .setInterpolator(FastOutSlowInInterpolator())
.alpha(1f).scaleX(1f).scaleY(1f) .alpha(1f).scaleX(1f).scaleY(1f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(ExecOnEndListener(execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
execOnEnd?.run()
}
}).start()
} else { } else {
scaleX = 1f scaleX = 1f
scaleY = 1f scaleY = 1f
@ -184,12 +176,8 @@ private fun View.animateScaleAndAlpha(enterOrExit: Boolean, duration: Long, dela
.setInterpolator(FastOutSlowInInterpolator()) .setInterpolator(FastOutSlowInInterpolator())
.alpha(0f).scaleX(.8f).scaleY(.8f) .alpha(0f).scaleX(.8f).scaleY(.8f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(HideAndExecOnEndListener(this, execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
isGone = true
execOnEnd?.run()
}
}).start()
} }
} }
@ -202,11 +190,8 @@ private fun View.animateLightScaleAndAlpha(enterOrExit: Boolean, duration: Long,
.setInterpolator(FastOutSlowInInterpolator()) .setInterpolator(FastOutSlowInInterpolator())
.alpha(1f).scaleX(1f).scaleY(1f) .alpha(1f).scaleX(1f).scaleY(1f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(ExecOnEndListener(execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
execOnEnd?.run()
}
}).start()
} else { } else {
alpha = 1f alpha = 1f
scaleX = 1f scaleX = 1f
@ -215,12 +200,8 @@ private fun View.animateLightScaleAndAlpha(enterOrExit: Boolean, duration: Long,
.setInterpolator(FastOutSlowInInterpolator()) .setInterpolator(FastOutSlowInInterpolator())
.alpha(0f).scaleX(.95f).scaleY(.95f) .alpha(0f).scaleX(.95f).scaleY(.95f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(HideAndExecOnEndListener(this, execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
isGone = true
execOnEnd?.run()
}
}).start()
} }
} }
@ -231,22 +212,15 @@ private fun View.animateSlideAndAlpha(enterOrExit: Boolean, duration: Long, dela
animate() animate()
.setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f) .setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(ExecOnEndListener(execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
execOnEnd?.run()
}
}).start()
} else { } else {
animate() animate()
.setInterpolator(FastOutSlowInInterpolator()) .setInterpolator(FastOutSlowInInterpolator())
.alpha(0f).translationY(-height.toFloat()) .alpha(0f).translationY(-height.toFloat())
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(HideAndExecOnEndListener(this, execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
isGone = true
execOnEnd?.run()
}
}).start()
} }
} }
@ -257,21 +231,14 @@ private fun View.animateLightSlideAndAlpha(enterOrExit: Boolean, duration: Long,
animate() animate()
.setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f) .setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(ExecOnEndListener(execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
execOnEnd?.run()
}
}).start()
} else { } else {
animate().setInterpolator(FastOutSlowInInterpolator()) animate().setInterpolator(FastOutSlowInInterpolator())
.alpha(0f).translationY(-height / 2.0f) .alpha(0f).translationY(-height / 2.0f)
.setDuration(duration).setStartDelay(delay) .setDuration(duration).setStartDelay(delay)
.setListener(object : AnimatorListenerAdapter() { .setListener(HideAndExecOnEndListener(this, execOnEnd))
override fun onAnimationEnd(animation: Animator) { .start()
isGone = true
execOnEnd?.run()
}
}).start()
} }
} }
@ -293,11 +260,7 @@ fun View.slideUp(
.setStartDelay(delay) .setStartDelay(delay)
.setDuration(duration) .setDuration(duration)
.setInterpolator(FastOutSlowInInterpolator()) .setInterpolator(FastOutSlowInInterpolator())
.setListener(object : AnimatorListenerAdapter() { .setListener(ExecOnEndListener(execOnEnd))
override fun onAnimationEnd(animation: Animator) {
execOnEnd?.run()
}
})
.start() .start()
} }
@ -311,6 +274,20 @@ fun View.animateHideRecyclerViewAllowingScrolling() {
animate().alpha(0.0f).setDuration(200).start() animate().alpha(0.0f).setDuration(200).start()
} }
private open class ExecOnEndListener(private val execOnEnd: Runnable?) : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
execOnEnd?.run()
}
}
private class HideAndExecOnEndListener(private val view: View, execOnEnd: Runnable?) :
ExecOnEndListener(execOnEnd) {
override fun onAnimationEnd(animation: Animator) {
view.isGone = true
super.onAnimationEnd(animation)
}
}
enum class AnimationType { enum class AnimationType {
ALPHA, SCALE_AND_ALPHA, LIGHT_SCALE_AND_ALPHA, SLIDE_AND_ALPHA, LIGHT_SLIDE_AND_ALPHA ALPHA, SCALE_AND_ALPHA, LIGHT_SCALE_AND_ALPHA, SLIDE_AND_ALPHA, LIGHT_SLIDE_AND_ALPHA
} }