From 3fe80ec5acf1b95e1df96b487004b263ae17102b Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 24 Apr 2021 07:45:12 +0530 Subject: [PATCH] Use Animator.addListener() extension. --- .../java/org/schabi/newpipe/ktx/TextView.kt | 13 ++------- .../main/java/org/schabi/newpipe/ktx/View.kt | 29 ++++++++----------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/ktx/TextView.kt b/app/src/main/java/org/schabi/newpipe/ktx/TextView.kt index cfb13a107..c70af1e7d 100644 --- a/app/src/main/java/org/schabi/newpipe/ktx/TextView.kt +++ b/app/src/main/java/org/schabi/newpipe/ktx/TextView.kt @@ -2,13 +2,12 @@ package org.schabi.newpipe.ktx -import android.animation.Animator -import android.animation.AnimatorListenerAdapter import android.animation.ArgbEvaluator import android.animation.ValueAnimator import android.util.Log import android.widget.TextView import androidx.annotation.ColorInt +import androidx.core.animation.addListener import androidx.interpolator.view.animation.FastOutSlowInInterpolator import org.schabi.newpipe.MainActivity @@ -34,14 +33,6 @@ fun TextView.animateTextColor(duration: Long, @ColorInt colorStart: Int, @ColorI viewPropertyAnimator.interpolator = FastOutSlowInInterpolator() viewPropertyAnimator.duration = duration viewPropertyAnimator.addUpdateListener { setTextColor(it.animatedValue as Int) } - viewPropertyAnimator.addListener(object : AnimatorListenerAdapter() { - override fun onAnimationEnd(animation: Animator) { - setTextColor(colorEnd) - } - - override fun onAnimationCancel(animation: Animator) { - setTextColor(colorEnd) - } - }) + viewPropertyAnimator.addListener(onCancel = { setTextColor(colorEnd) }, onEnd = { setTextColor(colorEnd) }) viewPropertyAnimator.start() } diff --git a/app/src/main/java/org/schabi/newpipe/ktx/View.kt b/app/src/main/java/org/schabi/newpipe/ktx/View.kt index 2fd80703c..8f2249493 100644 --- a/app/src/main/java/org/schabi/newpipe/ktx/View.kt +++ b/app/src/main/java/org/schabi/newpipe/ktx/View.kt @@ -11,6 +11,7 @@ import android.util.Log import android.view.View import androidx.annotation.ColorInt import androidx.annotation.FloatRange +import androidx.core.animation.addListener import androidx.core.view.ViewCompat import androidx.core.view.isGone import androidx.core.view.isInvisible @@ -106,15 +107,10 @@ fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @Colo viewPropertyAnimator.addUpdateListener { animation: ValueAnimator -> backgroundTintListCompat = ColorStateList(empty, intArrayOf(animation.animatedValue as Int)) } - viewPropertyAnimator.addListener(object : AnimatorListenerAdapter() { - override fun onAnimationEnd(animation: Animator) { - backgroundTintListCompat = ColorStateList(empty, intArrayOf(colorEnd)) - } - - override fun onAnimationCancel(animation: Animator) { - onAnimationEnd(animation) - } - }) + viewPropertyAnimator.addListener( + onCancel = { backgroundTintListCompat = ColorStateList(empty, intArrayOf(colorEnd)) }, + onEnd = { backgroundTintListCompat = ColorStateList(empty, intArrayOf(colorEnd)) } + ) viewPropertyAnimator.start() } @@ -134,17 +130,16 @@ fun View.animateHeight(duration: Long, targetHeight: Int): ValueAnimator { layoutParams.height = value.toInt() requestLayout() } - animator.addListener(object : AnimatorListenerAdapter() { - override fun onAnimationEnd(animation: Animator) { + animator.addListener( + onCancel = { + layoutParams.height = targetHeight + requestLayout() + }, + onEnd = { layoutParams.height = targetHeight requestLayout() } - - override fun onAnimationCancel(animation: Animator) { - layoutParams.height = targetHeight - requestLayout() - } - }) + ) animator.start() return animator }