|
| 1 | +package androidx.core.animation |
| 2 | + |
| 3 | +import android.animation.Animator |
| 4 | +import android.animation.ValueAnimator |
| 5 | +import android.view.ViewPropertyAnimator |
| 6 | +import androidx.annotation.RequiresApi |
| 7 | + |
| 8 | +/** |
| 9 | + * Add an action which will be invoked when the animation has ended. |
| 10 | + * |
| 11 | + * @return the [Animator.AnimatorListener] added to the Animator |
| 12 | + * @see Animator.end |
| 13 | + */ |
| 14 | +fun ViewPropertyAnimator.doOnEnd(action: (animator: Animator) -> Unit) = |
| 15 | + setListener(onEnd = action) |
| 16 | + |
| 17 | +/** |
| 18 | + * Add an action which will be invoked when the animation has started. |
| 19 | + * |
| 20 | + * @return the [Animator.AnimatorListener] added to the Animator |
| 21 | + * @see Animator.start |
| 22 | + */ |
| 23 | +fun ViewPropertyAnimator.doOnStart(action: (animator: Animator) -> Unit) = |
| 24 | + setListener(onStart = action) |
| 25 | + |
| 26 | +/** |
| 27 | + * Add an action which will be invoked when the animation has been cancelled. |
| 28 | + * |
| 29 | + * @return the [Animator.AnimatorListener] added to the Animator |
| 30 | + * @see Animator.cancel |
| 31 | + */ |
| 32 | +fun ViewPropertyAnimator.doOnCancel(action: (animator: Animator) -> Unit) = |
| 33 | + setListener(onCancel = action) |
| 34 | + |
| 35 | +/** |
| 36 | + * Add an action which will be invoked when the animation has repeated. |
| 37 | + * @return the [Animator.AnimatorListener] added to the Animator |
| 38 | + */ |
| 39 | +fun ViewPropertyAnimator.doOnRepeat(action: (animator: Animator) -> Unit) = |
| 40 | + setListener(onRepeat = action) |
| 41 | + |
| 42 | +/** |
| 43 | + * Add an action which will be invoked when the animation has been updated. |
| 44 | + * |
| 45 | + * @return the [ValueAnimator.AnimatorUpdateListener] added to the Animator |
| 46 | + * @see Animator.pause |
| 47 | + */ |
| 48 | +@RequiresApi(19) |
| 49 | +fun ViewPropertyAnimator.doOnUpdate(action: (animator: ValueAnimator?) -> Unit) = |
| 50 | + setUpdateListener(onUpdate = action) |
| 51 | + |
| 52 | +/** |
| 53 | + * Add a listener to this Animator using the provided actions. |
| 54 | + */ |
| 55 | +fun ViewPropertyAnimator.setListener( |
| 56 | + onEnd: ((animator: Animator) -> Unit)? = null, |
| 57 | + onStart: ((animator: Animator) -> Unit)? = null, |
| 58 | + onCancel: ((animator: Animator) -> Unit)? = null, |
| 59 | + onRepeat: ((animator: Animator) -> Unit)? = null |
| 60 | +): Animator.AnimatorListener { |
| 61 | + val listener = object : Animator.AnimatorListener { |
| 62 | + override fun onAnimationRepeat(animator: Animator) { |
| 63 | + onRepeat?.invoke(animator) |
| 64 | + } |
| 65 | + |
| 66 | + override fun onAnimationEnd(animator: Animator) { |
| 67 | + onEnd?.invoke(animator) |
| 68 | + } |
| 69 | + |
| 70 | + override fun onAnimationCancel(animator: Animator) { |
| 71 | + onCancel?.invoke(animator) |
| 72 | + } |
| 73 | + |
| 74 | + override fun onAnimationStart(animator: Animator) { |
| 75 | + onStart?.invoke(animator) |
| 76 | + } |
| 77 | + } |
| 78 | + setListener(listener) |
| 79 | + return listener |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Add an update listener to this Animator using the provided actions. |
| 84 | + */ |
| 85 | +@RequiresApi(19) |
| 86 | +fun ViewPropertyAnimator.setUpdateListener( |
| 87 | + onUpdate: ((animator: ValueAnimator?) -> Unit)? = null |
| 88 | +): ValueAnimator.AnimatorUpdateListener { |
| 89 | + val listener = ValueAnimator.AnimatorUpdateListener { |
| 90 | + onUpdate?.invoke(it) |
| 91 | + } |
| 92 | + setUpdateListener(listener) |
| 93 | + return listener |
| 94 | +} |
0 commit comments