This repository has been archived by the owner on Nov 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 563
Added ViewPropertyAnimator extensions #560
Open
ancirja
wants to merge
3
commits into
android:master
Choose a base branch
from
ancirja:ancirja/view_property_animator_extensions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/androidTest/java/androidx/core/animation/ViewPropertyAnimatorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* Copyright (C) 2017 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.core.animation | ||
|
||
import android.support.test.InstrumentationRegistry | ||
import android.support.test.annotation.UiThreadTest | ||
import android.support.test.filters.SdkSuppress | ||
import android.support.test.runner.AndroidJUnit4 | ||
import android.view.View | ||
import android.view.ViewPropertyAnimator | ||
import org.junit.Assert | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ViewPropertyAnimatorTest { | ||
private val context = InstrumentationRegistry.getContext() | ||
private val view = View(context) | ||
|
||
private lateinit var animator: ViewPropertyAnimator | ||
|
||
@Before | ||
fun before() { | ||
view.alpha = 0f | ||
animator = view.animate().alpha(1f).setDuration(0) | ||
} | ||
|
||
@UiThreadTest | ||
@Test fun testDoOnStart() { | ||
var called = false | ||
animator.doOnStart { | ||
called = true | ||
} | ||
|
||
animator.start() | ||
Assert.assertTrue(called) | ||
} | ||
|
||
@UiThreadTest | ||
@Test fun testDoOnEnd() { | ||
var called = false | ||
animator.doOnEnd { | ||
called = true | ||
} | ||
|
||
animator.start() | ||
animator.cancel() | ||
Assert.assertTrue(called) | ||
} | ||
|
||
@UiThreadTest | ||
@Test fun testDoOnCancel() { | ||
var cancelCalled = false | ||
animator.doOnCancel { | ||
cancelCalled = true | ||
} | ||
|
||
animator.start() | ||
animator.cancel() | ||
Assert.assertTrue(cancelCalled) | ||
} | ||
|
||
@UiThreadTest | ||
@SdkSuppress(minSdkVersion = 19) | ||
@Test fun testDoOnUpdate() { | ||
var called = false | ||
animator.doOnUpdate { | ||
called = true | ||
} | ||
|
||
animator.start() | ||
Assert.assertTrue(called) | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
src/main/java/androidx/core/animation/ViewPropertyAnimator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (C) 2017 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.core.animation | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing copyright header There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. I added the copyright header. |
||
|
||
import android.animation.Animator | ||
import android.animation.ValueAnimator | ||
import android.view.ViewPropertyAnimator | ||
import androidx.annotation.RequiresApi | ||
|
||
/** | ||
* Add an action which will be invoked when the animation has ended. | ||
* | ||
* @return the [Animator.AnimatorListener] added to the Animator | ||
* @see Animator.end | ||
*/ | ||
fun ViewPropertyAnimator.doOnEnd(action: (animator: Animator) -> Unit) = | ||
setListener(onEnd = action) | ||
|
||
/** | ||
* Add an action which will be invoked when the animation has started. | ||
* | ||
* @return the [Animator.AnimatorListener] added to the Animator | ||
* @see Animator.start | ||
*/ | ||
fun ViewPropertyAnimator.doOnStart(action: (animator: Animator) -> Unit) = | ||
setListener(onStart = action) | ||
|
||
/** | ||
* Add an action which will be invoked when the animation has been cancelled. | ||
* | ||
* @return the [Animator.AnimatorListener] added to the Animator | ||
* @see Animator.cancel | ||
*/ | ||
fun ViewPropertyAnimator.doOnCancel(action: (animator: Animator) -> Unit) = | ||
setListener(onCancel = action) | ||
|
||
/** | ||
* Add an action which will be invoked when the animation has repeated. | ||
* @return the [Animator.AnimatorListener] added to the Animator | ||
*/ | ||
fun ViewPropertyAnimator.doOnRepeat(action: (animator: Animator) -> Unit) = | ||
setListener(onRepeat = action) | ||
|
||
/** | ||
* Add an action which will be invoked when the animation has been updated. | ||
* | ||
* @return the [ValueAnimator.AnimatorUpdateListener] added to the Animator | ||
* @see Animator.pause | ||
*/ | ||
@RequiresApi(19) | ||
fun ViewPropertyAnimator.doOnUpdate(action: (animator: ValueAnimator?) -> Unit) = | ||
setUpdateListener(onUpdate = action) | ||
|
||
/** | ||
* Add a listener to this Animator using the provided actions. | ||
*/ | ||
fun ViewPropertyAnimator.setListener( | ||
onEnd: ((animator: Animator) -> Unit)? = null, | ||
onStart: ((animator: Animator) -> Unit)? = null, | ||
onCancel: ((animator: Animator) -> Unit)? = null, | ||
onRepeat: ((animator: Animator) -> Unit)? = null | ||
): Animator.AnimatorListener { | ||
val listener = object : Animator.AnimatorListener { | ||
override fun onAnimationRepeat(animator: Animator) { | ||
onRepeat?.invoke(animator) | ||
} | ||
|
||
override fun onAnimationEnd(animator: Animator) { | ||
onEnd?.invoke(animator) | ||
} | ||
|
||
override fun onAnimationCancel(animator: Animator) { | ||
onCancel?.invoke(animator) | ||
} | ||
|
||
override fun onAnimationStart(animator: Animator) { | ||
onStart?.invoke(animator) | ||
} | ||
} | ||
setListener(listener) | ||
return listener | ||
} | ||
|
||
/** | ||
* Add an update listener to this Animator using the provided actions. | ||
*/ | ||
@RequiresApi(19) | ||
fun ViewPropertyAnimator.setUpdateListener( | ||
onUpdate: ((animator: ValueAnimator?) -> Unit)? = null | ||
): ValueAnimator.AnimatorUpdateListener { | ||
val listener = ValueAnimator.AnimatorUpdateListener { | ||
onUpdate?.invoke(it) | ||
} | ||
setUpdateListener(listener) | ||
return listener | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing copyright header
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. I added the copyright header.