Skip to content

Instantly share code, notes, and snippets.

@iserbin
Forked from gpeal/fadeTo.kt
Created April 7, 2021 08:47
Show Gist options
  • Save iserbin/183ea50e994f72503f324c6f9314c06c to your computer and use it in GitHub Desktop.
Save iserbin/183ea50e994f72503f324c6f9314c06c to your computer and use it in GitHub Desktop.

Revisions

  1. @gpeal gpeal revised this gist Jan 21, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions fadeTo.kt
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,8 @@ fun View.fadeTo(visible: Boolean, duration: Long = 500, startDelay: Long = 0, to

    setTag(tagKey, visible)
    setTag("fadeToAlpha".hashCode(), toAlpha)

    if (visible && alpha == 1f) alpha = 0f
    animate()
    .alpha(if (visible) toAlpha else 0f)
    .withStartAction {
  2. @gpeal gpeal revised this gist Jan 17, 2021. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion fadeTo.kt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    /**
    * Fade a view to visible or gone. This function is idempotent - it can be called over and over again with the same value without affecting an in-progress animation.
    * Fade a view to visible or gone. This function is idempotent - it can be called over and over again with the same
    * value without affecting an in-progress animation.
    */
    fun View.fadeTo(visible: Boolean, duration: Long = 500, startDelay: Long = 0, toAlpha: Float = 1f) {
    // Make this idempotent.
  3. @gpeal gpeal revised this gist Jan 17, 2021. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions fadeTo.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    /**
    * Fade a view to visible or gone. This function is idempotent - it can be called over and over again with the same value without affecting an in-progress animation.
    */
    fun View.fadeTo(visible: Boolean, duration: Long = 500, startDelay: Long = 0, toAlpha: Float = 1f) {
    // Make this idempotent.
    val tagKey = "fadeTo".hashCode()
  4. @gpeal gpeal created this gist Jan 17, 2021.
    42 changes: 42 additions & 0 deletions fadeTo.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    fun View.fadeTo(visible: Boolean, duration: Long = 500, startDelay: Long = 0, toAlpha: Float = 1f) {
    // Make this idempotent.
    val tagKey = "fadeTo".hashCode()
    if (visible == isVisible && animation == null && getTag(tagKey) == null) return
    if (getTag(tagKey) == visible) return

    setTag(tagKey, visible)
    setTag("fadeToAlpha".hashCode(), toAlpha)
    animate()
    .alpha(if (visible) toAlpha else 0f)
    .withStartAction {
    if (visible) isVisible = true
    }
    .withEndAction {
    setTag(tagKey, null)
    if (isAttachedToWindow && !visible) isVisible = false
    }
    .setInterpolator(FastOutSlowInInterpolator())
    .setDuration(duration)
    .setStartDelay(startDelay)
    .start()
    }

    /**
    * Cancels the animation started by [fadeTo] and jumps to the end of it.
    */
    fun View.cancelFade() {
    val tagKey = "fadeTo".hashCode()
    val visible = getTag(tagKey)?.castOrNull<Boolean>() ?: return
    animate().cancel()
    isVisible = visible
    alpha = if (visible) getTag("fadeToAlpha".hashCode())?.castOrNull<Float>() ?: 1f else 0f
    setTag(tagKey, null)
    }

    /**
    * Cancels the fade for this view and any ancestors.
    */
    fun View.cancelFadeRecursively() {
    cancelFade()
    castOrNull<ViewGroup>()?.children?.asSequence()?.forEach { it.cancelFade() }
    }