1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.core.animation 18 19 import android.animation.Animator 20 import androidx.annotation.RequiresApi 21 22 /** 23 * Add an action which will be invoked when the animation has ended. 24 * 25 * @return the [Animator.AnimatorListener] added to the Animator 26 * @see Animator.end 27 */ 28 fun Animator.doOnEnd(action: (animator: Animator) -> Unit) = addListener(onEnd = action) 29 30 /** 31 * Add an action which will be invoked when the animation has started. 32 * 33 * @return the [Animator.AnimatorListener] added to the Animator 34 * @see Animator.start 35 */ 36 fun Animator.doOnStart(action: (animator: Animator) -> Unit) = addListener(onStart = action) 37 38 /** 39 * Add an action which will be invoked when the animation has been cancelled. 40 * 41 * @return the [Animator.AnimatorListener] added to the Animator 42 * @see Animator.cancel 43 */ 44 fun Animator.doOnCancel(action: (animator: Animator) -> Unit) = addListener(onCancel = action) 45 46 /** 47 * Add an action which will be invoked when the animation has repeated. 48 * @return the [Animator.AnimatorListener] added to the Animator 49 */ 50 fun Animator.doOnRepeat(action: (animator: Animator) -> Unit) = addListener(onRepeat = action) 51 52 /** 53 * Add an action which will be invoked when the animation has resumed after a pause. 54 * 55 * @return the [Animator.AnimatorPauseListener] added to the Animator 56 * @see Animator.resume 57 */ 58 @RequiresApi(19) 59 fun Animator.doOnResume(action: (animator: Animator) -> Unit) = addPauseListener(onResume = action) 60 61 /** 62 * Add an action which will be invoked when the animation has been paused. 63 * 64 * @return the [Animator.AnimatorPauseListener] added to the Animator 65 * @see Animator.pause 66 */ 67 @RequiresApi(19) 68 fun Animator.doOnPause(action: (animator: Animator) -> Unit) = addPauseListener(onPause = action) 69 70 /** 71 * Add a listener to this Animator using the provided actions. 72 */ 73 fun Animator.addListener( 74 onEnd: ((animator: Animator) -> Unit)? = null, 75 onStart: ((animator: Animator) -> Unit)? = null, 76 onCancel: ((animator: Animator) -> Unit)? = null, 77 onRepeat: ((animator: Animator) -> Unit)? = null 78 ): Animator.AnimatorListener { 79 val listener = object : Animator.AnimatorListener { 80 override fun onAnimationRepeat(animator: Animator) { 81 onRepeat?.invoke(animator) 82 } 83 84 override fun onAnimationEnd(animator: Animator) { 85 onEnd?.invoke(animator) 86 } 87 88 override fun onAnimationCancel(animator: Animator) { 89 onCancel?.invoke(animator) 90 } 91 92 override fun onAnimationStart(animator: Animator) { 93 onStart?.invoke(animator) 94 } 95 } 96 addListener(listener) 97 return listener 98 } 99 100 /** 101 * Add a pause and resume listener to this Animator using the provided actions. 102 */ 103 @RequiresApi(19) 104 fun Animator.addPauseListener( 105 onResume: ((animator: Animator) -> Unit)? = null, 106 onPause: ((animator: Animator) -> Unit)? = null 107 ): Animator.AnimatorPauseListener { 108 val listener = object : Animator.AnimatorPauseListener { 109 override fun onAnimationPause(animator: Animator) { 110 onPause?.invoke(animator) 111 } 112 113 override fun onAnimationResume(animator: Animator) { 114 onResume?.invoke(animator) 115 } 116 } 117 addPauseListener(listener) 118 return listener 119 } 120