Skip to content

Instantly share code, notes, and snippets.

@esensar
Last active November 27, 2020 22:56
Show Gist options
  • Save esensar/aa906d78e71ac6d690435aca1c6e7b9a to your computer and use it in GitHub Desktop.
Save esensar/aa906d78e71ac6d690435aca1c6e7b9a to your computer and use it in GitHub Desktop.
Fade Transition for Android (Kotlin)
class FadeTransition : Transition {
companion object {
private const val PROPNAME_ALPHA = "android:fadeTransition:alpha"
private val TRANSITION_PROPERTIES = arrayOf(PROPNAME_ALPHA)
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
constructor() : super()
private fun captureValues(transitionValues: TransitionValues) {
transitionValues.values[PROPNAME_ALPHA] = transitionValues.view.alpha
}
override fun captureStartValues(transitionValues: TransitionValues) = captureValues(transitionValues)
override fun captureEndValues(transitionValues: TransitionValues) = captureValues(transitionValues)
override fun getTransitionProperties() = TRANSITION_PROPERTIES
override fun createAnimator(
sceneRoot: ViewGroup,
startValues: TransitionValues?,
endValues: TransitionValues?
): Animator? {
if (startValues == null || endValues == null) {
return null
}
val startAlpha: Float = startValues.values[PROPNAME_ALPHA] as? Float ?: 1F
val endAlpha: Float = endValues.values[PROPNAME_ALPHA] as? Float ?: 1F
return ObjectAnimator.ofFloat(endValues.view, View.ALPHA, startAlpha, endAlpha)
}
}
<?xml version="1.0" encoding="utf-8"?>
<!-- New transition resource file, using FadeTransition, as well as defaults from android.R.transition.move -->
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<changeBounds/>
<changeTransform/>
<changeClipBounds/>
<changeImageTransform/>
<transition class="full.qualified.name.of.FadeTransition" />
</transitionSet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment