... i dati di associazione alla tua UI devono essere stateless. Tuttavia, le animazioni sono stateful.
/* Copyright 2019 Google LLC. SPDX-License-Identifier: Apache-2.0 */ data class LoginUiModel( val submitEnabled: Boolean, val loginInProgress: Boolean ) … setVisibility(login, !uiModel.loginInProgress) setVisibility(progress, uiModel.loginInProgress) … fun setVisibility(view: View, visible: Boolean) { view.visibility = if (visible) View.VISIBLE else View.GONE }
/* Copyright 2019 Google LLC. SPDX-License-Identifier: Apache-2.0 */ fun animateVisibility(view: View, visible: Boolean) { view.visibility = View.VISIBLE if (visible) { ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f).start() } else { ObjectAnimator.ofFloat(view, View.ALPHA, 1f, 0f).apply { doOnEnd { view.visibility = View.GONE } }.start() } }
/* Copyright 2019 Google LLC. SPDX-License-Identifier: Apache-2.0 */ - ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f).start() + ObjectAnimator.ofFloat(view, View.ALPHA, 1f).start()
/* 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
/* Copyright 2019 Google LLC. SPDX-License-Identifier: Apache-2.0 */ - val anim = ObjectAnimator.ofFloat(view, View.ALPHA, targetAlpha) - if (!visible) { - anim.doOnEnd { view.visibility = View.GONE } - } + val anim = view.animate().alpha(targetAlpha) + if (!visible) { + anim.withEndAction { view.visibility = View.GONE } + }
/* Copyright 2019 Google LLC. SPDX-License-Identifier: Apache-2.0 */ fun View.spring( property: ViewProperty ): SpringAnimation { val key = getKey(property) var springAnim = getTag(key) as? SpringAnimation? if (springAnim == null) { springAnim = SpringAnimation(this, property) setTag(key, springAnim) } return springAnim }
/* Copyright 2019 Google LLC. SPDX-License-Identifier: Apache-2.0 */ - val anim = view.animate().alpha(targetAlpha) + val spring = view.spring(SpringAnimation.ALPHA) + spring.animateToFinalPosition(targetAlpha)