Skip to content
This repository was archived by the owner on Nov 14, 2018. It is now read-only.

Commit 47881f8

Browse files
committed
Added ViewPropertyAnimator extensions
1 parent c803919 commit 47881f8

3 files changed

Lines changed: 178 additions & 0 deletions

File tree

api/current.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ package androidx.core.animation {
1212
method public static android.animation.Animator.AnimatorListener doOnStart(android.animation.Animator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit> action);
1313
}
1414

15+
public final class ViewPropertyAnimatorKt {
16+
ctor public ViewPropertyAnimatorKt();
17+
method public static android.animation.Animator.AnimatorListener doOnCancel(android.view.ViewPropertyAnimator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit> action);
18+
method public static android.animation.Animator.AnimatorListener doOnEnd(android.view.ViewPropertyAnimator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit> action);
19+
method public static android.animation.Animator.AnimatorListener doOnRepeat(android.view.ViewPropertyAnimator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit> action);
20+
method public static android.animation.Animator.AnimatorListener doOnStart(android.view.ViewPropertyAnimator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit> action);
21+
method public static android.animation.ValueAnimator.AnimatorUpdateListener doOnUpdate(android.view.ViewPropertyAnimator, kotlin.jvm.functions.Function1<? super android.animation.ValueAnimator,kotlin.Unit> action);
22+
method public static android.animation.Animator.AnimatorListener setListener(android.view.ViewPropertyAnimator, kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onEnd = "null", kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onStart = "null", kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onCancel = "null", kotlin.jvm.functions.Function1<? super android.animation.Animator,kotlin.Unit>? onRepeat = "null");
23+
method public static android.animation.ValueAnimator.AnimatorUpdateListener setUpdateListener(android.view.ViewPropertyAnimator, kotlin.jvm.functions.Function1<? super android.animation.ValueAnimator,kotlin.Unit>? onUpdate = "null");
24+
}
25+
1526
}
1627

1728
package androidx.core.content {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package androidx.core.animation
2+
3+
import android.support.test.InstrumentationRegistry
4+
import android.support.test.annotation.UiThreadTest
5+
import android.support.test.filters.SdkSuppress
6+
import android.support.test.runner.AndroidJUnit4
7+
import android.view.View
8+
import android.view.ViewPropertyAnimator
9+
import org.junit.Assert
10+
import org.junit.Before
11+
import org.junit.Test
12+
import org.junit.runner.RunWith
13+
14+
@RunWith(AndroidJUnit4::class)
15+
class ViewPropertyAnimatorTest {
16+
private val context = InstrumentationRegistry.getContext()
17+
private val view = View(context)
18+
19+
private lateinit var animator: ViewPropertyAnimator
20+
21+
@Before
22+
fun before() {
23+
view.alpha = 0f
24+
animator = view.animate().alpha(1f).setDuration(0)
25+
}
26+
27+
@UiThreadTest
28+
@Test fun testDoOnStart() {
29+
var called = false
30+
animator.doOnStart {
31+
called = true
32+
}
33+
34+
animator.start()
35+
Assert.assertTrue(called)
36+
}
37+
38+
@UiThreadTest
39+
@Test fun testDoOnEnd() {
40+
var called = false
41+
animator.doOnEnd {
42+
called = true
43+
}
44+
45+
animator.start()
46+
animator.cancel()
47+
Assert.assertTrue(called)
48+
}
49+
50+
@UiThreadTest
51+
@Test fun testDoOnCancel() {
52+
var cancelCalled = false
53+
animator.doOnCancel {
54+
cancelCalled = true
55+
}
56+
57+
animator.start()
58+
animator.cancel()
59+
Assert.assertTrue(cancelCalled)
60+
}
61+
62+
@UiThreadTest
63+
@SdkSuppress(minSdkVersion = 19)
64+
@Test fun testDoOnUpdate() {
65+
var called = false
66+
animator.doOnUpdate {
67+
called = true
68+
}
69+
70+
animator.start()
71+
Assert.assertTrue(called)
72+
}
73+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)