Skip to content

Commit 8024b43

Browse files
Add reusable classes extending AnimatorListenerAdapter.
1 parent 630558e commit 8024b43

1 file changed

Lines changed: 38 additions & 61 deletions

File tree

  • app/src/main/java/org/schabi/newpipe/ktx

app/src/main/java/org/schabi/newpipe/ktx/View.kt

Lines changed: 38 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ fun View.animate(
9090
*/
9191
fun View.animateBackgroundColor(duration: Long, @ColorInt colorStart: Int, @ColorInt colorEnd: Int) {
9292
if (MainActivity.DEBUG) {
93-
Log.d(TAG, "animateBackgroundColor() called with: view = [$this], duration = [$duration], " +
93+
Log.d(
94+
TAG,
95+
"animateBackgroundColor() called with: view = [$this], duration = [$duration], " +
9496
"colorStart = [$colorStart], colorEnd = [$colorEnd]"
9597
)
9698
}
@@ -147,20 +149,13 @@ private fun View.animateAlpha(enterOrExit: Boolean, duration: Long, delay: Long,
147149
if (enterOrExit) {
148150
animate().setInterpolator(FastOutSlowInInterpolator()).alpha(1f)
149151
.setDuration(duration).setStartDelay(delay)
150-
.setListener(object : AnimatorListenerAdapter() {
151-
override fun onAnimationEnd(animation: Animator) {
152-
execOnEnd?.run()
153-
}
154-
}).start()
152+
.setListener(ExecOnEndListener(execOnEnd))
153+
.start()
155154
} else {
156155
animate().setInterpolator(FastOutSlowInInterpolator()).alpha(0f)
157156
.setDuration(duration).setStartDelay(delay)
158-
.setListener(object : AnimatorListenerAdapter() {
159-
override fun onAnimationEnd(animation: Animator) {
160-
isGone = true
161-
execOnEnd?.run()
162-
}
163-
}).start()
157+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
158+
.start()
164159
}
165160
}
166161

@@ -172,24 +167,17 @@ private fun View.animateScaleAndAlpha(enterOrExit: Boolean, duration: Long, dela
172167
.setInterpolator(FastOutSlowInInterpolator())
173168
.alpha(1f).scaleX(1f).scaleY(1f)
174169
.setDuration(duration).setStartDelay(delay)
175-
.setListener(object : AnimatorListenerAdapter() {
176-
override fun onAnimationEnd(animation: Animator) {
177-
execOnEnd?.run()
178-
}
179-
}).start()
170+
.setListener(ExecOnEndListener(execOnEnd))
171+
.start()
180172
} else {
181173
scaleX = 1f
182174
scaleY = 1f
183175
animate()
184176
.setInterpolator(FastOutSlowInInterpolator())
185177
.alpha(0f).scaleX(.8f).scaleY(.8f)
186178
.setDuration(duration).setStartDelay(delay)
187-
.setListener(object : AnimatorListenerAdapter() {
188-
override fun onAnimationEnd(animation: Animator) {
189-
isGone = true
190-
execOnEnd?.run()
191-
}
192-
}).start()
179+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
180+
.start()
193181
}
194182
}
195183

@@ -202,11 +190,8 @@ private fun View.animateLightScaleAndAlpha(enterOrExit: Boolean, duration: Long,
202190
.setInterpolator(FastOutSlowInInterpolator())
203191
.alpha(1f).scaleX(1f).scaleY(1f)
204192
.setDuration(duration).setStartDelay(delay)
205-
.setListener(object : AnimatorListenerAdapter() {
206-
override fun onAnimationEnd(animation: Animator) {
207-
execOnEnd?.run()
208-
}
209-
}).start()
193+
.setListener(ExecOnEndListener(execOnEnd))
194+
.start()
210195
} else {
211196
alpha = 1f
212197
scaleX = 1f
@@ -215,12 +200,8 @@ private fun View.animateLightScaleAndAlpha(enterOrExit: Boolean, duration: Long,
215200
.setInterpolator(FastOutSlowInInterpolator())
216201
.alpha(0f).scaleX(.95f).scaleY(.95f)
217202
.setDuration(duration).setStartDelay(delay)
218-
.setListener(object : AnimatorListenerAdapter() {
219-
override fun onAnimationEnd(animation: Animator) {
220-
isGone = true
221-
execOnEnd?.run()
222-
}
223-
}).start()
203+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
204+
.start()
224205
}
225206
}
226207

@@ -231,22 +212,15 @@ private fun View.animateSlideAndAlpha(enterOrExit: Boolean, duration: Long, dela
231212
animate()
232213
.setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f)
233214
.setDuration(duration).setStartDelay(delay)
234-
.setListener(object : AnimatorListenerAdapter() {
235-
override fun onAnimationEnd(animation: Animator) {
236-
execOnEnd?.run()
237-
}
238-
}).start()
215+
.setListener(ExecOnEndListener(execOnEnd))
216+
.start()
239217
} else {
240218
animate()
241219
.setInterpolator(FastOutSlowInInterpolator())
242220
.alpha(0f).translationY(-height.toFloat())
243221
.setDuration(duration).setStartDelay(delay)
244-
.setListener(object : AnimatorListenerAdapter() {
245-
override fun onAnimationEnd(animation: Animator) {
246-
isGone = true
247-
execOnEnd?.run()
248-
}
249-
}).start()
222+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
223+
.start()
250224
}
251225
}
252226

@@ -257,21 +231,14 @@ private fun View.animateLightSlideAndAlpha(enterOrExit: Boolean, duration: Long,
257231
animate()
258232
.setInterpolator(FastOutSlowInInterpolator()).alpha(1f).translationY(0f)
259233
.setDuration(duration).setStartDelay(delay)
260-
.setListener(object : AnimatorListenerAdapter() {
261-
override fun onAnimationEnd(animation: Animator) {
262-
execOnEnd?.run()
263-
}
264-
}).start()
234+
.setListener(ExecOnEndListener(execOnEnd))
235+
.start()
265236
} else {
266237
animate().setInterpolator(FastOutSlowInInterpolator())
267238
.alpha(0f).translationY(-height / 2.0f)
268239
.setDuration(duration).setStartDelay(delay)
269-
.setListener(object : AnimatorListenerAdapter() {
270-
override fun onAnimationEnd(animation: Animator) {
271-
isGone = true
272-
execOnEnd?.run()
273-
}
274-
}).start()
240+
.setListener(HideAndExecOnEndListener(this, execOnEnd))
241+
.start()
275242
}
276243
}
277244

@@ -293,11 +260,7 @@ fun View.slideUp(
293260
.setStartDelay(delay)
294261
.setDuration(duration)
295262
.setInterpolator(FastOutSlowInInterpolator())
296-
.setListener(object : AnimatorListenerAdapter() {
297-
override fun onAnimationEnd(animation: Animator) {
298-
execOnEnd?.run()
299-
}
300-
})
263+
.setListener(ExecOnEndListener(execOnEnd))
301264
.start()
302265
}
303266

@@ -311,6 +274,20 @@ fun View.animateHideRecyclerViewAllowingScrolling() {
311274
animate().alpha(0.0f).setDuration(200).start()
312275
}
313276

277+
private open class ExecOnEndListener(private val execOnEnd: Runnable?) : AnimatorListenerAdapter() {
278+
override fun onAnimationEnd(animation: Animator) {
279+
execOnEnd?.run()
280+
}
281+
}
282+
283+
private class HideAndExecOnEndListener(private val view: View, execOnEnd: Runnable?) :
284+
ExecOnEndListener(execOnEnd) {
285+
override fun onAnimationEnd(animation: Animator) {
286+
view.isGone = true
287+
super.onAnimationEnd(animation)
288+
}
289+
}
290+
314291
enum class AnimationType {
315292
ALPHA, SCALE_AND_ALPHA, LIGHT_SCALE_AND_ALPHA, SLIDE_AND_ALPHA, LIGHT_SLIDE_AND_ALPHA
316293
}

0 commit comments

Comments
 (0)