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

Merge pull request #8708 from Isira-Seneviratne/Reduce_View.kt_size

Reduce View.kt size.
This commit is contained in:
Stypox 2022-08-06 10:59:10 +02:00 committed by GitHub
commit d9230c0103
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -92,62 +92,43 @@ fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @Colo
if (MainActivity.DEBUG) { if (MainActivity.DEBUG) {
Log.d( Log.d(
TAG, TAG,
"animateBackgroundColor() called with: " + "animateBackgroundColor() called with: view = [$this], duration = [$duration], " +
"view = [" + this + "], duration = [" + duration + "], " + "colorStart = [$colorStart], colorEnd = [$colorEnd]"
"colorStart = [" + colorStart + "], colorEnd = [" + colorEnd + "]"
) )
} }
val empty = arrayOf(IntArray(0))
val viewPropertyAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorStart, colorEnd) val viewPropertyAnimator = ValueAnimator.ofObject(ArgbEvaluator(), colorStart, colorEnd)
viewPropertyAnimator.interpolator = FastOutSlowInInterpolator() viewPropertyAnimator.interpolator = FastOutSlowInInterpolator()
viewPropertyAnimator.duration = duration viewPropertyAnimator.duration = duration
viewPropertyAnimator.addUpdateListener { animation: ValueAnimator ->
ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(animation.animatedValue as Int))) fun listenerAction(color: Int) {
ViewCompat.setBackgroundTintList(this, ColorStateList.valueOf(color))
} }
viewPropertyAnimator.addListener( viewPropertyAnimator.addUpdateListener { listenerAction(it.animatedValue as Int) }
onCancel = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) }, viewPropertyAnimator.addListener(onCancel = { listenerAction(colorEnd) }, onEnd = { listenerAction(colorEnd) })
onEnd = { ViewCompat.setBackgroundTintList(this, ColorStateList(empty, intArrayOf(colorEnd))) }
)
viewPropertyAnimator.start() viewPropertyAnimator.start()
} }
fun View.animateHeight(duration: Long, targetHeight: Int): ValueAnimator { fun View.animateHeight(duration: Long, targetHeight: Int): ValueAnimator {
if (MainActivity.DEBUG) { if (MainActivity.DEBUG) {
Log.d( Log.d(TAG, "animateHeight: duration = [$duration], from $height to → $targetHeight in: $this")
TAG,
"animateHeight: duration = [" + duration + "], " +
"from " + height + " to → " + targetHeight + " in: " + this
)
} }
val animator = ValueAnimator.ofFloat(height.toFloat(), targetHeight.toFloat()) val animator = ValueAnimator.ofFloat(height.toFloat(), targetHeight.toFloat())
animator.interpolator = FastOutSlowInInterpolator() animator.interpolator = FastOutSlowInInterpolator()
animator.duration = duration animator.duration = duration
animator.addUpdateListener { animation: ValueAnimator ->
val value = animation.animatedValue as Float fun listenerAction(value: Int) {
layoutParams.height = value.toInt() layoutParams.height = value
requestLayout() requestLayout()
} }
animator.addListener( animator.addUpdateListener { listenerAction((it.animatedValue as Float).toInt()) }
onCancel = { animator.addListener(onCancel = { listenerAction(targetHeight) }, onEnd = { listenerAction(targetHeight) })
layoutParams.height = targetHeight
requestLayout()
},
onEnd = {
layoutParams.height = targetHeight
requestLayout()
}
)
animator.start() animator.start()
return animator return animator
} }
fun View.animateRotation(duration: Long, targetRotation: Int) { fun View.animateRotation(duration: Long, targetRotation: Int) {
if (MainActivity.DEBUG) { if (MainActivity.DEBUG) {
Log.d( Log.d(TAG, "animateRotation: duration = [$duration], from $rotation to → $targetRotation in: $this")
TAG,
"animateRotation: duration = [" + duration + "], " +
"from " + rotation + " to → " + targetRotation + " in: " + this
)
} }
animate().setListener(null).cancel() animate().setListener(null).cancel()
animate() animate()
@ -168,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()
} }
} }
@ -193,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
@ -205,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()
} }
} }
@ -223,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
@ -236,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()
} }
} }
@ -252,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()
} }
} }
@ -278,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()
} }
} }
@ -314,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()
} }
@ -332,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
} }