Created
July 24, 2019 21:08
-
-
Save nickbutcher/3b18682130a6b1c008a425af2c1179a5 to your computer and use it in GitHub Desktop.
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
/* Copyright 2019 Google LLC. | |
SPDX-License-Identifier: Apache-2.0 */ | |
fun animateVisibility(view: View, visible: Boolean) { | |
val targetAlpha = if (visible) 1f else 0f | |
if (view.alpha == targetAlpha) return | |
view.visibility = View.VISIBLE | |
val spring = view.spring(SpringAnimation.ALPHA) | |
(view.getTag(R.id.tag_pending_end_listener) as? | |
DynamicAnimation.OnAnimationEndListener)?.let { | |
spring.removeEndListener(it) | |
} | |
if (!visible) { | |
val listener = { | |
override fun onAnimationEnd() { | |
spring.removeEndListener(this) | |
view.visibility = View.GONE | |
} | |
} | |
spring.addEndListener(listener) | |
view.setTag(R.id.tag_pending_end_listener, listener) | |
} | |
spring.animateToFinalPosition(targetAlpha) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment