From bf99786d28da16f67b6477beab89d1a95122ce73 Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Sat, 17 Oct 2020 00:15:16 -0300 Subject: [PATCH 01/14] Clear unnecessary code, add callback on expandable animations --- .../jerry/FadeSpringAnimation.kt | 54 +++---------------- .../ExpandableFadingSpringAnimation.kt | 23 ++------ .../expandable/ExpandableSpingAnimation.kt | 28 +++------- 3 files changed, 19 insertions(+), 86 deletions(-) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt index 57725c8..4d51c44 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt @@ -35,7 +35,12 @@ fun View.fadeOutSpring( stiffness: Float = ANIMATION_STIFFNESS, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - hideFadeOutSpring(stiffness, hide = { gone() }, onAnimationEnd = onAnimationEnd) + fadeSpring(stiffness = stiffness).startFadeOutSpringAnimation( + onAnimationEnd = { canceled -> + gone() + onAnimationEnd?.invoke(canceled) + } + ) } /** @@ -54,53 +59,13 @@ fun View.fadeInSpring( stiffness: Float = ANIMATION_STIFFNESS, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - if (isFadeInRunning() || (alpha == 1f && isVisible() && isFadeOutRunning().not())) { - if (isFadeInRunning().not()) { - onAnimationEnd?.invoke(false) - } - return - } - startFadeInRunning() - if (alpha == 1f) alpha = 0f + if (isVisible().not()) alpha = 0f visible() - fadeSpring(stiffness = stiffness).startFadeInSpringAnimation( onAnimationEnd = onAnimationEnd ) } -/** - * Start the fade out animation without changing the visibility status. The changes in the - * visibility status is delegate to the function [hide]. - * - * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to - * the object attached when the spring is not at the final position. Default stiffness is - * [ANIMATION_STIFFNESS]. - * @param onAnimationEnd The function to call when the animation is finished. - * - * @see [SpringAnimation] - */ -fun View.hideFadeOutSpring( - stiffness: Float, - hide: (() -> Unit)? = null, - onAnimationEnd: ((canceled: Boolean) -> Unit)? -) { - if (isVisible().not() || isFadeOutRunning()) { - if (isFadeOutRunning().not()) { - onAnimationEnd?.invoke(false) - } - return - } - startFadeOutRunning() - - fadeSpring(stiffness = stiffness).startFadeOutSpringAnimation( - onAnimationEnd = { canceled -> - hide?.invoke() - onAnimationEnd?.invoke(canceled) - } - ) -} - fun JerryAnimation.startFadeOutSpringAnimation( onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) = startFadeSpringAnimation( @@ -154,8 +119,5 @@ fun JerryAnimation.startFadeSpringAnimation( onAnimationEnd: ((canceled: Boolean) -> Unit)? = null, ) = startSpringAnimation( targetValue = targetValue, - onAnimationEnd = { canceled -> - view.clearFadeInFadeOutRunning() - onAnimationEnd?.invoke(canceled) - } + onAnimationEnd = onAnimationEnd ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt index 74070b1..a3fc498 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt @@ -5,8 +5,9 @@ import androidx.dynamicanimation.animation.SpringAnimation import br.alexandregpereira.jerry.ANIMATION_STIFFNESS import br.alexandregpereira.jerry.fadeInSpring import br.alexandregpereira.jerry.fadeOutSpring -import br.alexandregpereira.jerry.hideFadeOutSpring +import br.alexandregpereira.jerry.fadeSpring import br.alexandregpereira.jerry.isVisible +import br.alexandregpereira.jerry.startFadeOutSpringAnimation /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the @@ -92,7 +93,7 @@ fun View.collapseHeightFadingSpring( ) = collapseFadingSpring(stiffness, isHeight = true, onAnimationEnd = onAnimationEnd) /** - * Uses the [hideFadeOutSpring] and [collapseWidthSpring] animations in sequence. This animation + * Uses the [fadeSpring] and [collapseWidthSpring] animations in sequence. This animation * handles double click. This method can be reverted in the middle of the animation if the * [expandWidthFadingSpring] method is called. * @@ -113,12 +114,7 @@ private fun View.collapseFadingSpring( isHeight: Boolean = true, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - if (isExpandingRunning()) { - collapseSpring(isHeight = isHeight, onAnimationEnd = onAnimationEnd) - return - } - - hideFadeOutSpring(stiffness = stiffness * 2f) { + fadeSpring(stiffness = stiffness * 2f).startFadeOutSpringAnimation { collapseSpring( stiffness = stiffness * 2f, isHeight = isHeight, @@ -132,16 +128,7 @@ private fun View.expandFadingSpring( isHeight: Boolean = true, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - if (alpha == 1f && (isVisible() && isCollapsingRunning().not())) { - return - } - if (alpha == 1f) alpha = 0f - - if (alpha > 0f && alpha < 1f) { - fadeInSpring(onAnimationEnd = onAnimationEnd) - return - } - + if (isVisible().not()) alpha = 0f expandSpring(stiffness = stiffness * 2f, isHeight = isHeight) { fadeInSpring(stiffness = stiffness * 2f, onAnimationEnd = onAnimationEnd) } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt index c7fc9bc..48acf1a 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt @@ -3,8 +3,6 @@ package br.alexandregpereira.jerry.expandable import android.view.View import androidx.dynamicanimation.animation.SpringAnimation import br.alexandregpereira.jerry.ANIMATION_STIFFNESS -import br.alexandregpereira.jerry.JerryAnimation -import br.alexandregpereira.jerry.JerryAnimationSet import br.alexandregpereira.jerry.gone import br.alexandregpereira.jerry.isVisible import br.alexandregpereira.jerry.spring @@ -146,6 +144,9 @@ internal fun View.collapseSpring( onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (isVisible().not() || isCollapsingRunning()) { + if (isCollapsingRunning().not()) { + onAnimationEnd?.invoke(false) + } return } startCollapsingRunning() @@ -171,6 +172,9 @@ internal fun View.expandSpring( onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (isExpandingRunning() || (isVisible() && isCollapsingRunning().not())) { + if (isExpandingRunning().not()) { + onAnimationEnd?.invoke(false) + } return } @@ -207,26 +211,6 @@ internal fun View.expandSpring( ) } -private fun JerryAnimationSet.expandCollapseSpring( - stiffness: Float, - isHeight: Boolean, - onProgressChange: ((progress: Float) -> Unit)?, -) = spring( - key = getExpandingCollapsingSpringKey(isHeight), - property = jerryAnimations.last().view.widthHeightViewProperty(isHeight, onProgressChange), - stiffness = stiffness -) - -private fun JerryAnimation.expandCollapseSpring( - stiffness: Float, - isHeight: Boolean, - onProgressChange: ((progress: Float) -> Unit)?, -) = spring( - key = getExpandingCollapsingSpringKey(isHeight), - property = view.widthHeightViewProperty(isHeight, onProgressChange), - stiffness = stiffness -) - private fun View.expandCollapseSpring( stiffness: Float, isHeight: Boolean, From 54ab0e644a631dbe7b58ee29e6b4935484a6721b Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Sat, 17 Oct 2020 00:15:37 -0300 Subject: [PATCH 02/14] Clear unnecessary code, add callback on expandable animations --- jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt | 2 +- .../alexandregpereira/jerry/textview/TextViewSpringAnimation.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt index b39110b..ca4ae1e 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt @@ -3,7 +3,7 @@ package br.alexandregpereira.jerry import android.view.View import android.view.ViewPropertyAnimator -const val ANIMATION_SHORT_TIME = 200L +const val ANIMATION_SHORT_TIME = 300L enum class AnimationMode { /** diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt index 5ccfc60..18a0340 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt @@ -34,7 +34,7 @@ fun TextView.setTextExpandableSpring( return } - if (isVisible() && isCollapsingRunning().not() && isFadeOutRunning().not()) { + if (isVisible() && isCollapsingRunning().not()) { setTextFadeSpring( text, stiffness = stiffness, From 97495f51c56cb8e03cd28f265f31edec8916c956 Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Sat, 17 Oct 2020 02:45:11 -0300 Subject: [PATCH 03/14] Organize names and Jerry animation Api, Add damping ration as param --- .../animation/CollapseAnimationActivity.kt | 4 +- .../CollapseFadingSpringAnimationActivity.kt | 28 ++-- .../CollapseSpringAnimationActivity.kt | 34 ++--- .../ExpandFadingSpringAnimationActivity.kt | 32 ++--- .../ExpandSpringAnimationActivity.kt | 40 +++--- .../animation/FadeSpringAnimationActivity.kt | 12 +- .../GridRecyclerViewSpringActivity.kt | 8 +- .../jerry/ElevationSpringAnimation.kt | 18 ++- .../jerry/FadeAnimationCommon.kt | 5 +- .../jerry/FadeSpringAnimation.kt | 128 ++++++++++++------ .../alexandregpereira/jerry/JerryAnimation.kt | 2 +- .../jerry/SpringAnimation.kt | 82 +++++------ .../jerry/TransalationSpringAnimation.kt | 48 +++++-- .../animator/ElevationSpringItemAnimator.kt | 86 +++++++----- .../ExpandableFadingSpringAnimation.kt | 74 +++++----- .../expandable/ExpandableSpingAnimation.kt | 70 +++++----- .../jerry/textview/TextViewSpringAnimation.kt | 36 ++--- 17 files changed, 404 insertions(+), 303 deletions(-) diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt index 6ff8ba7..75ea15d 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt @@ -5,7 +5,7 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat -import br.alexandregpereira.jerry.expandable.animateWidthVisibility +import br.alexandregpereira.jerry.expandable.visibleOrGoneExpandableWidth import br.alexandregpereira.jerry.app.R import br.alexandregpereira.jerry.expandable.collapseHeight import br.alexandregpereira.jerry.expandable.collapseWidth @@ -71,7 +71,7 @@ class CollapseAnimationActivity : AppCompatActivity(R.layout.activity_collapse_a } collapseReverseMatchWidthButton.setOnClickListener { - collapseMatchWidthView.animateWidthVisibility( + collapseMatchWidthView.visibleOrGoneExpandableWidth( visible = collapseMatchWidthViewVisible.not().also { collapseMatchWidthViewVisible = it } diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt index 7ab0e36..35a2c49 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt @@ -5,10 +5,10 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeightFadingSpring -import br.alexandregpereira.jerry.expandable.collapseWidthFadingSpring -import br.alexandregpereira.jerry.expandable.expandHeightFadingSpring -import br.alexandregpereira.jerry.expandable.expandWidthFadingSpring +import br.alexandregpereira.jerry.expandable.goneCollapseHeightFadeOut +import br.alexandregpereira.jerry.expandable.goneCollapseWidthFadeOut +import br.alexandregpereira.jerry.expandable.visibleExpandHeightFadeIn +import br.alexandregpereira.jerry.expandable.visibleExpandWidthFadeIn import kotlinx.android.synthetic.main.activity_collapse_fading_animation.* class CollapseFadingSpringAnimationActivity : AppCompatActivity() { @@ -26,43 +26,43 @@ class CollapseFadingSpringAnimationActivity : AppCompatActivity() { collapseFadingLabel.setText(R.string.collapse_fading_spring) collapseFadingTextButton.setOnClickListener { - collapseFadingTextView.collapseHeightFadingSpring() + collapseFadingTextView.goneCollapseHeightFadeOut() } collapseExpandFadingTextButton.setOnClickListener { - collapseFadingTextView.expandHeightFadingSpring() + collapseFadingTextView.visibleExpandHeightFadeIn() } collapseFixedFadingTextButton.setOnClickListener { - collapseFixedFadingTextView.collapseHeightFadingSpring() + collapseFixedFadingTextView.goneCollapseHeightFadeOut() } collapseExpandFixedFadingTextButton.setOnClickListener { - collapseFixedFadingTextView.expandHeightFadingSpring() + collapseFixedFadingTextView.visibleExpandHeightFadeIn() } collapseWidthFadingTextButton.setOnClickListener { - collapseWidthFadingTextView.collapseWidthFadingSpring() + collapseWidthFadingTextView.goneCollapseWidthFadeOut() } collapseExpandWidthFadingTextButton.setOnClickListener { - collapseWidthFadingTextView.expandWidthFadingSpring() + collapseWidthFadingTextView.visibleExpandWidthFadeIn() } collapseMatchWidthFadingButton.setOnClickListener { - collapseMatchWidthFadingView.collapseWidthFadingSpring() + collapseMatchWidthFadingView.goneCollapseWidthFadeOut() } collapseExpandMatchWidthFadingButton.setOnClickListener { - collapseMatchWidthFadingView.expandWidthFadingSpring() + collapseMatchWidthFadingView.visibleExpandWidthFadeIn() } collapseFixedWidthFadingButton.setOnClickListener { - collapseFixedWidthFadingView.collapseWidthFadingSpring() + collapseFixedWidthFadingView.goneCollapseWidthFadeOut() } collapseExpandFixedWidthFadingButton.setOnClickListener { - collapseFixedWidthFadingView.expandWidthFadingSpring() + collapseFixedWidthFadingView.visibleExpandWidthFadeIn() } } } diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt index ead33e8..7dcf0df 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt @@ -5,12 +5,12 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat -import br.alexandregpereira.jerry.expandable.animateWidthVisibility +import br.alexandregpereira.jerry.expandable.visibleOrGoneExpandableWidth import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeightSpring -import br.alexandregpereira.jerry.expandable.collapseWidthSpring -import br.alexandregpereira.jerry.expandable.expandHeightSpring -import br.alexandregpereira.jerry.expandable.expandWidthSpring +import br.alexandregpereira.jerry.expandable.goneCollapseHeight +import br.alexandregpereira.jerry.expandable.goneCollapseWidth +import br.alexandregpereira.jerry.expandable.visibleExpandHeight +import br.alexandregpereira.jerry.expandable.visibleExpandWidth import kotlinx.android.synthetic.main.activity_collapse_animation.* import kotlinx.android.synthetic.main.container_animation_info.view.* @@ -35,9 +35,9 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll resources.getDimension(R.dimen.strong_elevation) ) - collapseTextView.expandHeightSpring() + collapseTextView.visibleExpandHeight() collapseTextButton.setOnClickListener { - collapseTextView.collapseHeightSpring( + collapseTextView.goneCollapseHeight( onProgressChange = { interpolatedTime -> collapseAnimationInfo.percentageTextView.text = (interpolatedTime * 100).toInt().toString() } @@ -47,7 +47,7 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll } collapseExpandTextButton.setOnClickListener { - collapseTextView.expandHeightSpring( + collapseTextView.visibleExpandHeight( onProgressChange = { interpolatedTime -> collapseAnimationInfo.percentageTextView.text = (interpolatedTime * 100).toInt().toString() } @@ -57,23 +57,23 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll } collapseFixedTextButton.setOnClickListener { - collapseFixedTextView.collapseHeightSpring() + collapseFixedTextView.goneCollapseHeight() } collapseExpandFixedTextButton.setOnClickListener { - collapseFixedTextView.expandHeightSpring() + collapseFixedTextView.visibleExpandHeight() } collapseWidthTextButton.setOnClickListener { - collapseWidthTextView.collapseWidthSpring() + collapseWidthTextView.goneCollapseWidth() } collapseExpandWidthTextButton.setOnClickListener { - collapseWidthTextView.expandWidthSpring() + collapseWidthTextView.visibleExpandWidth() } collapseReverseMatchWidthButton.setOnClickListener { - collapseMatchWidthView.animateWidthVisibility( + collapseMatchWidthView.visibleOrGoneExpandableWidth( visible = collapseMatchWidthViewVisible.not().also { collapseMatchWidthViewVisible = it } @@ -81,19 +81,19 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll } collapseMatchWidthButton.setOnClickListener { - collapseMatchWidthView.collapseWidthSpring() + collapseMatchWidthView.goneCollapseWidth() } collapseExpandMatchWidthButton.setOnClickListener { - collapseMatchWidthView.expandWidthSpring() + collapseMatchWidthView.visibleExpandWidth() } collapseFixedWidthButton.setOnClickListener { - collapseFixedWidthView.collapseWidthSpring() + collapseFixedWidthView.goneCollapseWidth() } collapseExpandFixedWidthButton.setOnClickListener { - collapseFixedWidthView.expandWidthSpring() + collapseFixedWidthView.visibleExpandWidth() } } } diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt index 149d487..73f66b0 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt @@ -5,10 +5,10 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeightFadingSpring -import br.alexandregpereira.jerry.expandable.collapseWidthFadingSpring -import br.alexandregpereira.jerry.expandable.expandHeightFadingSpring -import br.alexandregpereira.jerry.expandable.expandWidthFadingSpring +import br.alexandregpereira.jerry.expandable.goneCollapseHeightFadeOut +import br.alexandregpereira.jerry.expandable.goneCollapseWidthFadeOut +import br.alexandregpereira.jerry.expandable.visibleExpandHeightFadeIn +import br.alexandregpereira.jerry.expandable.visibleExpandWidthFadeIn import kotlinx.android.synthetic.main.activity_expand_fading_animation.* class ExpandFadingSpringAnimationActivity : AppCompatActivity() { @@ -26,51 +26,51 @@ class ExpandFadingSpringAnimationActivity : AppCompatActivity() { expandFadingLabel.setText(R.string.expand_fading_spring) expandFadingTextButton.setOnClickListener { - expandFadingTextView.expandHeightFadingSpring() + expandFadingTextView.visibleExpandHeightFadeIn() } expandCollapseFadingTextButton.setOnClickListener { - expandFadingTextView.collapseHeightFadingSpring() + expandFadingTextView.goneCollapseHeightFadeOut() } expandWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.expandWidthFadingSpring() + expandWidthFadingTextView.visibleExpandWidthFadeIn() } expandCollapseWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.collapseWidthFadingSpring() + expandWidthFadingTextView.goneCollapseWidthFadeOut() } expandFixedFadingTextButton.setOnClickListener { - expandFixedFadingTextView.expandHeightFadingSpring() + expandFixedFadingTextView.visibleExpandHeightFadeIn() } expandCollapseFixedFadingTextButton.setOnClickListener { - expandFixedFadingTextView.collapseHeightFadingSpring() + expandFixedFadingTextView.goneCollapseHeightFadeOut() } expandWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.expandWidthFadingSpring() + expandWidthFadingTextView.visibleExpandWidthFadeIn() } expandCollapseWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.collapseWidthFadingSpring() + expandWidthFadingTextView.goneCollapseWidthFadeOut() } expandMatchWidthFadingButton.setOnClickListener { - expandMatchWidthFadingView.expandWidthFadingSpring() + expandMatchWidthFadingView.visibleExpandWidthFadeIn() } expandCollapseMatchWidthFadingButton.setOnClickListener { - expandMatchWidthFadingView.collapseWidthFadingSpring() + expandMatchWidthFadingView.goneCollapseWidthFadeOut() } expandFixedWidthFadingButton.setOnClickListener { - expandFixedWidthFadingView.expandWidthFadingSpring() + expandFixedWidthFadingView.visibleExpandWidthFadeIn() } expandCollapseFixedWidthFadingButton.setOnClickListener { - expandFixedWidthFadingView.collapseWidthFadingSpring() + expandFixedWidthFadingView.goneCollapseWidthFadeOut() } } } diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt index d836eea..de21e77 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt @@ -5,10 +5,10 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeightSpring -import br.alexandregpereira.jerry.expandable.collapseWidthSpring -import br.alexandregpereira.jerry.expandable.expandHeightSpring -import br.alexandregpereira.jerry.expandable.expandWidthSpring +import br.alexandregpereira.jerry.expandable.goneCollapseHeight +import br.alexandregpereira.jerry.expandable.goneCollapseWidth +import br.alexandregpereira.jerry.expandable.visibleExpandHeight +import br.alexandregpereira.jerry.expandable.visibleExpandWidth import kotlinx.android.synthetic.main.activity_expand_animation.* class ExpandSpringAnimationActivity : AppCompatActivity() { @@ -26,67 +26,67 @@ class ExpandSpringAnimationActivity : AppCompatActivity() { expandLabel.setText(R.string.expand_spring) expandTextButton.setOnClickListener { - expandTextView.expandHeightSpring() + expandTextView.visibleExpandHeight() } expandCollapseTextButton.setOnClickListener { - expandTextView.collapseHeightSpring() + expandTextView.goneCollapseHeight() } expandWidthTextButton.setOnClickListener { - expandWidthTextView.expandWidthSpring() + expandWidthTextView.visibleExpandWidth() } expandCollapseWidthTextButton.setOnClickListener { - expandWidthTextView.collapseWidthSpring() + expandWidthTextView.goneCollapseWidth() } expandFixedTextButton.setOnClickListener { - expandFixedTextView.expandHeightSpring() + expandFixedTextView.visibleExpandHeight() } expandCollapseFixedTextButton.setOnClickListener { - expandFixedTextView.collapseHeightSpring() + expandFixedTextView.goneCollapseHeight() } expandWidthTextButton.setOnClickListener { - expandWidthTextView.expandWidthSpring() + expandWidthTextView.visibleExpandWidth() } expandCollapseWidthTextButton.setOnClickListener { - expandWidthTextView.collapseWidthSpring() + expandWidthTextView.goneCollapseWidth() } expandMatchWidthButton.setOnClickListener { - expandMatchWidthView.expandWidthSpring() + expandMatchWidthView.visibleExpandWidth() } expandCollapseMatchWidthButton.setOnClickListener { - expandMatchWidthView.collapseWidthSpring() + expandMatchWidthView.goneCollapseWidth() } expand0dpWidthButton.setOnClickListener { - expand0dpWidthView.expandWidthSpring() + expand0dpWidthView.visibleExpandWidth() } expandCollapse0dpWidthButton.setOnClickListener { - expand0dpWidthView.collapseWidthSpring() + expand0dpWidthView.goneCollapseWidth() } expandHalf0dpWidthButton.setOnClickListener { - expandHalf0dpWidthView.expandWidthSpring() + expandHalf0dpWidthView.visibleExpandWidth() } expandCollapseHalf0dpWidthButton.setOnClickListener { - expandHalf0dpWidthView.collapseWidthSpring() + expandHalf0dpWidthView.goneCollapseWidth() } expandFixedWidthButton.setOnClickListener { - expandFixedWidthView.expandWidthSpring() + expandFixedWidthView.visibleExpandWidth() } expandCollapseFixedWidthButton.setOnClickListener { - expandFixedWidthView.collapseWidthSpring() + expandFixedWidthView.goneCollapseWidth() } } } diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt index 87124ea..105d4ec 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt @@ -5,9 +5,9 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.fadeInSpring -import br.alexandregpereira.jerry.fadeOutSpring +import br.alexandregpereira.jerry.goneFadeOut import br.alexandregpereira.jerry.textview.setTextFadeSpring +import br.alexandregpereira.jerry.visibleFadeIn import kotlinx.android.synthetic.main.activity_fade_animation.* class FadeSpringAnimationActivity : AppCompatActivity() { @@ -36,19 +36,19 @@ class FadeSpringAnimationActivity : AppCompatActivity() { } goneTextButton.setOnClickListener { - goneTextView.fadeOutSpring() + goneTextView.goneFadeOut() } goneVisibleTextButton.setOnClickListener { - goneTextView.fadeInSpring() + goneTextView.visibleFadeIn() } visibleTextButton.setOnClickListener { - visibleTextView.fadeInSpring() + visibleTextView.visibleFadeIn() } visibleInvisibleTextButton.setOnClickListener { - visibleTextView.fadeOutSpring() + visibleTextView.goneFadeOut() } } } diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/recyclerview/GridRecyclerViewSpringActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/recyclerview/GridRecyclerViewSpringActivity.kt index add51c5..cb895c9 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/recyclerview/GridRecyclerViewSpringActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/recyclerview/GridRecyclerViewSpringActivity.kt @@ -35,11 +35,11 @@ class GridRecyclerViewSpringActivity : AppCompatActivity() { recyclerView.layoutManager = GridLayoutManager(this, 4) recyclerView.adapter = adapter if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - recyclerView.itemAnimator = ElevationSpringItemAnimator( - elevationStiffness = 400f, - alphaStiffness = 380f, + recyclerView.itemAnimator = ElevationSpringItemAnimator().apply { + elevationStiffness = 400f + alphaStiffness = 380f translationStiffness = 120f - ) + } } button.setOnClickListener { diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt index a0790b2..76e070b 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt @@ -7,28 +7,40 @@ import androidx.dynamicanimation.animation.SpringForce @RequiresApi(21) fun JerryAnimationSet.elevationSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.ELEVATION.id, property = elevationViewProperty(), + targetValue = targetValue, + dampingRatio = dampingRatio, stiffness = stiffness ) @RequiresApi(21) fun JerryAnimation.elevationSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.ELEVATION.id, property = elevationViewProperty(), + targetValue = targetValue, + dampingRatio = dampingRatio, stiffness = stiffness ) @RequiresApi(21) fun View.elevationSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.ELEVATION.id, property = elevationViewProperty(), + targetValue = targetValue, + dampingRatio = dampingRatio, stiffness = stiffness ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt index 9f36b65..bab41a7 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt @@ -10,7 +10,10 @@ internal fun View.setFadeInFadeOutRunning(animationMode: AnimationMode) = fun View.isFadeInRunning() = isFadeInFadeOutRunning(AnimationMode.ENTER_ANIMATION_MODE) -fun View.isFadeOutRunning() = isFadeInFadeOutRunning(AnimationMode.POP_ANIMATION_MODE) +fun View.isFadeOutRunning() = isFadeInFadeOutRunning(AnimationMode.POP_ANIMATION_MODE) || + getSpringAnimation(SpringAnimationPropertyKey.ALPHA.id)?.run { + isRunning && spring?.finalPosition == 0f + } == true internal fun View.clearFadeInFadeOutRunning() = setFadeInFadeOutRunning(AnimationMode.NONE_ANIMATION_MODE) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt index 4d51c44..326d788 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt @@ -3,26 +3,28 @@ package br.alexandregpereira.jerry import android.view.View import androidx.dynamicanimation.animation.DynamicAnimation import androidx.dynamicanimation.animation.SpringAnimation +import androidx.dynamicanimation.animation.SpringForce /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the - * [fadeInSpring] is called, else the [fadeOutSpring] is called. + * [visibleFadeIn] is called, else the [goneFadeOut] is called. */ -fun View.animateAlphaVisibility( +fun View.visibleOrGoneFade( visible: Boolean, stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (visible) { - fadeInSpring(stiffness, onAnimationEnd = onAnimationEnd) + visibleFadeIn(stiffness, dampingRatio, onAnimationEnd = onAnimationEnd) } else { - fadeOutSpring(stiffness, onAnimationEnd = onAnimationEnd) + goneFadeOut(stiffness, dampingRatio, onAnimationEnd = onAnimationEnd) } } /** * Change the visibility to GONE of the view using fade out animation. This method can be - * reverted in the middle of the animation if the [fadeInSpring] method is called. + * reverted in the middle of the animation if the [visibleFadeIn] method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to * the object attached when the spring is not at the final position. Default stiffness is @@ -31,21 +33,23 @@ fun View.animateAlphaVisibility( * * @see [SpringAnimation] */ -fun View.fadeOutSpring( +fun View.goneFadeOut( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - fadeSpring(stiffness = stiffness).startFadeOutSpringAnimation( - onAnimationEnd = { canceled -> - gone() - onAnimationEnd?.invoke(canceled) - } - ) + fadeOutSpring( + stiffness = stiffness, + dampingRatio = dampingRatio + ).startSpringAnimation { canceled -> + gone() + onAnimationEnd?.invoke(canceled) + } } /** * Change the visibility to VISIBLE of the view using fade in animation. This method can be - * reverted in the middle of the animation if the [fadeOutSpring] + * reverted in the middle of the animation if the [goneFadeOut] * method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to @@ -55,69 +59,107 @@ fun View.fadeOutSpring( * * @see [SpringAnimation] */ -fun View.fadeInSpring( +fun View.visibleFadeIn( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (isVisible().not()) alpha = 0f visible() - fadeSpring(stiffness = stiffness).startFadeInSpringAnimation( + fadeInSpring( + stiffness = stiffness, + dampingRatio = dampingRatio + ).startSpringAnimation( onAnimationEnd = onAnimationEnd ) } -fun JerryAnimation.startFadeOutSpringAnimation( - onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = startFadeSpringAnimation( - targetValue = 0f, - onAnimationEnd = { canceled -> - onAnimationEnd?.invoke(canceled) - } +fun JerryAnimationSet.fadeInSpring( + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +) = fadeSpring( + targetValue = 1f, + stiffness = stiffness, + dampingRatio = dampingRatio ) -fun JerryAnimation.startFadeInSpringAnimation( - onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = startFadeSpringAnimation( +fun JerryAnimation.fadeInSpring( + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +) = fadeSpring( targetValue = 1f, - onAnimationEnd = onAnimationEnd + stiffness = stiffness, + dampingRatio = dampingRatio ) -fun JerryAnimation.targetFadeIn() = target(1f) +fun View.fadeInSpring( + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +) = fadeSpring( + targetValue = 1f, + stiffness = stiffness, + dampingRatio = dampingRatio +) -fun JerryAnimation.targetFadeOut() = target(0f) +fun JerryAnimationSet.fadeOutSpring( + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +) = fadeSpring( + targetValue = 0f, + dampingRatio = dampingRatio, + stiffness = stiffness +) -fun JerryAnimationSet.targetFadeIn() = target(1f) +fun JerryAnimation.fadeOutSpring( + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +) = fadeSpring( + targetValue = 0f, + stiffness = stiffness, + dampingRatio = dampingRatio +) -fun JerryAnimationSet.targetFadeOut() = target(0f) +fun View.fadeOutSpring( + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +) = fadeSpring( + targetValue = 0f, + stiffness = stiffness, + dampingRatio = dampingRatio +) fun JerryAnimationSet.fadeSpring( - stiffness: Float = ANIMATION_STIFFNESS + targetValue: Float, + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.ALPHA.id, property = DynamicAnimation.ALPHA, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio ) fun JerryAnimation.fadeSpring( - stiffness: Float = ANIMATION_STIFFNESS + targetValue: Float, + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.ALPHA.id, property = DynamicAnimation.ALPHA, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio ) fun View.fadeSpring( - stiffness: Float = ANIMATION_STIFFNESS + targetValue: Float, + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.ALPHA.id, property = DynamicAnimation.ALPHA, - stiffness = stiffness -) - -fun JerryAnimation.startFadeSpringAnimation( - targetValue: Float, - onAnimationEnd: ((canceled: Boolean) -> Unit)? = null, -) = startSpringAnimation( targetValue = targetValue, - onAnimationEnd = onAnimationEnd + stiffness = stiffness, + dampingRatio = dampingRatio ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt index de81ede..c1cdc0f 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt @@ -7,7 +7,7 @@ data class JerryAnimation( val key: Int, val view: View, val springAnimation: SpringAnimation, - internal val targetValue: Float? = null, + internal val targetValue: Float, internal val jerryAnimations: List? = null ) { val isRunning: Boolean diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt index 378d630..f4c593a 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt @@ -28,81 +28,66 @@ fun View.skipToEndSpringAnimation(key: Int) { getSpringAnimation(key)?.takeIf { it.canSkipToEnd() }?.skipToEnd() } -fun View.clearSpring(key: Int) { - setTag(key, null) -} - /** * */ fun View.spring( key: Int, property: FloatPropertyCompat, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimation { var springAnimation = getSpringAnimation(key) if (springAnimation == null) { springAnimation = SpringAnimation(this, property).apply { - spring = SpringForce().apply { - this.dampingRatio = dampingRatio - this.stiffness = stiffness - } + spring = SpringForce() } addSpringAnimationKeyIfNotContains(key) setTag(key, SpringAnimationHolder(springAnimation)) } - return JerryAnimation(key = key, view = this, springAnimation) + springAnimation.spring.apply { + this.dampingRatio = dampingRatio + this.stiffness = stiffness + } + return JerryAnimation(key = key, view = this, springAnimation, targetValue) } fun JerryAnimation.spring( key: Int, property: FloatPropertyCompat, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { - return animationSet().spring(key, property, dampingRatio, stiffness) + return animationSet().spring( + key = key, + property = property, + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio + ) } fun JerryAnimationSet.spring( key: Int, property: FloatPropertyCompat, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { return this.copy( jerryAnimations = jerryAnimations + - listOf(jerryAnimations.last().view.spring(key, property, dampingRatio, stiffness)) - ) -} - -fun JerryAnimation.startSpringAnimation( - targetValue: Float, - onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) { - - this.addSpringEndListener( - onAnimationEnd = onAnimationEnd + listOf(jerryAnimations.last().view.spring( + key, property, targetValue, stiffness, dampingRatio + )) ) - - springAnimation.animateToFinalPosition(targetValue) } private fun JerryAnimation.animationSet(): JerryAnimationSet { return JerryAnimationSet(jerryAnimations = listOf(this)) } -fun JerryAnimationSet.target(targetValue: Float): JerryAnimationSet { - return this.copy( - jerryAnimations = jerryAnimations.subList(0, jerryAnimations.size - 1) + - jerryAnimations.last().target(targetValue) - ) -} - -fun JerryAnimation.target(targetValue: Float): JerryAnimation { - return this.copy(targetValue = targetValue) -} - fun JerryAnimation.after(jerryAnimationSet: JerryAnimationSet): JerryAnimationSet { return animationSet().after(jerryAnimationSet) } @@ -131,12 +116,9 @@ fun JerryAnimationSet.startSpringAnimation( } } } - return jerryAnimations.forEachIndexed { index, animation -> - val targetValue = animation.targetValue ?: throw IllegalStateException( - "The animation target from index $index must be set before execution of a list of animations" - ) + return jerryAnimations.forEach { animation -> val otherAnimations = jerryAnimations.filterNot { it.key == animation.key } - animation.startSpringAnimation(targetValue) { canceled -> + animation.startSpringAnimation { canceled -> onAnimationsEnd( canceled, otherAnimations.isRunning().not() @@ -145,6 +127,16 @@ fun JerryAnimationSet.startSpringAnimation( } } +fun JerryAnimation.startSpringAnimation( + onAnimationEnd: ((canceled: Boolean) -> Unit)? = null +) { + this.addSpringEndListener( + onAnimationEnd = onAnimationEnd + ) + + springAnimation.animateToFinalPosition(targetValue) +} + fun List.isRunning(): Boolean { return isNotEmpty() && map { it.isRunning }.reduce { acc, isRunning -> acc || isRunning } } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt index bf265d3..b62b5db 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt @@ -5,49 +5,73 @@ import androidx.dynamicanimation.animation.DynamicAnimation import androidx.dynamicanimation.animation.SpringForce fun JerryAnimation.translationXSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_X.id, property = DynamicAnimation.TRANSLATION_X, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio, ) fun JerryAnimationSet.translationXSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_X.id, property = DynamicAnimation.TRANSLATION_X, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio, ) fun View.translationXSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_X.id, property = DynamicAnimation.TRANSLATION_X, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio, ) fun JerryAnimation.translationYSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_Y.id, property = DynamicAnimation.TRANSLATION_Y, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio, ) fun JerryAnimationSet.translationYSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_Y.id, property = DynamicAnimation.TRANSLATION_Y, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio, ) fun View.translationYSpring( - stiffness: Float = SpringForce.STIFFNESS_LOW + targetValue: Float, + stiffness: Float = SpringForce.STIFFNESS_LOW, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_Y.id, property = DynamicAnimation.TRANSLATION_Y, - stiffness = stiffness + targetValue = targetValue, + stiffness = stiffness, + dampingRatio = dampingRatio, ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt index fdc3f42..f13aeb1 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt @@ -2,27 +2,33 @@ package br.alexandregpereira.jerry.animator import android.view.View import androidx.annotation.RequiresApi +import androidx.dynamicanimation.animation.SpringForce.DAMPING_RATIO_NO_BOUNCY import androidx.recyclerview.widget.RecyclerView import br.alexandregpereira.jerry.ANIMATION_STIFFNESS import br.alexandregpereira.jerry.after import br.alexandregpereira.jerry.dpToPx import br.alexandregpereira.jerry.elevationSpring +import br.alexandregpereira.jerry.fadeInSpring +import br.alexandregpereira.jerry.fadeOutSpring import br.alexandregpereira.jerry.fadeSpring import br.alexandregpereira.jerry.startSpringAnimation -import br.alexandregpereira.jerry.target -import br.alexandregpereira.jerry.targetFadeIn -import br.alexandregpereira.jerry.targetFadeOut import br.alexandregpereira.jerry.translationXSpring import br.alexandregpereira.jerry.translationYSpring @RequiresApi(21) class ElevationSpringItemAnimator( private val elevation: Float? = null, - private val elevationStiffness: Float = ANIMATION_STIFFNESS * 2.5f, - private val alphaStiffness: Float = ANIMATION_STIFFNESS * 2f, - private val translationStiffness: Float = ANIMATION_STIFFNESS * 1.2f + stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = DAMPING_RATIO_NO_BOUNCY ) : BaseItemAnimator() { + var elevationStiffness: Float = stiffness * 2.5f + var alphaStiffness: Float = stiffness * 2f + var translationStiffness: Float = stiffness * 1.2f + var elevationDampingRatio: Float = dampingRatio + var alphaDampingRatio: Float = dampingRatio + var translationDampingRatio: Float = dampingRatio + private val translationOrigin = 0f private val elevationNone = 0f private val alphaNone = 0f @@ -63,19 +69,23 @@ class ElevationSpringItemAnimator( val translationXTargetValue = if (deltaX != 0) translationOrigin else translationX val translationYTargetValue = if (deltaY != 0) translationOrigin else translationY - translationXSpring(stiffness = translationStiffness) - .target(translationXTargetValue) - .translationYSpring(stiffness = translationStiffness) - .target(translationYTargetValue) - .startSpringAnimation { canceled -> - if (canceled) { - if (deltaX != 0) translationX = translationOrigin - if (deltaY != 0) translationY = translationOrigin - } - alpha = alphaFull - elevation = elevationFull - onAnimateMoveFinished(holder) + translationXSpring( + translationXTargetValue, + translationStiffness, + translationDampingRatio + ).translationYSpring( + translationYTargetValue, + translationStiffness, + translationDampingRatio + ).startSpringAnimation { canceled -> + if (canceled) { + if (deltaX != 0) translationX = translationOrigin + if (deltaY != 0) translationY = translationOrigin } + alpha = alphaFull + elevation = elevationFull + onAnimateMoveFinished(holder) + } } return true } @@ -116,17 +126,25 @@ class ElevationSpringItemAnimator( ) { val elevationFull = itemView.elevationFull this.itemView.apply { - elevationSpring(stiffness = elevationStiffness) - .target(elevationNone) + elevationSpring(targetValue = elevationNone, elevationStiffness, elevationDampingRatio) .after( - fadeSpring(stiffness = alphaStiffness) - .target(alphaTargetValue) - .translationXSpring(stiffness = translationStiffness) - .target(translationXTargetValue) - .translationYSpring(stiffness = translationStiffness) - .target(translationYTargetValue) + fadeSpring(alphaTargetValue, alphaStiffness, alphaDampingRatio) + .translationXSpring( + translationXTargetValue, + translationStiffness, + translationDampingRatio + ) + .translationYSpring( + translationYTargetValue, + translationStiffness, + translationDampingRatio + ) .after( - elevationSpring(stiffness = elevationStiffness).target(elevationFull) + elevationSpring( + targetValue = elevationFull, + elevationStiffness, + elevationDampingRatio + ) ) ).startSpringAnimation { itemView.alpha = alphaFull @@ -139,10 +157,13 @@ class ElevationSpringItemAnimator( } private fun View.startFadeElevationInAnimation(onAnimationFinished: () -> Unit) { - fadeSpring(stiffness = alphaStiffness) - .targetFadeIn() + fadeInSpring(stiffness = alphaStiffness, alphaDampingRatio) .after( - elevationSpring(stiffness = elevationStiffness).target(elevationFull) + elevationSpring( + targetValue = elevationFull, + stiffness = elevationStiffness, + dampingRatio = elevationDampingRatio + ) ) .startSpringAnimation { onAnimationFinished() @@ -150,10 +171,9 @@ class ElevationSpringItemAnimator( } private fun View.startFadeElevationOutAnimation(onAnimationFinished: () -> Unit) { - elevationSpring(stiffness = elevationStiffness) - .target(elevationNone) + elevationSpring(targetValue = elevationNone, elevationStiffness, elevationDampingRatio) .after( - fadeSpring(stiffness = alphaStiffness).targetFadeOut() + fadeOutSpring(stiffness = alphaStiffness, alphaDampingRatio) ) .startSpringAnimation { alpha = alphaFull diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt index a3fc498..3d5c6a3 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt @@ -2,49 +2,53 @@ package br.alexandregpereira.jerry.expandable import android.view.View import androidx.dynamicanimation.animation.SpringAnimation +import androidx.dynamicanimation.animation.SpringForce import br.alexandregpereira.jerry.ANIMATION_STIFFNESS -import br.alexandregpereira.jerry.fadeInSpring import br.alexandregpereira.jerry.fadeOutSpring +import br.alexandregpereira.jerry.visibleFadeIn +import br.alexandregpereira.jerry.goneFadeOut import br.alexandregpereira.jerry.fadeSpring import br.alexandregpereira.jerry.isVisible -import br.alexandregpereira.jerry.startFadeOutSpringAnimation +import br.alexandregpereira.jerry.startSpringAnimation /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the - * [expandHeightFadingSpring] is called, else the [collapseHeightFadingSpring] is called. + * [visibleExpandHeightFadeIn] is called, else the [goneCollapseHeightFadeOut] is called. */ -fun View.animateHeightFadingVisibility( +fun View.visibleOrGoneExpandableHeightFade( visible: Boolean, stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (visible) { - expandHeightFadingSpring(stiffness, onAnimationEnd) + visibleExpandHeightFadeIn(stiffness, dampingRatio, onAnimationEnd) } else { - collapseHeightFadingSpring(stiffness, onAnimationEnd) + goneCollapseHeightFadeOut(stiffness, dampingRatio, onAnimationEnd) } } /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the - * [expandWidthFadingSpring] is called, else the [collapseWidthFadingSpring] is called. + * [visibleExpandWidthFadeIn] is called, else the [goneCollapseWidthFadeOut] is called. */ -fun View.animateWidthFadingVisibility( +fun View.visibleOrGoneExpandableWidthFade( visible: Boolean, stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (visible) { - expandWidthFadingSpring(stiffness, onAnimationEnd) + visibleExpandWidthFadeIn(stiffness, dampingRatio, onAnimationEnd) } else { - collapseWidthFadingSpring(stiffness, onAnimationEnd) + goneCollapseWidthFadeOut(stiffness, dampingRatio, onAnimationEnd) } } /** - * Uses the [expandHeightSpring] and [fadeInSpring] animations in sequence. This animation + * Uses the [visibleExpandHeight] and [visibleFadeIn] animations in sequence. This animation * handles double click. This method can be reverted in the middle of the animation if the - * [collapseHeightFadingSpring] method is called. + * [goneCollapseHeightFadeOut] method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to * the object attached when the spring is not at the final position. Default stiffness is @@ -53,15 +57,16 @@ fun View.animateWidthFadingVisibility( * * @see [SpringAnimation] */ -fun View.expandHeightFadingSpring( +fun View.visibleExpandHeightFadeIn( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = expandFadingSpring(stiffness, isHeight = true, onAnimationEnd = onAnimationEnd) +) = visibleExpandFadeIn(stiffness, dampingRatio, isHeight = true, onAnimationEnd = onAnimationEnd) /** - * Uses the [expandWidthSpring] and [fadeInSpring] animations in sequence. This animation + * Uses the [visibleExpandWidth] and [visibleFadeIn] animations in sequence. This animation * handles double click. This method can be reverted in the middle of the animation if the - * [collapseWidthFadingSpring] method is called. + * [goneCollapseWidthFadeOut] method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to * the object attached when the spring is not at the final position. Default stiffness is @@ -70,15 +75,16 @@ fun View.expandHeightFadingSpring( * * @see [SpringAnimation] */ -fun View.expandWidthFadingSpring( +fun View.visibleExpandWidthFadeIn( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = expandFadingSpring(stiffness, isHeight = false, onAnimationEnd = onAnimationEnd) +) = visibleExpandFadeIn(stiffness, dampingRatio, isHeight = false, onAnimationEnd = onAnimationEnd) /** - * Uses the [fadeOutSpring] and [collapseHeightSpring] animations in sequence. This animation + * Uses the [goneFadeOut] and [goneCollapseHeight] animations in sequence. This animation * handles double click. This method can be reverted in the middle of the animation if the - * [expandHeightFadingSpring] method is called. + * [visibleExpandHeightFadeIn] method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to * the object attached when the spring is not at the final position. Default stiffness is @@ -87,15 +93,16 @@ fun View.expandWidthFadingSpring( * * @see [SpringAnimation] */ -fun View.collapseHeightFadingSpring( +fun View.goneCollapseHeightFadeOut( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = collapseFadingSpring(stiffness, isHeight = true, onAnimationEnd = onAnimationEnd) +) = goneCollapseFadeOut(stiffness, dampingRatio, isHeight = true, onAnimationEnd = onAnimationEnd) /** - * Uses the [fadeSpring] and [collapseWidthSpring] animations in sequence. This animation + * Uses the [fadeSpring] and [goneCollapseWidth] animations in sequence. This animation * handles double click. This method can be reverted in the middle of the animation if the - * [expandWidthFadingSpring] method is called. + * [visibleExpandWidthFadeIn] method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to * the object attached when the spring is not at the final position. Default stiffness is @@ -104,18 +111,20 @@ fun View.collapseHeightFadingSpring( * * @see [SpringAnimation] */ -fun View.collapseWidthFadingSpring( +fun View.goneCollapseWidthFadeOut( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = collapseFadingSpring(stiffness, isHeight = false, onAnimationEnd = onAnimationEnd) +) = goneCollapseFadeOut(stiffness, dampingRatio, isHeight = false, onAnimationEnd = onAnimationEnd) -private fun View.collapseFadingSpring( +private fun View.goneCollapseFadeOut( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, isHeight: Boolean = true, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - fadeSpring(stiffness = stiffness * 2f).startFadeOutSpringAnimation { - collapseSpring( + fadeOutSpring(stiffness = stiffness * 2f, dampingRatio).startSpringAnimation { + goneCollapse( stiffness = stiffness * 2f, isHeight = isHeight, onAnimationEnd = onAnimationEnd @@ -123,13 +132,14 @@ private fun View.collapseFadingSpring( } } -private fun View.expandFadingSpring( +private fun View.visibleExpandFadeIn( stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, isHeight: Boolean = true, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (isVisible().not()) alpha = 0f - expandSpring(stiffness = stiffness * 2f, isHeight = isHeight) { - fadeInSpring(stiffness = stiffness * 2f, onAnimationEnd = onAnimationEnd) + visibleExpand(stiffness = stiffness * 2f, isHeight = isHeight) { + visibleFadeIn(stiffness = stiffness * 2f, dampingRatio, onAnimationEnd) } } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt index 48acf1a..cb0b3dd 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt @@ -11,42 +11,42 @@ import br.alexandregpereira.jerry.visible /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the - * [expandHeightSpring] is called, else the [collapseHeightSpring] is called. + * [visibleExpandHeight] is called, else the [goneCollapseHeight] is called. */ -fun View.animateHeightVisibility( +fun View.visibleOrGoneExpandableHeight( visible: Boolean, stiffness: Float = ANIMATION_STIFFNESS, onProgressChange: ((progress: Float) -> Unit)? = null, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (visible) { - expandHeightSpring(stiffness, onProgressChange, onAnimationEnd) + visibleExpandHeight(stiffness, onProgressChange, onAnimationEnd) } else { - collapseHeightSpring(stiffness, onProgressChange, onAnimationEnd) + goneCollapseHeight(stiffness, onProgressChange, onAnimationEnd) } } /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the - * [expandWidthSpring] is called, else the [collapseWidthSpring] is called. + * [visibleExpandWidth] is called, else the [goneCollapseWidth] is called. */ -fun View.animateWidthVisibility( +fun View.visibleOrGoneExpandableWidth( visible: Boolean, stiffness: Float = ANIMATION_STIFFNESS, onProgressChange: ((progress: Float) -> Unit)? = null, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (visible) { - expandWidthSpring(stiffness, onProgressChange, onAnimationEnd) + visibleExpandWidth(stiffness, onProgressChange, onAnimationEnd) } else { - collapseWidthSpring(stiffness, onProgressChange, onAnimationEnd) + goneCollapseWidth(stiffness, onProgressChange, onAnimationEnd) } } /** * Animates collapsing the height and changes the visibility status to GONE. * This animation handles double click. This method can be reverted in the middle of the animation - * if the [expandHeightSpring] method is called. + * if the [visibleExpandHeight] method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to * the object attached when the spring is not at the final position. Default stiffness is @@ -55,11 +55,11 @@ fun View.animateWidthVisibility( * * @see [SpringAnimation] */ -fun View.collapseHeightSpring( +fun View.goneCollapseHeight( stiffness: Float = ANIMATION_STIFFNESS, onProgressChange: ((progress: Float) -> Unit)? = null, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = collapseSpring( +) = goneCollapse( stiffness = stiffness, isHeight = true, onProgressChange = onProgressChange, @@ -69,7 +69,7 @@ fun View.collapseHeightSpring( /** * Animates collapsing the width and changes the visibility status to GONE. * This animation handles double click. This method can be reverted in the middle of the animation - * if the [expandWidthSpring] method is called. + * if the [visibleExpandWidth] method is called. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to * the object attached when the spring is not at the final position. Default stiffness is @@ -78,11 +78,11 @@ fun View.collapseHeightSpring( * * @see [SpringAnimation] */ -fun View.collapseWidthSpring( +fun View.goneCollapseWidth( stiffness: Float = ANIMATION_STIFFNESS, onProgressChange: ((progress: Float) -> Unit)? = null, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = collapseSpring( +) = goneCollapse( stiffness = stiffness, isHeight = false, onProgressChange = onProgressChange, @@ -92,7 +92,7 @@ fun View.collapseWidthSpring( /** * Animates expanding the height and changes the visibility status to VISIBLE. * This animation handles double click. This method can be reverted in the middle of the animation - * if the [collapseHeightSpring] method is called. Any alteration of the parent width during the this + * if the [goneCollapseHeight] method is called. Any alteration of the parent width during the this * animation makes glitches in the animation. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to @@ -102,11 +102,11 @@ fun View.collapseWidthSpring( * * @see [SpringAnimation] */ -fun View.expandHeightSpring( +fun View.visibleExpandHeight( stiffness: Float = ANIMATION_STIFFNESS, onProgressChange: ((progress: Float) -> Unit)? = null, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = expandSpring( +) = visibleExpand( stiffness = stiffness, isHeight = true, onProgressChange = onProgressChange, @@ -116,7 +116,7 @@ fun View.expandHeightSpring( /** * Animates expanding the width and changes the visibility status to VISIBLE. * This animation handles double click. This method can be reverted in the middle of the animation - * if the [collapseWidthSpring] method is called. Any alteration of the parent width during the this + * if the [goneCollapseWidth] method is called. Any alteration of the parent width during the this * animation makes glitches in the animation. * * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to @@ -126,18 +126,18 @@ fun View.expandHeightSpring( * * @see [SpringAnimation] */ -fun View.expandWidthSpring( +fun View.visibleExpandWidth( stiffness: Float = ANIMATION_STIFFNESS, onProgressChange: ((progress: Float) -> Unit)? = null, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null -) = expandSpring( +) = visibleExpand( stiffness = stiffness, isHeight = false, onProgressChange = onProgressChange, onAnimationEnd = onAnimationEnd ) -internal fun View.collapseSpring( +internal fun View.goneCollapse( stiffness: Float = ANIMATION_STIFFNESS, isHeight: Boolean = true, onProgressChange: ((progress: Float) -> Unit)? = null, @@ -152,20 +152,18 @@ internal fun View.collapseSpring( startCollapsingRunning() getOrStoreWidthOrHeightOriginalValue(isHeight) expandCollapseSpring( + targetValue = 0f, stiffness = stiffness, isHeight = isHeight, onProgressChange = onProgressChange, - ).startSpringAnimation( - targetValue = 0f, - onAnimationEnd = { canceled -> - gone() - setLayoutParamSize(getOrStoreWidthOrHeightOriginalValue(isHeight), isHeight) - finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) - } - ) + ).startSpringAnimation { canceled -> + gone() + setLayoutParamSize(getOrStoreWidthOrHeightOriginalValue(isHeight), isHeight) + finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) + } } -internal fun View.expandSpring( +internal fun View.visibleExpand( stiffness: Float = ANIMATION_STIFFNESS, isHeight: Boolean = true, onProgressChange: ((progress: Float) -> Unit)? = null, @@ -200,23 +198,23 @@ internal fun View.expandSpring( visible() expandCollapseSpring( + targetValue = targetValue.toFloat(), stiffness = stiffness, isHeight = isHeight, onProgressChange = onProgressChange - ).startSpringAnimation( - targetValue = targetValue.toFloat(), - onAnimationEnd = { canceled -> - finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) - } - ) + ).startSpringAnimation { canceled -> + finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) + } } private fun View.expandCollapseSpring( + targetValue: Float, stiffness: Float, isHeight: Boolean, onProgressChange: ((progress: Float) -> Unit)?, ) = spring( key = getExpandingCollapsingSpringKey(isHeight), property = widthHeightViewProperty(isHeight, onProgressChange), + targetValue = targetValue, stiffness = stiffness ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt index 18a0340..404c88c 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt @@ -2,20 +2,22 @@ package br.alexandregpereira.jerry.textview import android.widget.TextView import androidx.dynamicanimation.animation.SpringAnimation +import androidx.dynamicanimation.animation.SpringForce import br.alexandregpereira.jerry.ANIMATION_STIFFNESS -import br.alexandregpereira.jerry.expandable.collapseHeightFadingSpring -import br.alexandregpereira.jerry.expandable.expandHeightFadingSpring +import br.alexandregpereira.jerry.expandable.goneCollapseHeightFadeOut import br.alexandregpereira.jerry.expandable.isCollapsingRunning -import br.alexandregpereira.jerry.fadeSpring +import br.alexandregpereira.jerry.expandable.visibleExpandHeightFadeIn +import br.alexandregpereira.jerry.fadeInSpring +import br.alexandregpereira.jerry.fadeOutSpring import br.alexandregpereira.jerry.isFadeOutRunning import br.alexandregpereira.jerry.isVisible -import br.alexandregpereira.jerry.startFadeSpringAnimation +import br.alexandregpereira.jerry.startSpringAnimation /** - * Uses the [setTextFadeSpring], [expandHeightFadingSpring] or [collapseHeightFadingSpring] + * Uses the [setTextFadeSpring], [visibleExpandHeightFadeIn] or [goneCollapseHeightFadeOut] * animation methods depending of the TextView state. If the new text is null or empty, - * the [collapseHeightFadingSpring] is used, else if the TextView is already visible, - * the [setTextFadeSpring] is used, else the [expandHeightFadingSpring] is used. + * the [goneCollapseHeightFadeOut] is used, else if the TextView is already visible, + * the [setTextFadeSpring] is used, else the [visibleExpandHeightFadeIn] is used. * * @param text The new text of the TextView * @param stiffness Stiffness of a spring. The more stiff a spring is, the more force it applies to @@ -27,14 +29,15 @@ import br.alexandregpereira.jerry.startFadeSpringAnimation fun TextView.setTextExpandableSpring( text: String?, stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { if (text == null || text.trim().isEmpty()) { - collapseHeightFadingSpring(stiffness = stiffness, onAnimationEnd = onAnimationEnd) + goneCollapseHeightFadeOut(stiffness, dampingRatio, onAnimationEnd) return } - if (isVisible() && isCollapsingRunning().not()) { + if (isVisible() && isCollapsingRunning().not() && isFadeOutRunning().not()) { setTextFadeSpring( text, stiffness = stiffness, @@ -43,7 +46,7 @@ fun TextView.setTextExpandableSpring( return } setText(text) - expandHeightFadingSpring(stiffness = stiffness, onAnimationEnd = onAnimationEnd) + visibleExpandHeightFadeIn(stiffness, dampingRatio, onAnimationEnd) } /** @@ -59,6 +62,7 @@ fun TextView.setTextExpandableSpring( fun TextView.setTextFadeSpring( text: String = "", stiffness: Float = ANIMATION_STIFFNESS, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { val textView = this @@ -69,20 +73,16 @@ fun TextView.setTextFadeSpring( if (oldText.isEmpty()) { textView.text = text textView.alpha = 0f - fadeSpring(stiffness = stiffness).startFadeSpringAnimation( - targetValue = 1f, + fadeInSpring(stiffness = stiffness, dampingRatio).startSpringAnimation( onAnimationEnd = onAnimationEnd ) return } - fadeSpring(stiffness = stiffness * 1.5f) - .startFadeSpringAnimation( - targetValue = 0f - ) { + fadeOutSpring(stiffness = stiffness * 1.5f, dampingRatio) + .startSpringAnimation { textView.text = text - fadeSpring(stiffness = stiffness * 1.5f).startFadeSpringAnimation( - targetValue = 1f, + fadeInSpring(stiffness = stiffness * 1.5f, dampingRatio).startSpringAnimation( onAnimationEnd = onAnimationEnd ) } From 02eeaad80aa9c84604768f1d08df64e96ef6d431 Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Mon, 19 Oct 2020 23:41:12 -0300 Subject: [PATCH 04/14] Remove not spring animations and change functions names --- app/src/main/AndroidManifest.xml | 12 - .../jerry/app/MainActivity.kt | 37 +--- .../animation/CollapseAnimationActivity.kt | 97 -------- .../CollapseFadingAnimationActivity.kt | 66 ------ .../app/animation/ExpandAnimationActivity.kt | 90 -------- .../ExpandFadingAnimationActivity.kt | 74 ------- .../app/animation/FadeAnimationActivity.kt | 52 ----- .../TextExpandableAnimationActivity.kt | 74 ------- .../alexandregpereira/jerry/FadeAnimation.kt | 99 --------- .../jerry/FadeAnimationCommon.kt | 17 +- .../jerry/FadeSpringAnimation.kt | 4 +- .../jerry/SpringAnimation.kt | 14 +- .../animator/ElevationSpringItemAnimator.kt | 10 +- .../jerry/expandable/ExpandableAnimation.kt | 207 ------------------ .../expandable/ExpandableAnimationCommon.kt | 13 -- .../expandable/ExpandableFadingAnimation.kt | 94 -------- .../ExpandableFadingSpringAnimation.kt | 4 +- .../expandable/ExpandableSpingAnimation.kt | 6 +- .../{ => expandable}/OriginalValueKey.kt | 5 +- .../jerry/{ => expandable}/ViewAnimation.kt | 11 +- .../jerry/textview/TextViewAnimation.kt | 86 -------- .../jerry/textview/TextViewSpringAnimation.kt | 8 +- 22 files changed, 30 insertions(+), 1050 deletions(-) delete mode 100644 app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt delete mode 100644 app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingAnimationActivity.kt delete mode 100644 app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandAnimationActivity.kt delete mode 100644 app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingAnimationActivity.kt delete mode 100644 app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeAnimationActivity.kt delete mode 100644 app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableAnimationActivity.kt delete mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimation.kt delete mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimation.kt delete mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingAnimation.kt rename jerry/src/main/java/br/alexandregpereira/jerry/{ => expandable}/OriginalValueKey.kt (54%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => expandable}/ViewAnimation.kt (84%) delete mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewAnimation.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7e597db..3bb21c3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,28 +19,16 @@ - - - - - - - - - - - - diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt index 0f9afea..78b29cd 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt @@ -7,17 +7,11 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -import br.alexandregpereira.jerry.app.animation.CollapseAnimationActivity -import br.alexandregpereira.jerry.app.animation.CollapseFadingAnimationActivity -import br.alexandregpereira.jerry.app.animation.CollapseSpringAnimationActivity import br.alexandregpereira.jerry.app.animation.CollapseFadingSpringAnimationActivity -import br.alexandregpereira.jerry.app.animation.ExpandAnimationActivity -import br.alexandregpereira.jerry.app.animation.ExpandFadingAnimationActivity -import br.alexandregpereira.jerry.app.animation.ExpandSpringAnimationActivity +import br.alexandregpereira.jerry.app.animation.CollapseSpringAnimationActivity import br.alexandregpereira.jerry.app.animation.ExpandFadingSpringAnimationActivity -import br.alexandregpereira.jerry.app.animation.FadeAnimationActivity +import br.alexandregpereira.jerry.app.animation.ExpandSpringAnimationActivity import br.alexandregpereira.jerry.app.animation.FadeSpringAnimationActivity -import br.alexandregpereira.jerry.app.animation.TextExpandableAnimationActivity import br.alexandregpereira.jerry.app.animation.TextExpandableSpringAnimationActivity import br.alexandregpereira.jerry.app.recyclerview.GridRecyclerViewActivity import br.alexandregpereira.jerry.app.recyclerview.GridRecyclerViewSpringActivity @@ -36,42 +30,21 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { layoutManager = LinearLayoutManager(this@MainActivity) adapter = MainAdapter(getAnimationComponentsName()) { when (it) { - AnimationComponent.FADE.ordinal -> startActivity( - FadeAnimationActivity.getStartIntent(this@MainActivity) - ) AnimationComponent.FADE_SPRING.ordinal -> startActivity( FadeSpringAnimationActivity.getStartIntent(this@MainActivity) ) - AnimationComponent.COLLAPSE_FADING.ordinal -> startActivity( - CollapseFadingAnimationActivity.getStartIntent(this@MainActivity) - ) - AnimationComponent.COLLAPSE_FADING.ordinal -> startActivity( - CollapseFadingAnimationActivity.getStartIntent(this@MainActivity) - ) AnimationComponent.COLLAPSE_FADING_SPRING.ordinal -> startActivity( CollapseFadingSpringAnimationActivity.getStartIntent(this@MainActivity) ) - AnimationComponent.EXPAND_FADING.ordinal -> startActivity( - ExpandFadingAnimationActivity.getStartIntent(this@MainActivity) - ) AnimationComponent.EXPAND_FADING_SPRING.ordinal -> startActivity( ExpandFadingSpringAnimationActivity.getStartIntent(this@MainActivity) ) - AnimationComponent.COLLAPSE.ordinal -> startActivity( - CollapseAnimationActivity.getStartIntent(this@MainActivity) - ) AnimationComponent.COLLAPSE_SPRING.ordinal -> startActivity( CollapseSpringAnimationActivity.getStartIntent(this@MainActivity) ) - AnimationComponent.EXPAND.ordinal -> startActivity( - ExpandAnimationActivity.getStartIntent(this@MainActivity) - ) AnimationComponent.EXPAND_SPRING.ordinal -> startActivity( ExpandSpringAnimationActivity.getStartIntent(this@MainActivity) ) - AnimationComponent.TEXT_EXPANDABLE.ordinal -> startActivity( - TextExpandableAnimationActivity.getStartIntent(this@MainActivity) - ) AnimationComponent.TEXT_EXPANDABLE_SPRING.ordinal -> startActivity( TextExpandableSpringAnimationActivity.getStartIntent(this@MainActivity) ) @@ -98,17 +71,11 @@ fun getAnimationComponentsName(): List { } enum class AnimationComponent { - COLLAPSE, COLLAPSE_SPRING, - COLLAPSE_FADING, COLLAPSE_FADING_SPRING, - EXPAND, EXPAND_SPRING, - EXPAND_FADING, EXPAND_FADING_SPRING, - FADE, FADE_SPRING, - TEXT_EXPANDABLE, TEXT_EXPANDABLE_SPRING, RECYCLER, RECYCLER_SPRING, diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt deleted file mode 100644 index 75ea15d..0000000 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseAnimationActivity.kt +++ /dev/null @@ -1,97 +0,0 @@ -package br.alexandregpereira.jerry.app.animation - -import android.content.Context -import android.content.Intent -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import androidx.core.view.ViewCompat -import br.alexandregpereira.jerry.expandable.visibleOrGoneExpandableWidth -import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeight -import br.alexandregpereira.jerry.expandable.collapseWidth -import br.alexandregpereira.jerry.expandable.expandHeight -import br.alexandregpereira.jerry.expandable.expandWidth -import kotlinx.android.synthetic.main.activity_collapse_animation.* -import kotlinx.android.synthetic.main.container_animation_info.view.* - -class CollapseAnimationActivity : AppCompatActivity(R.layout.activity_collapse_animation) { - - private var collapseTextViewCount = 1 - private var collapseMatchWidthViewVisible = true - - companion object { - fun getStartIntent(context: Context): Intent { - return Intent(context, CollapseAnimationActivity::class.java) - } - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - ViewCompat.setTranslationZ( - collapseAnimationInfo, - resources.getDimension(R.dimen.strong_elevation) - ) - - collapseTextView.expandHeight() - collapseTextButton.setOnClickListener { - collapseTextView.collapseHeight( - onProgressChange = { interpolatedTime -> - collapseAnimationInfo.percentageTextView.text = (interpolatedTime * 100).toInt().toString() - } - ) { - collapseAnimationInfo.countTextView.text = collapseTextViewCount++.toString() - } - } - - collapseExpandTextButton.setOnClickListener { - collapseTextView.expandHeight( - onProgressChange = { interpolatedTime -> - collapseAnimationInfo.percentageTextView.text = (interpolatedTime * 100).toInt().toString() - } - ) { - collapseAnimationInfo.countTextView.text = collapseTextViewCount++.toString() - } - } - - collapseFixedTextButton.setOnClickListener { - collapseFixedTextView.collapseHeight() - } - - collapseExpandFixedTextButton.setOnClickListener { - collapseFixedTextView.expandHeight() - } - - collapseWidthTextButton.setOnClickListener { - collapseWidthTextView.collapseWidth() - } - - collapseExpandWidthTextButton.setOnClickListener { - collapseWidthTextView.expandWidth() - } - - collapseReverseMatchWidthButton.setOnClickListener { - collapseMatchWidthView.visibleOrGoneExpandableWidth( - visible = collapseMatchWidthViewVisible.not().also { - collapseMatchWidthViewVisible = it - } - ) - } - - collapseMatchWidthButton.setOnClickListener { - collapseMatchWidthView.collapseWidth() - } - - collapseExpandMatchWidthButton.setOnClickListener { - collapseMatchWidthView.expandWidth() - } - - collapseFixedWidthButton.setOnClickListener { - collapseFixedWidthView.collapseWidth() - } - - collapseExpandFixedWidthButton.setOnClickListener { - collapseFixedWidthView.expandWidth() - } - } -} diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingAnimationActivity.kt deleted file mode 100644 index c967190..0000000 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingAnimationActivity.kt +++ /dev/null @@ -1,66 +0,0 @@ -package br.alexandregpereira.jerry.app.animation - -import android.content.Context -import android.content.Intent -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeightFading -import br.alexandregpereira.jerry.expandable.collapseWidthFading -import br.alexandregpereira.jerry.expandable.expandHeightFading -import br.alexandregpereira.jerry.expandable.expandWidthFading -import kotlinx.android.synthetic.main.activity_collapse_fading_animation.* - -class CollapseFadingAnimationActivity : AppCompatActivity() { - - companion object { - fun getStartIntent(context: Context): Intent { - return Intent(context, CollapseFadingAnimationActivity::class.java) - } - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_collapse_fading_animation) - - collapseFadingTextButton.setOnClickListener { - collapseFadingTextView.collapseHeightFading() - } - - collapseExpandFadingTextButton.setOnClickListener { - collapseFadingTextView.expandHeightFading() - } - - collapseFixedFadingTextButton.setOnClickListener { - collapseFixedFadingTextView.collapseHeightFading() - } - - collapseExpandFixedFadingTextButton.setOnClickListener { - collapseFixedFadingTextView.expandHeightFading() - } - - collapseWidthFadingTextButton.setOnClickListener { - collapseWidthFadingTextView.collapseWidthFading() - } - - collapseExpandWidthFadingTextButton.setOnClickListener { - collapseWidthFadingTextView.expandWidthFading() - } - - collapseMatchWidthFadingButton.setOnClickListener { - collapseMatchWidthFadingView.collapseWidthFading() - } - - collapseExpandMatchWidthFadingButton.setOnClickListener { - collapseMatchWidthFadingView.expandWidthFading() - } - - collapseFixedWidthFadingButton.setOnClickListener { - collapseFixedWidthFadingView.collapseWidthFading() - } - - collapseExpandFixedWidthFadingButton.setOnClickListener { - collapseFixedWidthFadingView.expandWidthFading() - } - } -} diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandAnimationActivity.kt deleted file mode 100644 index 12f85e0..0000000 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandAnimationActivity.kt +++ /dev/null @@ -1,90 +0,0 @@ -package br.alexandregpereira.jerry.app.animation - -import android.content.Context -import android.content.Intent -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeight -import br.alexandregpereira.jerry.expandable.collapseWidth -import br.alexandregpereira.jerry.expandable.expandHeight -import br.alexandregpereira.jerry.expandable.expandWidth -import kotlinx.android.synthetic.main.activity_expand_animation.* - -class ExpandAnimationActivity : AppCompatActivity() { - - companion object { - fun getStartIntent(context: Context): Intent { - return Intent(context, ExpandAnimationActivity::class.java) - } - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_expand_animation) - - expandTextButton.setOnClickListener { - expandTextView.expandHeight() - } - - expandCollapseTextButton.setOnClickListener { - expandTextView.collapseHeight() - } - - expandWidthTextButton.setOnClickListener { - expandWidthTextView.expandWidth() - } - - expandCollapseWidthTextButton.setOnClickListener { - expandWidthTextView.collapseWidth() - } - - expandFixedTextButton.setOnClickListener { - expandFixedTextView.expandHeight() - } - - expandCollapseFixedTextButton.setOnClickListener { - expandFixedTextView.collapseHeight() - } - - expandWidthTextButton.setOnClickListener { - expandWidthTextView.expandWidth() - } - - expandCollapseWidthTextButton.setOnClickListener { - expandWidthTextView.collapseWidth() - } - - expandMatchWidthButton.setOnClickListener { - expandMatchWidthView.expandWidth() - } - - expandCollapseMatchWidthButton.setOnClickListener { - expandMatchWidthView.collapseWidth() - } - - expand0dpWidthButton.setOnClickListener { - expand0dpWidthView.expandWidth() - } - - expandCollapse0dpWidthButton.setOnClickListener { - expand0dpWidthView.collapseWidth() - } - - expandHalf0dpWidthButton.setOnClickListener { - expandHalf0dpWidthView.expandWidth() - } - - expandCollapseHalf0dpWidthButton.setOnClickListener { - expandHalf0dpWidthView.collapseWidth() - } - - expandFixedWidthButton.setOnClickListener { - expandFixedWidthView.expandWidth() - } - - expandCollapseFixedWidthButton.setOnClickListener { - expandFixedWidthView.collapseWidth() - } - } -} diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingAnimationActivity.kt deleted file mode 100644 index 4985cd8..0000000 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingAnimationActivity.kt +++ /dev/null @@ -1,74 +0,0 @@ -package br.alexandregpereira.jerry.app.animation - -import android.content.Context -import android.content.Intent -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.collapseHeightFading -import br.alexandregpereira.jerry.expandable.collapseWidthFading -import br.alexandregpereira.jerry.expandable.expandHeightFading -import br.alexandregpereira.jerry.expandable.expandWidthFading -import kotlinx.android.synthetic.main.activity_expand_fading_animation.* - -class ExpandFadingAnimationActivity : AppCompatActivity() { - - companion object { - fun getStartIntent(context: Context): Intent { - return Intent(context, ExpandFadingAnimationActivity::class.java) - } - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_expand_fading_animation) - - expandFadingTextButton.setOnClickListener { - expandFadingTextView.expandHeightFading() - } - - expandCollapseFadingTextButton.setOnClickListener { - expandFadingTextView.collapseHeightFading() - } - - expandWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.expandWidthFading() - } - - expandCollapseWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.collapseWidthFading() - } - - expandFixedFadingTextButton.setOnClickListener { - expandFixedFadingTextView.expandHeightFading() - } - - expandCollapseFixedFadingTextButton.setOnClickListener { - expandFixedFadingTextView.collapseHeightFading() - } - - expandWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.expandWidthFading() - } - - expandCollapseWidthFadingTextButton.setOnClickListener { - expandWidthFadingTextView.collapseWidthFading() - } - - expandMatchWidthFadingButton.setOnClickListener { - expandMatchWidthFadingView.expandWidthFading() - } - - expandCollapseMatchWidthFadingButton.setOnClickListener { - expandMatchWidthFadingView.collapseWidthFading() - } - - expandFixedWidthFadingButton.setOnClickListener { - expandFixedWidthFadingView.expandWidthFading() - } - - expandCollapseFixedWidthFadingButton.setOnClickListener { - expandFixedWidthFadingView.collapseWidthFading() - } - } -} diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeAnimationActivity.kt deleted file mode 100644 index 76dcd35..0000000 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeAnimationActivity.kt +++ /dev/null @@ -1,52 +0,0 @@ -package br.alexandregpereira.jerry.app.animation - -import android.content.Context -import android.content.Intent -import android.os.Bundle -import androidx.appcompat.app.AppCompatActivity -import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.fadeOut -import br.alexandregpereira.jerry.textview.setTextFade -import br.alexandregpereira.jerry.fadeIn -import kotlinx.android.synthetic.main.activity_fade_animation.* - -class FadeAnimationActivity : AppCompatActivity() { - - companion object { - fun getStartIntent(context: Context): Intent { - return Intent(context, FadeAnimationActivity::class.java) - } - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_fade_animation) - - val list = listOf( - "Fade text changeasaaaaa 2", - "Fade text change asda sdas 3", - "asdasd asdasdasd 4", - "Fade asdasdasddas change 5", - "Fade text change 1", - ).circularIterator() - fadeTextButton.setOnClickListener { - fadeTextView.setTextFade(list.next()) - } - - goneTextButton.setOnClickListener { - goneTextView.fadeOut() - } - - goneVisibleTextButton.setOnClickListener { - goneTextView.fadeIn() - } - - visibleTextButton.setOnClickListener { - visibleTextView.fadeIn() - } - - visibleInvisibleTextButton.setOnClickListener { - visibleTextView.fadeOut() - } - } -} diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableAnimationActivity.kt deleted file mode 100644 index bd136a5..0000000 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableAnimationActivity.kt +++ /dev/null @@ -1,74 +0,0 @@ -package br.alexandregpereira.jerry.app.animation - -import android.content.Context -import android.content.Intent -import android.os.Bundle -import android.os.Handler -import android.os.Looper -import androidx.appcompat.app.AppCompatActivity -import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.textview.setTextExpandable -import kotlinx.android.synthetic.main.activity_text_expandable_animation.* - -class TextExpandableAnimationActivity : AppCompatActivity() { - - companion object { - fun getStartIntent(context: Context): Intent { - return Intent(context, TextExpandableAnimationActivity::class.java) - } - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_text_expandable_animation) - - expandCancelTextButton.setOnClickListener { - expandCancelTextView.setTextExpandable( - getString(R.string.expand) - ) - Handler(Looper.getMainLooper()).postDelayed({ - expandCancelTextView.setTextExpandable(null) - }, 100) - } - - collapseCancelTextButton.setOnClickListener { - collapseCancelTextView.setTextExpandable(null) - Handler(Looper.getMainLooper()).postDelayed({ - collapseCancelTextView.setTextExpandable( - getString(R.string.collapse) - ) - }, 100) - } - - expandFadingTextButton.setOnClickListener { - expandFadingTextView.setTextExpandable( - getString(R.string.expand_collapse_animation) - ) - } - - expandCollapseFadingTextButton.setOnClickListener { - expandFadingTextView.setTextExpandable(null) - } - - collapseFadingTextButton.setOnClickListener { - collapseFadingTextView.setTextExpandable(null) - } - - collapseExpandFadingTextButton.setOnClickListener { - collapseFadingTextView.setTextExpandable( - getString(R.string.collapse_expand_animation) - ) - } - - val list = listOf( - "Fade text changeasaaaaa 2", - "Fade text change asda sdas 3", - "asdasd asdasdasd 4", - "Fade asdasdasddas change 5", - "Fade text change 1", - ).circularIterator() - textFadeTextButton.setOnClickListener { - textFadeTextView.setTextExpandable(list.next()) - } - } -} diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimation.kt deleted file mode 100644 index 9e05ce1..0000000 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimation.kt +++ /dev/null @@ -1,99 +0,0 @@ -package br.alexandregpereira.jerry - -import android.view.View -import android.view.ViewPropertyAnimator -import android.view.animation.Interpolator - -/** - * Change the visibility to GONE of the view using fade out animation. This method can be - * reverted in the middle of the animation if the [fadeIn] method is called. - * - * @param duration The duration of the animation - * @param onAnimationEnd The function to call when the animation is finished - */ -fun View.fadeOut( - duration: Long = ANIMATION_SHORT_TIME, - onAnimationEnd: (() -> Unit)? = null -) { - hideFadeOut(duration, hide = { gone() }, onAnimationEnd = onAnimationEnd) -} - -/** - * Change the visibility to VISIBLE of the view using fade in animation. This method can be - * reverted in the middle of the animation if the [fadeOut] - * method is called. - * - * @param duration The duration of the animation - * @param onAnimationEnd The function to call when the animation is finished - */ -fun View.fadeIn( - duration: Long = ANIMATION_SHORT_TIME, - onAnimationEnd: (() -> Unit)? = null -) { - if (isFadeInRunning() || (alpha == 1f && isVisible() && isFadeOutRunning().not())) { - if (isFadeInRunning().not()) { - onAnimationEnd?.invoke() - } - return - } - startFadeInRunning() - if (alpha == 1f) alpha = 0f - visible() - fadeInAnimation(duration = duration).withEndAction { - clearFadeInFadeOutRunning() - onAnimationEnd?.invoke() - } -} - -/** - * Start the fade out animation without changing the visibility status. The changes in the - * visibility status is delegate to the function [hide]. - * - * @param duration The duration of the animation - * @param onAnimationEnd The function to call when the animation is finished - * - * @see [fadeOut] - */ -internal fun View.hideFadeOut( - duration: Long = ANIMATION_SHORT_TIME, - hide: (() -> Unit)? = null, - onAnimationEnd: (() -> Unit)? = null -) { - if (isVisible().not() || isFadeOutRunning()) { - if (isFadeOutRunning().not()) { - onAnimationEnd?.invoke() - } - return - } - startFadeOutRunning() - fadeOutAnimation(duration).withEndAction { - hide?.invoke() - clearFadeInFadeOutRunning() - onAnimationEnd?.invoke() - } -} - -fun View.fadeOutAnimation( - duration: Long = ANIMATION_SHORT_TIME, - interpolator: Interpolator? = null -): ViewPropertyAnimator { - return animateFadeVisibility(0f, duration, interpolator) -} - -fun View.fadeInAnimation( - duration: Long = ANIMATION_SHORT_TIME, - interpolator: Interpolator? = null -): ViewPropertyAnimator { - return animateFadeVisibility(1f, duration, interpolator) -} - -fun View.animateFadeVisibility( - targetAlpha: Float, - duration: Long = ANIMATION_SHORT_TIME, - interpolator: Interpolator? = null -): ViewPropertyAnimator { - return animate() - .alpha(targetAlpha) - .setDuration(duration) - .apply { interpolator?.let { setInterpolator(it) } } -} diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt index bab41a7..2234e64 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt @@ -2,21 +2,6 @@ package br.alexandregpereira.jerry import android.view.View -internal fun View.isFadeInFadeOutRunning(animationMode: AnimationMode): Boolean = - isAnimationRunning(R.string.is_fade_in_fade_out_key, animationMode) - -internal fun View.setFadeInFadeOutRunning(animationMode: AnimationMode) = - setAnimationRunning(R.string.is_fade_in_fade_out_key, animationMode) - -fun View.isFadeInRunning() = isFadeInFadeOutRunning(AnimationMode.ENTER_ANIMATION_MODE) - -fun View.isFadeOutRunning() = isFadeInFadeOutRunning(AnimationMode.POP_ANIMATION_MODE) || - getSpringAnimation(SpringAnimationPropertyKey.ALPHA.id)?.run { +fun View.isFadeOutRunning() = getSpringAnimation(SpringAnimationPropertyKey.ALPHA.id)?.run { isRunning && spring?.finalPosition == 0f } == true - -internal fun View.clearFadeInFadeOutRunning() = setFadeInFadeOutRunning(AnimationMode.NONE_ANIMATION_MODE) - -internal fun View.startFadeInRunning() = setFadeInFadeOutRunning(AnimationMode.ENTER_ANIMATION_MODE) - -internal fun View.startFadeOutRunning() = setFadeInFadeOutRunning(AnimationMode.POP_ANIMATION_MODE) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt index 326d788..dc7e38c 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt @@ -41,7 +41,7 @@ fun View.goneFadeOut( fadeOutSpring( stiffness = stiffness, dampingRatio = dampingRatio - ).startSpringAnimation { canceled -> + ).start { canceled -> gone() onAnimationEnd?.invoke(canceled) } @@ -69,7 +69,7 @@ fun View.visibleFadeIn( fadeInSpring( stiffness = stiffness, dampingRatio = dampingRatio - ).startSpringAnimation( + ).start( onAnimationEnd = onAnimationEnd ) } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt index f4c593a..e5eaaf4 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt @@ -35,7 +35,7 @@ fun View.spring( key: Int, property: FloatPropertyCompat, targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, + stiffness: Float = ANIMATION_STIFFNESS, dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimation { var springAnimation = getSpringAnimation(key) @@ -57,7 +57,7 @@ fun JerryAnimation.spring( key: Int, property: FloatPropertyCompat, targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, + stiffness: Float = ANIMATION_STIFFNESS, dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { return animationSet().spring( @@ -73,7 +73,7 @@ fun JerryAnimationSet.spring( key: Int, property: FloatPropertyCompat, targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, + stiffness: Float = ANIMATION_STIFFNESS, dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { return this.copy( @@ -104,7 +104,7 @@ fun JerryAnimationSet.after(jerryAnimation: JerryAnimation): JerryAnimationSet { return this.copy(jerryAfterAnimationSet = jerryAnimation.animationSet()) } -fun JerryAnimationSet.startSpringAnimation( +fun JerryAnimationSet.start( onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { val onAnimationsEnd: (canceled: Boolean, completed: Boolean) -> Unit = { canceled, completed -> @@ -112,13 +112,13 @@ fun JerryAnimationSet.startSpringAnimation( if (canceled || jerryAfterAnimationSet == null) { onAnimationEnd?.invoke(canceled) } else { - jerryAfterAnimationSet.startSpringAnimation(onAnimationEnd) + jerryAfterAnimationSet.start(onAnimationEnd) } } } return jerryAnimations.forEach { animation -> val otherAnimations = jerryAnimations.filterNot { it.key == animation.key } - animation.startSpringAnimation { canceled -> + animation.start { canceled -> onAnimationsEnd( canceled, otherAnimations.isRunning().not() @@ -127,7 +127,7 @@ fun JerryAnimationSet.startSpringAnimation( } } -fun JerryAnimation.startSpringAnimation( +fun JerryAnimation.start( onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { this.addSpringEndListener( diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt index f13aeb1..9a77026 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt @@ -11,7 +11,7 @@ import br.alexandregpereira.jerry.elevationSpring import br.alexandregpereira.jerry.fadeInSpring import br.alexandregpereira.jerry.fadeOutSpring import br.alexandregpereira.jerry.fadeSpring -import br.alexandregpereira.jerry.startSpringAnimation +import br.alexandregpereira.jerry.start import br.alexandregpereira.jerry.translationXSpring import br.alexandregpereira.jerry.translationYSpring @@ -77,7 +77,7 @@ class ElevationSpringItemAnimator( translationYTargetValue, translationStiffness, translationDampingRatio - ).startSpringAnimation { canceled -> + ).start { canceled -> if (canceled) { if (deltaX != 0) translationX = translationOrigin if (deltaY != 0) translationY = translationOrigin @@ -146,7 +146,7 @@ class ElevationSpringItemAnimator( elevationDampingRatio ) ) - ).startSpringAnimation { + ).start { itemView.alpha = alphaFull itemView.elevation = elevationFull itemView.translationX = translationOrigin @@ -165,7 +165,7 @@ class ElevationSpringItemAnimator( dampingRatio = elevationDampingRatio ) ) - .startSpringAnimation { + .start { onAnimationFinished() } } @@ -175,7 +175,7 @@ class ElevationSpringItemAnimator( .after( fadeOutSpring(stiffness = alphaStiffness, alphaDampingRatio) ) - .startSpringAnimation { + .start { alpha = alphaFull elevation = elevationFull onAnimationFinished() diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimation.kt deleted file mode 100644 index b7b8351..0000000 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimation.kt +++ /dev/null @@ -1,207 +0,0 @@ -package br.alexandregpereira.jerry.expandable - -import android.view.View -import android.view.ViewGroup -import android.view.animation.Animation -import android.view.animation.Transformation -import br.alexandregpereira.jerry.ANIMATION_SHORT_TIME -import br.alexandregpereira.jerry.gone -import br.alexandregpereira.jerry.isVisible -import br.alexandregpereira.jerry.visible - -/** - * Animates expanding the height and changes the visibility status to VISIBLE. - * This animation handles double click. This method can be reverted in the middle of the animation - * if the [collapseHeight] method is called. Any alteration of the parent width during the this animation - * makes glitches in the animation. - * - * @param duration The duration of the animation. - * @param onAnimationEnd The function to call when the animation is finished. - */ -fun View.expandHeight( - duration: Long = ANIMATION_SHORT_TIME, - onProgressChange: ((interpolatedTime: Float) -> Unit)? = null, - onAnimationEnd: (() -> Unit)? = null -) = expand( - duration, - isHeight = true, - onProgressChange = onProgressChange, - onAnimationEnd = onAnimationEnd -) - -/** - * Animates expanding the width and changes the visibility status to VISIBLE. - * This animation handles double click. This method can be reverted in the middle of the animation - * if the [collapseWidth] method is called. Any alteration of the parent width during the this animation - * makes glitches in the animation. - * - * @param duration The duration of the animation. - * @param onAnimationEnd The function to call when the animation is finished. - */ -fun View.expandWidth( - duration: Long = ANIMATION_SHORT_TIME, - onProgressChange: ((interpolatedTime: Float) -> Unit)? = null, - onAnimationEnd: (() -> Unit)? = null -) = expand( - duration, - isHeight = false, - onProgressChange = onProgressChange, - onAnimationEnd = onAnimationEnd -) - -/** - * Animates collapsing the height and changes the visibility status to GONE. - * This animation handles double click. This method can be reverted in the middle of the animation - * if the [expandHeight] method is called. - * - * @param duration The duration of the animation. - * @param onAnimationEnd The function to call when the animation is finished. - */ -fun View.collapseHeight( - duration: Long = ANIMATION_SHORT_TIME, - onProgressChange: ((interpolatedTime: Float) -> Unit)? = null, - onAnimationEnd: (() -> Unit)? = null -) = collapse( - duration, - isHeight = true, - onProgressChange = onProgressChange, - onAnimationEnd = onAnimationEnd -) - -/** - * Animates collapsing the width and changes the visibility status to GONE. - * This animation handles double click. This method can be reverted in the middle of the animation - * if the [expandWidth] method is called. - * - * @param duration The duration of the animation. - * @param onAnimationEnd The function to call when the animation is finished. - */ -fun View.collapseWidth( - duration: Long = ANIMATION_SHORT_TIME, - onProgressChange: ((interpolatedTime: Float) -> Unit)? = null, - onAnimationEnd: (() -> Unit)? = null -) = collapse( - duration, - isHeight = false, - onProgressChange = onProgressChange, - onAnimationEnd = onAnimationEnd -) - -internal fun View.collapse( - duration: Long = ANIMATION_SHORT_TIME, - isHeight: Boolean = true, - onProgressChange: ((interpolatedTime: Float) -> Unit)? = null, - onAnimationEnd: (() -> Unit)? = null -) { - if (isVisible().not() || isCollapsingRunning()) { - return - } - startCollapsingRunning() - - val originalValue = getOrStoreWidthOrHeightOriginalValue(isHeight) - val initialValue = getCollapsingInitialValue(isHeight) - - val animation = object : Animation() { - override fun applyTransformation(interpolatedTime: Float, t: Transformation) { - if (interpolatedTime == 1f) { - gone() - setLayoutParamSize(originalValue, isHeight) - } else { - val value = initialValue - (initialValue * interpolatedTime).toInt() - val finalValue = if (value == 0) 1 else value - - setLayoutParamSize(finalValue, isHeight) - } - onProgressChange?.invoke(interpolatedTime) - requestLayout() - } - - override fun willChangeBounds(): Boolean { - return true - } - } - animation.setAnimationListener( - onEnd = { - finishExpandingCollapsingAnimation(isHeight, onAnimationEnd) - } - ) - - animation.duration = duration - startAnimation(animation) -} - -internal fun View.expand( - duration: Long = ANIMATION_SHORT_TIME, - isHeight: Boolean = true, - onProgressChange: ((interpolatedTime: Float) -> Unit)? = null, - onAnimationEnd: (() -> Unit)? = null -) { - if (isExpandingRunning() || (isVisible() && isCollapsingRunning().not())) { - return - } - - val originalValue = getOrStoreWidthOrHeightOriginalValue(isHeight) - val initialValue = (getLayoutParamSize(isHeight)).let { - if (it == originalValue || it < 0) 0 else it - } - val targetValue = getTargetValue(originalValue, isHeight) - - if (targetValue == null) { - finishExpandingCollapsingAnimation(isHeight, onAnimationEnd) - return - } - startExpandingRunning() - - if (initialValue == 0) { - if (isHeight) layoutParams.height = 1 else layoutParams.width = 1 - } - visible() - val animation = object : Animation() { - override fun applyTransformation(interpolatedTime: Float, t: Transformation) { - val value = initialValue + ((targetValue - initialValue) * interpolatedTime).toInt() - - val finalValue = if (value == 0) 1 else { - if (value == targetValue && originalValue == ViewGroup.LayoutParams.WRAP_CONTENT) { - ViewGroup.LayoutParams.WRAP_CONTENT - } else { - value - } - } - - setLayoutParamSize(finalValue, isHeight) - onProgressChange?.invoke(interpolatedTime) - requestLayout() - } - - override fun willChangeBounds(): Boolean { - return true - } - } - animation.setAnimationListener( - onEnd = { - finishExpandingCollapsingAnimation(isHeight, onAnimationEnd) - } - ) - - animation.duration = duration - startAnimation(animation) -} - -fun Animation.setAnimationListener( - onStart: (() -> Unit)? = null, - onEnd: (() -> Unit)? = null -): Animation { - setAnimationListener(object : Animation.AnimationListener { - override fun onAnimationRepeat(animation: Animation?) {} - - override fun onAnimationEnd(animation: Animation?) { - onEnd?.invoke() - } - - override fun onAnimationStart(animation: Animation?) { - onStart?.invoke() - } - }) - - return this -} diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimationCommon.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimationCommon.kt index b9ce7da..d7a539d 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimationCommon.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimationCommon.kt @@ -3,12 +3,8 @@ package br.alexandregpereira.jerry.expandable import android.view.View import android.view.View.MeasureSpec import android.view.ViewGroup -import br.alexandregpereira.jerry.AnimationMode -import br.alexandregpereira.jerry.OriginalValueKey import br.alexandregpereira.jerry.R import br.alexandregpereira.jerry.SpringAnimationPropertyKey -import br.alexandregpereira.jerry.isAnimationRunning -import br.alexandregpereira.jerry.setAnimationRunning import br.alexandregpereira.jerry.visible internal fun getExpandingCollapsingSpringKey(isHeight: Boolean): Int { @@ -19,15 +15,6 @@ internal fun getExpandingCollapsingSpringKey(isHeight: Boolean): Int { } } -internal fun View.finishExpandingCollapsingAnimation( - isHeight: Boolean, - onAnimationEnd: (() -> Unit)? -) { - clearWidthOrHeightOriginalValue(isHeight) - clearExpandingCollapsingRunning() - onAnimationEnd?.invoke() -} - internal fun View.finishExpandingCollapsingAnimation( isHeight: Boolean, canceled: Boolean, diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingAnimation.kt deleted file mode 100644 index 8e6d0eb..0000000 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingAnimation.kt +++ /dev/null @@ -1,94 +0,0 @@ -package br.alexandregpereira.jerry.expandable - -import android.view.View -import br.alexandregpereira.jerry.ANIMATION_SHORT_TIME -import br.alexandregpereira.jerry.fadeIn -import br.alexandregpereira.jerry.hideFadeOut -import br.alexandregpereira.jerry.isVisible - -/** - * Uses the [expandHeight] and [fadeIn] animations in sequence. This animation - * handles double click. This method can be reverted in the middle of the animation if the - * [collapseHeightFading] method is called. - * - * @param duration The duration of the animation - * @param onAnimationEnd The function to call when the animation is finished - */ -fun View.expandHeightFading( - duration: Long = ANIMATION_SHORT_TIME, - onAnimationEnd: (() -> Unit)? = null -) = expandFading(duration, isHeight = true, onAnimationEnd) - -/** - * Uses the [expandWidth] and [fadeIn] animations in sequence. This animation - * handles double click. This method can be reverted in the middle of the animation if the - * [collapseWidthFading] method is called. - * - * @param duration The duration of the animation - * @param onAnimationEnd The function to call when the animation is finished - */ -fun View.expandWidthFading( - duration: Long = ANIMATION_SHORT_TIME, - onAnimationEnd: (() -> Unit)? = null -) = expandFading(duration, isHeight = false, onAnimationEnd) - -/** - * Uses the [hideFadeOut] and [collapseHeight] animations in sequence. This animation - * handles double click. This method can be reverted in the middle of the animation if the - * [expandHeightFading] method is called. - * - * @param duration The duration of the animation - * @param onAnimationEnd The function to call when the animation is finished - */ -fun View.collapseHeightFading( - duration: Long = ANIMATION_SHORT_TIME, - onAnimationEnd: (() -> Unit)? = null -) = collapseFading(duration, isHeight = true, onAnimationEnd) - -/** - * Uses the [hideFadeOut] and [collapseWidth] animations in sequence. This animation - * handles double click. This method can be reverted in the middle of the animation if the - * [expandWidthFading] method is called. - * - * @param duration The duration of the animation - * @param onAnimationEnd The function to call when the animation is finished - */ -fun View.collapseWidthFading( - duration: Long = ANIMATION_SHORT_TIME, - onAnimationEnd: (() -> Unit)? = null -) = collapseFading(duration, isHeight = false, onAnimationEnd) - -private fun View.collapseFading( - duration: Long = ANIMATION_SHORT_TIME, - isHeight: Boolean = true, - onAnimationEnd: (() -> Unit)? = null -) { - if (isExpandingRunning()) { - collapse(duration = duration, isHeight = isHeight, onAnimationEnd = onAnimationEnd) - return - } - - hideFadeOut(duration = duration / 2) { - collapse(duration = duration / 2, isHeight = isHeight, onAnimationEnd = onAnimationEnd) - } -} - -private fun View.expandFading( - duration: Long = ANIMATION_SHORT_TIME, - isHeight: Boolean = true, - onAnimationEnd: (() -> Unit)? = null -) { - if (alpha == 1f && (isVisible() && isCollapsingRunning().not())) { - return - } - if (alpha == 1f) alpha = 0f - - if (alpha > 0f && alpha < 1f) { - fadeIn(duration = duration, onAnimationEnd = onAnimationEnd) - return - } - - expand(duration = duration / 2, isHeight = isHeight) { - fadeIn(duration = duration / 2, onAnimationEnd = onAnimationEnd) - } -} diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt index 3d5c6a3..b1c9ac4 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt @@ -9,7 +9,7 @@ import br.alexandregpereira.jerry.visibleFadeIn import br.alexandregpereira.jerry.goneFadeOut import br.alexandregpereira.jerry.fadeSpring import br.alexandregpereira.jerry.isVisible -import br.alexandregpereira.jerry.startSpringAnimation +import br.alexandregpereira.jerry.start /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the @@ -123,7 +123,7 @@ private fun View.goneCollapseFadeOut( isHeight: Boolean = true, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - fadeOutSpring(stiffness = stiffness * 2f, dampingRatio).startSpringAnimation { + fadeOutSpring(stiffness = stiffness * 2f, dampingRatio).start { goneCollapse( stiffness = stiffness * 2f, isHeight = isHeight, diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt index cb0b3dd..fb7261f 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt @@ -6,7 +6,7 @@ import br.alexandregpereira.jerry.ANIMATION_STIFFNESS import br.alexandregpereira.jerry.gone import br.alexandregpereira.jerry.isVisible import br.alexandregpereira.jerry.spring -import br.alexandregpereira.jerry.startSpringAnimation +import br.alexandregpereira.jerry.start import br.alexandregpereira.jerry.visible /** @@ -156,7 +156,7 @@ internal fun View.goneCollapse( stiffness = stiffness, isHeight = isHeight, onProgressChange = onProgressChange, - ).startSpringAnimation { canceled -> + ).start { canceled -> gone() setLayoutParamSize(getOrStoreWidthOrHeightOriginalValue(isHeight), isHeight) finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) @@ -202,7 +202,7 @@ internal fun View.visibleExpand( stiffness = stiffness, isHeight = isHeight, onProgressChange = onProgressChange - ).startSpringAnimation { canceled -> + ).start { canceled -> finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) } } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/OriginalValueKey.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/OriginalValueKey.kt similarity index 54% rename from jerry/src/main/java/br/alexandregpereira/jerry/OriginalValueKey.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/expandable/OriginalValueKey.kt index 00e8a0b..35fd771 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/OriginalValueKey.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/OriginalValueKey.kt @@ -1,8 +1,9 @@ -package br.alexandregpereira.jerry +package br.alexandregpereira.jerry.expandable import androidx.annotation.StringRes +import br.alexandregpereira.jerry.R -enum class OriginalValueKey(@StringRes val id: Int) { +internal enum class OriginalValueKey(@StringRes val id: Int) { HEIGHT(R.string.expanding_collapsing_height_original_value_key), WIDTH(R.string.expanding_collapsing_width_original_value_key) } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ViewAnimation.kt similarity index 84% rename from jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/expandable/ViewAnimation.kt index ca4ae1e..bc18c10 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/ViewAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ViewAnimation.kt @@ -1,9 +1,6 @@ -package br.alexandregpereira.jerry +package br.alexandregpereira.jerry.expandable import android.view.View -import android.view.ViewPropertyAnimator - -const val ANIMATION_SHORT_TIME = 300L enum class AnimationMode { /** @@ -47,9 +44,3 @@ internal fun View.setAnimationRunning(key: Int, animationMode: AnimationMode) { setTag(key, animationMode) } } - -internal fun ViewPropertyAnimator.onAnimationEnd(onEnd: () -> Unit): ViewPropertyAnimator { - return withEndAction { - onEnd() - } -} diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewAnimation.kt deleted file mode 100644 index ee315f3..0000000 --- a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewAnimation.kt +++ /dev/null @@ -1,86 +0,0 @@ -package br.alexandregpereira.jerry.textview - -import android.view.animation.AccelerateInterpolator -import android.view.animation.DecelerateInterpolator -import android.widget.TextView -import br.alexandregpereira.jerry.ANIMATION_SHORT_TIME -import br.alexandregpereira.jerry.expandable.collapseHeightFading -import br.alexandregpereira.jerry.expandable.expandHeightFading -import br.alexandregpereira.jerry.expandable.isCollapsingRunning -import br.alexandregpereira.jerry.fadeInAnimation -import br.alexandregpereira.jerry.fadeOutAnimation -import br.alexandregpereira.jerry.isFadeOutRunning -import br.alexandregpereira.jerry.isVisible -import br.alexandregpereira.jerry.onAnimationEnd - -/** - * Uses the [setTextFade], [expandHeightFading] or [collapseHeightFading] animation methods - * depending of the TextView state. If the new text is null or empty, the [collapseHeightFading] - * is used, else if the TextView is already visible, the [setTextFade] is used, else the - * [expandHeightFading] is used. - * - * @param text The new text of the TextView - * @param duration The duration of the animation - * @param onFirstFadeEnd The function to call when the first fade of the [setTextFade] is finished - * @param onAnimationEnd The function to call when the animation is finished - */ -fun TextView.setTextExpandable( - text: String?, - duration: Long = ANIMATION_SHORT_TIME, - onAnimationEnd: (() -> Unit)? = null, - onFirstFadeEnd: (() -> Unit)? = null -) { - if (text == null || text.trim().isEmpty()) { - collapseHeightFading(onAnimationEnd = onAnimationEnd) - return - } - - if (isVisible() && isCollapsingRunning().not() && isFadeOutRunning().not()) { - setTextFade( - text, - duration = duration, - onFirstAnimationEnd = onFirstFadeEnd - ) - return - } - setText(text) - expandHeightFading(onAnimationEnd = onAnimationEnd) -} - -/** - * Changes the text of the [TextView] using cross fade animation. - * - * @param text The new text of the TextView - * @param duration The duration of the animation - * @param onFirstAnimationEnd The function to call when the first fade is finished - */ -fun TextView.setTextFade( - text: String = "", - duration: Long = ANIMATION_SHORT_TIME, - onFirstAnimationEnd: (() -> Unit)? = null -) { - val textView = this - val oldText = textView.text.toString() - - if (oldText == text) return - - if (oldText.isEmpty()) { - textView.text = text - textView.alpha = 0f - fadeInAnimation( - duration = duration, - interpolator = DecelerateInterpolator() - ) - return - } - - fadeOutAnimation(duration = duration, interpolator = AccelerateInterpolator()) - .onAnimationEnd { - textView.text = text - onFirstAnimationEnd?.invoke() - fadeInAnimation( - duration = duration, - interpolator = DecelerateInterpolator() - ) - } -} diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt index 404c88c..744d5c3 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt @@ -11,7 +11,7 @@ import br.alexandregpereira.jerry.fadeInSpring import br.alexandregpereira.jerry.fadeOutSpring import br.alexandregpereira.jerry.isFadeOutRunning import br.alexandregpereira.jerry.isVisible -import br.alexandregpereira.jerry.startSpringAnimation +import br.alexandregpereira.jerry.start /** * Uses the [setTextFadeSpring], [visibleExpandHeightFadeIn] or [goneCollapseHeightFadeOut] @@ -73,16 +73,16 @@ fun TextView.setTextFadeSpring( if (oldText.isEmpty()) { textView.text = text textView.alpha = 0f - fadeInSpring(stiffness = stiffness, dampingRatio).startSpringAnimation( + fadeInSpring(stiffness = stiffness, dampingRatio).start( onAnimationEnd = onAnimationEnd ) return } fadeOutSpring(stiffness = stiffness * 1.5f, dampingRatio) - .startSpringAnimation { + .start { textView.text = text - fadeInSpring(stiffness = stiffness * 1.5f, dampingRatio).startSpringAnimation( + fadeInSpring(stiffness = stiffness * 1.5f, dampingRatio).start( onAnimationEnd = onAnimationEnd ) } From 57e98c13775ec7a1dffecb0789d1a12e3ec1f72c Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Mon, 19 Oct 2020 23:45:44 -0300 Subject: [PATCH 05/14] Remove unused code --- .../src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt index c1cdc0f..0429ce3 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/JerryAnimation.kt @@ -7,8 +7,7 @@ data class JerryAnimation( val key: Int, val view: View, val springAnimation: SpringAnimation, - internal val targetValue: Float, - internal val jerryAnimations: List? = null + internal val targetValue: Float ) { val isRunning: Boolean get() = springAnimation.isRunning From e08d66f1450043c6d21add25c541a117a2a50a0d Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 00:30:26 -0300 Subject: [PATCH 06/14] Add force function --- .../jerry/ElevationSpringAnimation.kt | 25 ++---- .../jerry/FadeSpringAnimation.kt | 82 +++++-------------- .../jerry/SpringAnimation.kt | 43 ++++++---- .../jerry/TransalationSpringAnimation.kt | 48 +++-------- .../animator/ElevationSpringItemAnimator.kt | 70 +++++++--------- .../ExpandableFadingSpringAnimation.kt | 7 +- .../expandable/ExpandableSpingAnimation.kt | 11 +-- .../jerry/textview/TextViewSpringAnimation.kt | 7 +- 8 files changed, 107 insertions(+), 186 deletions(-) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt index 76e070b..58f02ad 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt @@ -3,45 +3,32 @@ package br.alexandregpereira.jerry import android.view.View import androidx.annotation.RequiresApi import androidx.dynamicanimation.animation.FloatPropertyCompat -import androidx.dynamicanimation.animation.SpringForce @RequiresApi(21) fun JerryAnimationSet.elevationSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.ELEVATION.id, property = elevationViewProperty(), - targetValue = targetValue, - dampingRatio = dampingRatio, - stiffness = stiffness + targetValue = targetValue ) @RequiresApi(21) fun JerryAnimation.elevationSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.ELEVATION.id, property = elevationViewProperty(), - targetValue = targetValue, - dampingRatio = dampingRatio, - stiffness = stiffness + targetValue = targetValue ) @RequiresApi(21) fun View.elevationSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.ELEVATION.id, property = elevationViewProperty(), - targetValue = targetValue, - dampingRatio = dampingRatio, - stiffness = stiffness + targetValue = targetValue ) @RequiresApi(21) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt index dc7e38c..12efdc3 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt @@ -38,7 +38,7 @@ fun View.goneFadeOut( dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - fadeOutSpring( + fadeOutSpring().force( stiffness = stiffness, dampingRatio = dampingRatio ).start { canceled -> @@ -66,7 +66,7 @@ fun View.visibleFadeIn( ) { if (isVisible().not()) alpha = 0f visible() - fadeInSpring( + fadeInSpring().force( stiffness = stiffness, dampingRatio = dampingRatio ).start( @@ -74,92 +74,50 @@ fun View.visibleFadeIn( ) } -fun JerryAnimationSet.fadeInSpring( - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY -) = fadeSpring( - targetValue = 1f, - stiffness = stiffness, - dampingRatio = dampingRatio +fun JerryAnimationSet.fadeInSpring() = fadeSpring( + targetValue = 1f ) -fun JerryAnimation.fadeInSpring( - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY -) = fadeSpring( - targetValue = 1f, - stiffness = stiffness, - dampingRatio = dampingRatio +fun JerryAnimation.fadeInSpring() = fadeSpring( + targetValue = 1f ) -fun View.fadeInSpring( - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY -) = fadeSpring( - targetValue = 1f, - stiffness = stiffness, - dampingRatio = dampingRatio +fun View.fadeInSpring() = fadeSpring( + targetValue = 1f ) -fun JerryAnimationSet.fadeOutSpring( - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY -) = fadeSpring( - targetValue = 0f, - dampingRatio = dampingRatio, - stiffness = stiffness +fun JerryAnimationSet.fadeOutSpring() = fadeSpring( + targetValue = 0f ) -fun JerryAnimation.fadeOutSpring( - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY -) = fadeSpring( - targetValue = 0f, - stiffness = stiffness, - dampingRatio = dampingRatio +fun JerryAnimation.fadeOutSpring() = fadeSpring( + targetValue = 0f ) -fun View.fadeOutSpring( - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY -) = fadeSpring( - targetValue = 0f, - stiffness = stiffness, - dampingRatio = dampingRatio +fun View.fadeOutSpring() = fadeSpring( + targetValue = 0f ) fun JerryAnimationSet.fadeSpring( - targetValue: Float, - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.ALPHA.id, property = DynamicAnimation.ALPHA, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio + targetValue = targetValue ) fun JerryAnimation.fadeSpring( - targetValue: Float, - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.ALPHA.id, property = DynamicAnimation.ALPHA, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio + targetValue = targetValue ) fun View.fadeSpring( - targetValue: Float, - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.ALPHA.id, property = DynamicAnimation.ALPHA, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio + targetValue = targetValue ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt index e5eaaf4..50eb775 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt @@ -34,9 +34,7 @@ fun View.skipToEndSpringAnimation(key: Int) { fun View.spring( key: Int, property: FloatPropertyCompat, - targetValue: Float, - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ): JerryAnimation { var springAnimation = getSpringAnimation(key) if (springAnimation == null) { @@ -47,8 +45,8 @@ fun View.spring( setTag(key, SpringAnimationHolder(springAnimation)) } springAnimation.spring.apply { - this.dampingRatio = dampingRatio - this.stiffness = stiffness + this.dampingRatio = SpringForce.DAMPING_RATIO_NO_BOUNCY + this.stiffness = ANIMATION_STIFFNESS } return JerryAnimation(key = key, view = this, springAnimation, targetValue) } @@ -56,34 +54,49 @@ fun View.spring( fun JerryAnimation.spring( key: Int, property: FloatPropertyCompat, - targetValue: Float, - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ): JerryAnimationSet { return animationSet().spring( key = key, property = property, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio + targetValue = targetValue ) } fun JerryAnimationSet.spring( key: Int, property: FloatPropertyCompat, - targetValue: Float, - stiffness: Float = ANIMATION_STIFFNESS, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ): JerryAnimationSet { return this.copy( jerryAnimations = jerryAnimations + listOf(jerryAnimations.last().view.spring( - key, property, targetValue, stiffness, dampingRatio + key, property, targetValue )) ) } +fun JerryAnimation.force( + stiffness: Float, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +): JerryAnimation { + return this.apply { + springAnimation.spring.apply { + this.dampingRatio = dampingRatio + this.stiffness = stiffness + } + } +} + +fun JerryAnimationSet.force( + stiffness: Float, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +): JerryAnimationSet { + return this.apply { + jerryAnimations.last().force(stiffness, dampingRatio) + } +} + private fun JerryAnimation.animationSet(): JerryAnimationSet { return JerryAnimationSet(jerryAnimations = listOf(this)) } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt index b62b5db..35d1df9 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt @@ -5,73 +5,49 @@ import androidx.dynamicanimation.animation.DynamicAnimation import androidx.dynamicanimation.animation.SpringForce fun JerryAnimation.translationXSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_X.id, property = DynamicAnimation.TRANSLATION_X, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio, + targetValue = targetValue ) fun JerryAnimationSet.translationXSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_X.id, property = DynamicAnimation.TRANSLATION_X, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio, + targetValue = targetValue ) fun View.translationXSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_X.id, property = DynamicAnimation.TRANSLATION_X, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio, + targetValue = targetValue ) fun JerryAnimation.translationYSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_Y.id, property = DynamicAnimation.TRANSLATION_Y, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio, + targetValue = targetValue ) fun JerryAnimationSet.translationYSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_Y.id, property = DynamicAnimation.TRANSLATION_Y, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio, + targetValue = targetValue ) fun View.translationYSpring( - targetValue: Float, - stiffness: Float = SpringForce.STIFFNESS_LOW, - dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY + targetValue: Float ) = spring( key = SpringAnimationPropertyKey.TRANSLATION_Y.id, property = DynamicAnimation.TRANSLATION_Y, - targetValue = targetValue, - stiffness = stiffness, - dampingRatio = dampingRatio, + targetValue = targetValue ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt index 9a77026..9cb9cd2 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt @@ -11,6 +11,7 @@ import br.alexandregpereira.jerry.elevationSpring import br.alexandregpereira.jerry.fadeInSpring import br.alexandregpereira.jerry.fadeOutSpring import br.alexandregpereira.jerry.fadeSpring +import br.alexandregpereira.jerry.force import br.alexandregpereira.jerry.start import br.alexandregpereira.jerry.translationXSpring import br.alexandregpereira.jerry.translationYSpring @@ -69,23 +70,19 @@ class ElevationSpringItemAnimator( val translationXTargetValue = if (deltaX != 0) translationOrigin else translationX val translationYTargetValue = if (deltaY != 0) translationOrigin else translationY - translationXSpring( - translationXTargetValue, - translationStiffness, - translationDampingRatio - ).translationYSpring( - translationYTargetValue, - translationStiffness, - translationDampingRatio - ).start { canceled -> - if (canceled) { - if (deltaX != 0) translationX = translationOrigin - if (deltaY != 0) translationY = translationOrigin + translationXSpring(translationXTargetValue) + .force(translationStiffness, translationDampingRatio) + .translationYSpring(translationYTargetValue) + .force(translationStiffness, translationDampingRatio) + .start { canceled -> + if (canceled) { + if (deltaX != 0) translationX = translationOrigin + if (deltaY != 0) translationY = translationOrigin + } + alpha = alphaFull + elevation = elevationFull + onAnimateMoveFinished(holder) } - alpha = alphaFull - elevation = elevationFull - onAnimateMoveFinished(holder) - } } return true } @@ -126,25 +123,18 @@ class ElevationSpringItemAnimator( ) { val elevationFull = itemView.elevationFull this.itemView.apply { - elevationSpring(targetValue = elevationNone, elevationStiffness, elevationDampingRatio) + elevationSpring(targetValue = elevationNone) + .force(elevationStiffness, elevationDampingRatio) .after( - fadeSpring(alphaTargetValue, alphaStiffness, alphaDampingRatio) - .translationXSpring( - translationXTargetValue, - translationStiffness, - translationDampingRatio - ) - .translationYSpring( - translationYTargetValue, - translationStiffness, - translationDampingRatio - ) + fadeSpring(alphaTargetValue) + .force(alphaStiffness, alphaDampingRatio) + .translationXSpring(translationXTargetValue) + .force(translationStiffness, translationDampingRatio) + .translationYSpring(translationYTargetValue) + .force(translationStiffness, translationDampingRatio) .after( - elevationSpring( - targetValue = elevationFull, - elevationStiffness, - elevationDampingRatio - ) + elevationSpring(targetValue = elevationFull) + .force(elevationStiffness, elevationDampingRatio) ) ).start { itemView.alpha = alphaFull @@ -157,13 +147,10 @@ class ElevationSpringItemAnimator( } private fun View.startFadeElevationInAnimation(onAnimationFinished: () -> Unit) { - fadeInSpring(stiffness = alphaStiffness, alphaDampingRatio) + fadeInSpring().force(stiffness = alphaStiffness, alphaDampingRatio) .after( - elevationSpring( - targetValue = elevationFull, - stiffness = elevationStiffness, - dampingRatio = elevationDampingRatio - ) + elevationSpring(targetValue = elevationFull) + .force(stiffness = elevationStiffness, dampingRatio = elevationDampingRatio) ) .start { onAnimationFinished() @@ -171,9 +158,10 @@ class ElevationSpringItemAnimator( } private fun View.startFadeElevationOutAnimation(onAnimationFinished: () -> Unit) { - elevationSpring(targetValue = elevationNone, elevationStiffness, elevationDampingRatio) + elevationSpring(targetValue = elevationNone) + .force(elevationStiffness, elevationDampingRatio) .after( - fadeOutSpring(stiffness = alphaStiffness, alphaDampingRatio) + fadeOutSpring().force(stiffness = alphaStiffness, alphaDampingRatio) ) .start { alpha = alphaFull diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt index b1c9ac4..edf42b6 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt @@ -5,11 +5,12 @@ import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringForce import br.alexandregpereira.jerry.ANIMATION_STIFFNESS import br.alexandregpereira.jerry.fadeOutSpring -import br.alexandregpereira.jerry.visibleFadeIn -import br.alexandregpereira.jerry.goneFadeOut import br.alexandregpereira.jerry.fadeSpring +import br.alexandregpereira.jerry.force +import br.alexandregpereira.jerry.goneFadeOut import br.alexandregpereira.jerry.isVisible import br.alexandregpereira.jerry.start +import br.alexandregpereira.jerry.visibleFadeIn /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the @@ -123,7 +124,7 @@ private fun View.goneCollapseFadeOut( isHeight: Boolean = true, onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { - fadeOutSpring(stiffness = stiffness * 2f, dampingRatio).start { + fadeOutSpring().force(stiffness = stiffness * 2f, dampingRatio).start { goneCollapse( stiffness = stiffness * 2f, isHeight = isHeight, diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt index fb7261f..617a46d 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt @@ -3,6 +3,7 @@ package br.alexandregpereira.jerry.expandable import android.view.View import androidx.dynamicanimation.animation.SpringAnimation import br.alexandregpereira.jerry.ANIMATION_STIFFNESS +import br.alexandregpereira.jerry.force import br.alexandregpereira.jerry.gone import br.alexandregpereira.jerry.isVisible import br.alexandregpereira.jerry.spring @@ -153,10 +154,9 @@ internal fun View.goneCollapse( getOrStoreWidthOrHeightOriginalValue(isHeight) expandCollapseSpring( targetValue = 0f, - stiffness = stiffness, isHeight = isHeight, onProgressChange = onProgressChange, - ).start { canceled -> + ).force(stiffness = stiffness).start { canceled -> gone() setLayoutParamSize(getOrStoreWidthOrHeightOriginalValue(isHeight), isHeight) finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) @@ -199,22 +199,19 @@ internal fun View.visibleExpand( expandCollapseSpring( targetValue = targetValue.toFloat(), - stiffness = stiffness, isHeight = isHeight, onProgressChange = onProgressChange - ).start { canceled -> + ).force(stiffness = stiffness).start { canceled -> finishExpandingCollapsingAnimation(isHeight, canceled, onAnimationEnd) } } private fun View.expandCollapseSpring( targetValue: Float, - stiffness: Float, isHeight: Boolean, onProgressChange: ((progress: Float) -> Unit)?, ) = spring( key = getExpandingCollapsingSpringKey(isHeight), property = widthHeightViewProperty(isHeight, onProgressChange), - targetValue = targetValue, - stiffness = stiffness + targetValue = targetValue ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt index 744d5c3..7180724 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt @@ -9,6 +9,7 @@ import br.alexandregpereira.jerry.expandable.isCollapsingRunning import br.alexandregpereira.jerry.expandable.visibleExpandHeightFadeIn import br.alexandregpereira.jerry.fadeInSpring import br.alexandregpereira.jerry.fadeOutSpring +import br.alexandregpereira.jerry.force import br.alexandregpereira.jerry.isFadeOutRunning import br.alexandregpereira.jerry.isVisible import br.alexandregpereira.jerry.start @@ -73,16 +74,16 @@ fun TextView.setTextFadeSpring( if (oldText.isEmpty()) { textView.text = text textView.alpha = 0f - fadeInSpring(stiffness = stiffness, dampingRatio).start( + fadeInSpring().force(stiffness = stiffness, dampingRatio).start( onAnimationEnd = onAnimationEnd ) return } - fadeOutSpring(stiffness = stiffness * 1.5f, dampingRatio) + fadeOutSpring().force(stiffness = stiffness * 1.5f, dampingRatio) .start { textView.text = text - fadeInSpring(stiffness = stiffness * 1.5f, dampingRatio).start( + fadeInSpring().force(stiffness = stiffness * 1.5f, dampingRatio).start( onAnimationEnd = onAnimationEnd ) } From 3340ba7e26dbd89eae29b47f7ba31243a28a1c6e Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 00:30:48 -0300 Subject: [PATCH 07/14] Add force function --- .../src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt index 50eb775..293c983 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt @@ -208,4 +208,3 @@ private fun View.addSpringAnimationKeyIfNotContains(key: Int) { }) } } - From 86b182bd259970d3dfe44007b3cc71a4672ba31a Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 00:40:55 -0300 Subject: [PATCH 08/14] Organize packages --- .../CollapseFadingSpringAnimationActivity.kt | 8 ++++---- .../CollapseSpringAnimationActivity.kt | 10 +++++----- .../ExpandFadingSpringAnimationActivity.kt | 8 ++++---- .../animation/ExpandSpringAnimationActivity.kt | 8 ++++---- .../animation/FadeSpringAnimationActivity.kt | 6 +++--- .../TextExpandableSpringAnimationActivity.kt | 2 +- .../jerry/FadeAnimationCommon.kt | 7 ------- .../{ => animation}/ElevationSpringAnimation.kt | 6 +++++- .../{ => animation}/FadeSpringAnimation.kt | 17 ++++++++++++++++- .../TransalationSpringAnimation.kt | 7 +++++-- .../expandable/ExpandableAnimationCommon.kt | 2 +- .../ExpandableFadingSpringAnimation.kt | 10 +++++----- .../expandable/ExpandableSpingAnimation.kt | 2 +- .../expandable/OriginalValueKey.kt | 2 +- .../{ => animation}/expandable/ViewAnimation.kt | 2 +- .../expandable/WidthHeightViewProperty.kt | 2 +- .../textview/TextViewSpringAnimation.kt | 14 +++++++------- .../animator/ElevationSpringItemAnimator.kt | 12 ++++++------ 18 files changed, 70 insertions(+), 55 deletions(-) delete mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/ElevationSpringAnimation.kt (80%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/FadeSpringAnimation.kt (83%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/TransalationSpringAnimation.kt (84%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/expandable/ExpandableAnimationCommon.kt (98%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/expandable/ExpandableFadingSpringAnimation.kt (95%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/expandable/ExpandableSpingAnimation.kt (99%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/expandable/OriginalValueKey.kt (83%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/expandable/ViewAnimation.kt (96%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/expandable/WidthHeightViewProperty.kt (97%) rename jerry/src/main/java/br/alexandregpereira/jerry/{ => animation}/textview/TextViewSpringAnimation.kt (86%) diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt index 35a2c49..df27a60 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseFadingSpringAnimationActivity.kt @@ -5,10 +5,10 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.goneCollapseHeightFadeOut -import br.alexandregpereira.jerry.expandable.goneCollapseWidthFadeOut -import br.alexandregpereira.jerry.expandable.visibleExpandHeightFadeIn -import br.alexandregpereira.jerry.expandable.visibleExpandWidthFadeIn +import br.alexandregpereira.jerry.animation.expandable.goneCollapseHeightFadeOut +import br.alexandregpereira.jerry.animation.expandable.goneCollapseWidthFadeOut +import br.alexandregpereira.jerry.animation.expandable.visibleExpandHeightFadeIn +import br.alexandregpereira.jerry.animation.expandable.visibleExpandWidthFadeIn import kotlinx.android.synthetic.main.activity_collapse_fading_animation.* class CollapseFadingSpringAnimationActivity : AppCompatActivity() { diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt index 7dcf0df..a4727c6 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt @@ -5,12 +5,12 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat -import br.alexandregpereira.jerry.expandable.visibleOrGoneExpandableWidth +import br.alexandregpereira.jerry.animation.expandable.visibleOrGoneExpandableWidth import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.goneCollapseHeight -import br.alexandregpereira.jerry.expandable.goneCollapseWidth -import br.alexandregpereira.jerry.expandable.visibleExpandHeight -import br.alexandregpereira.jerry.expandable.visibleExpandWidth +import br.alexandregpereira.jerry.animation.expandable.goneCollapseHeight +import br.alexandregpereira.jerry.animation.expandable.goneCollapseWidth +import br.alexandregpereira.jerry.animation.expandable.visibleExpandHeight +import br.alexandregpereira.jerry.animation.expandable.visibleExpandWidth import kotlinx.android.synthetic.main.activity_collapse_animation.* import kotlinx.android.synthetic.main.container_animation_info.view.* diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt index 73f66b0..8bc9c17 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandFadingSpringAnimationActivity.kt @@ -5,10 +5,10 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.goneCollapseHeightFadeOut -import br.alexandregpereira.jerry.expandable.goneCollapseWidthFadeOut -import br.alexandregpereira.jerry.expandable.visibleExpandHeightFadeIn -import br.alexandregpereira.jerry.expandable.visibleExpandWidthFadeIn +import br.alexandregpereira.jerry.animation.expandable.goneCollapseHeightFadeOut +import br.alexandregpereira.jerry.animation.expandable.goneCollapseWidthFadeOut +import br.alexandregpereira.jerry.animation.expandable.visibleExpandHeightFadeIn +import br.alexandregpereira.jerry.animation.expandable.visibleExpandWidthFadeIn import kotlinx.android.synthetic.main.activity_expand_fading_animation.* class ExpandFadingSpringAnimationActivity : AppCompatActivity() { diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt index de21e77..925c1fe 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/ExpandSpringAnimationActivity.kt @@ -5,10 +5,10 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.expandable.goneCollapseHeight -import br.alexandregpereira.jerry.expandable.goneCollapseWidth -import br.alexandregpereira.jerry.expandable.visibleExpandHeight -import br.alexandregpereira.jerry.expandable.visibleExpandWidth +import br.alexandregpereira.jerry.animation.expandable.goneCollapseHeight +import br.alexandregpereira.jerry.animation.expandable.goneCollapseWidth +import br.alexandregpereira.jerry.animation.expandable.visibleExpandHeight +import br.alexandregpereira.jerry.animation.expandable.visibleExpandWidth import kotlinx.android.synthetic.main.activity_expand_animation.* class ExpandSpringAnimationActivity : AppCompatActivity() { diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt index 105d4ec..edce1ab 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/FadeSpringAnimationActivity.kt @@ -5,9 +5,9 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.goneFadeOut -import br.alexandregpereira.jerry.textview.setTextFadeSpring -import br.alexandregpereira.jerry.visibleFadeIn +import br.alexandregpereira.jerry.animation.goneFadeOut +import br.alexandregpereira.jerry.animation.textview.setTextFadeSpring +import br.alexandregpereira.jerry.animation.visibleFadeIn import kotlinx.android.synthetic.main.activity_fade_animation.* class FadeSpringAnimationActivity : AppCompatActivity() { diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableSpringAnimationActivity.kt index 6e79e92..dc1c849 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/TextExpandableSpringAnimationActivity.kt @@ -7,7 +7,7 @@ import android.os.Handler import android.os.Looper import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.app.R -import br.alexandregpereira.jerry.textview.setTextExpandableSpring +import br.alexandregpereira.jerry.animation.textview.setTextExpandableSpring import kotlinx.android.synthetic.main.activity_text_expandable_animation.* class TextExpandableSpringAnimationActivity : AppCompatActivity() { diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt b/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt deleted file mode 100644 index 2234e64..0000000 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeAnimationCommon.kt +++ /dev/null @@ -1,7 +0,0 @@ -package br.alexandregpereira.jerry - -import android.view.View - -fun View.isFadeOutRunning() = getSpringAnimation(SpringAnimationPropertyKey.ALPHA.id)?.run { - isRunning && spring?.finalPosition == 0f - } == true diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt similarity index 80% rename from jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt index 58f02ad..6199247 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/ElevationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt @@ -1,8 +1,12 @@ -package br.alexandregpereira.jerry +package br.alexandregpereira.jerry.animation import android.view.View import androidx.annotation.RequiresApi import androidx.dynamicanimation.animation.FloatPropertyCompat +import br.alexandregpereira.jerry.JerryAnimation +import br.alexandregpereira.jerry.JerryAnimationSet +import br.alexandregpereira.jerry.SpringAnimationPropertyKey +import br.alexandregpereira.jerry.spring @RequiresApi(21) fun JerryAnimationSet.elevationSpring( diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/FadeSpringAnimation.kt similarity index 83% rename from jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/FadeSpringAnimation.kt index 12efdc3..a2333fa 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/FadeSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/FadeSpringAnimation.kt @@ -1,9 +1,20 @@ -package br.alexandregpereira.jerry +package br.alexandregpereira.jerry.animation import android.view.View import androidx.dynamicanimation.animation.DynamicAnimation import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringForce +import br.alexandregpereira.jerry.ANIMATION_STIFFNESS +import br.alexandregpereira.jerry.JerryAnimation +import br.alexandregpereira.jerry.JerryAnimationSet +import br.alexandregpereira.jerry.SpringAnimationPropertyKey +import br.alexandregpereira.jerry.force +import br.alexandregpereira.jerry.getSpringAnimation +import br.alexandregpereira.jerry.gone +import br.alexandregpereira.jerry.isVisible +import br.alexandregpereira.jerry.spring +import br.alexandregpereira.jerry.start +import br.alexandregpereira.jerry.visible /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the @@ -74,6 +85,10 @@ fun View.visibleFadeIn( ) } +fun View.isFadeOutRunning() = getSpringAnimation(SpringAnimationPropertyKey.ALPHA.id)?.run { + isRunning && spring?.finalPosition == 0f +} == true + fun JerryAnimationSet.fadeInSpring() = fadeSpring( targetValue = 1f ) diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/TransalationSpringAnimation.kt similarity index 84% rename from jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/TransalationSpringAnimation.kt index 35d1df9..e264362 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/TransalationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/TransalationSpringAnimation.kt @@ -1,8 +1,11 @@ -package br.alexandregpereira.jerry +package br.alexandregpereira.jerry.animation import android.view.View import androidx.dynamicanimation.animation.DynamicAnimation -import androidx.dynamicanimation.animation.SpringForce +import br.alexandregpereira.jerry.JerryAnimation +import br.alexandregpereira.jerry.JerryAnimationSet +import br.alexandregpereira.jerry.SpringAnimationPropertyKey +import br.alexandregpereira.jerry.spring fun JerryAnimation.translationXSpring( targetValue: Float diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimationCommon.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableAnimationCommon.kt similarity index 98% rename from jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimationCommon.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableAnimationCommon.kt index d7a539d..affef94 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableAnimationCommon.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableAnimationCommon.kt @@ -1,4 +1,4 @@ -package br.alexandregpereira.jerry.expandable +package br.alexandregpereira.jerry.animation.expandable import android.view.View import android.view.View.MeasureSpec diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableFadingSpringAnimation.kt similarity index 95% rename from jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableFadingSpringAnimation.kt index edf42b6..3b72523 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableFadingSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableFadingSpringAnimation.kt @@ -1,16 +1,16 @@ -package br.alexandregpereira.jerry.expandable +package br.alexandregpereira.jerry.animation.expandable import android.view.View import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringForce import br.alexandregpereira.jerry.ANIMATION_STIFFNESS -import br.alexandregpereira.jerry.fadeOutSpring -import br.alexandregpereira.jerry.fadeSpring +import br.alexandregpereira.jerry.animation.fadeOutSpring +import br.alexandregpereira.jerry.animation.fadeSpring import br.alexandregpereira.jerry.force -import br.alexandregpereira.jerry.goneFadeOut +import br.alexandregpereira.jerry.animation.goneFadeOut import br.alexandregpereira.jerry.isVisible import br.alexandregpereira.jerry.start -import br.alexandregpereira.jerry.visibleFadeIn +import br.alexandregpereira.jerry.animation.visibleFadeIn /** * Animates the View visibility depending of the [visible] flag. If [visible] is true, the diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableSpingAnimation.kt similarity index 99% rename from jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableSpingAnimation.kt index 617a46d..8c4cc59 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ExpandableSpingAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ExpandableSpingAnimation.kt @@ -1,4 +1,4 @@ -package br.alexandregpereira.jerry.expandable +package br.alexandregpereira.jerry.animation.expandable import android.view.View import androidx.dynamicanimation.animation.SpringAnimation diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/OriginalValueKey.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/OriginalValueKey.kt similarity index 83% rename from jerry/src/main/java/br/alexandregpereira/jerry/expandable/OriginalValueKey.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/OriginalValueKey.kt index 35fd771..ef22c73 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/OriginalValueKey.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/OriginalValueKey.kt @@ -1,4 +1,4 @@ -package br.alexandregpereira.jerry.expandable +package br.alexandregpereira.jerry.animation.expandable import androidx.annotation.StringRes import br.alexandregpereira.jerry.R diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ViewAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ViewAnimation.kt similarity index 96% rename from jerry/src/main/java/br/alexandregpereira/jerry/expandable/ViewAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ViewAnimation.kt index bc18c10..1250f02 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/ViewAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/ViewAnimation.kt @@ -1,4 +1,4 @@ -package br.alexandregpereira.jerry.expandable +package br.alexandregpereira.jerry.animation.expandable import android.view.View diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/WidthHeightViewProperty.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/WidthHeightViewProperty.kt similarity index 97% rename from jerry/src/main/java/br/alexandregpereira/jerry/expandable/WidthHeightViewProperty.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/WidthHeightViewProperty.kt index 349ec5f..bedc0e8 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/expandable/WidthHeightViewProperty.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/expandable/WidthHeightViewProperty.kt @@ -1,4 +1,4 @@ -package br.alexandregpereira.jerry.expandable +package br.alexandregpereira.jerry.animation.expandable import android.view.View import android.view.ViewGroup diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/textview/TextViewSpringAnimation.kt similarity index 86% rename from jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt rename to jerry/src/main/java/br/alexandregpereira/jerry/animation/textview/TextViewSpringAnimation.kt index 7180724..2864f19 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/textview/TextViewSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/textview/TextViewSpringAnimation.kt @@ -1,16 +1,16 @@ -package br.alexandregpereira.jerry.textview +package br.alexandregpereira.jerry.animation.textview import android.widget.TextView import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringForce import br.alexandregpereira.jerry.ANIMATION_STIFFNESS -import br.alexandregpereira.jerry.expandable.goneCollapseHeightFadeOut -import br.alexandregpereira.jerry.expandable.isCollapsingRunning -import br.alexandregpereira.jerry.expandable.visibleExpandHeightFadeIn -import br.alexandregpereira.jerry.fadeInSpring -import br.alexandregpereira.jerry.fadeOutSpring +import br.alexandregpereira.jerry.animation.expandable.goneCollapseHeightFadeOut +import br.alexandregpereira.jerry.animation.expandable.isCollapsingRunning +import br.alexandregpereira.jerry.animation.expandable.visibleExpandHeightFadeIn +import br.alexandregpereira.jerry.animation.fadeInSpring +import br.alexandregpereira.jerry.animation.fadeOutSpring import br.alexandregpereira.jerry.force -import br.alexandregpereira.jerry.isFadeOutRunning +import br.alexandregpereira.jerry.animation.isFadeOutRunning import br.alexandregpereira.jerry.isVisible import br.alexandregpereira.jerry.start diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt index 9cb9cd2..9eb2985 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt @@ -7,14 +7,14 @@ import androidx.recyclerview.widget.RecyclerView import br.alexandregpereira.jerry.ANIMATION_STIFFNESS import br.alexandregpereira.jerry.after import br.alexandregpereira.jerry.dpToPx -import br.alexandregpereira.jerry.elevationSpring -import br.alexandregpereira.jerry.fadeInSpring -import br.alexandregpereira.jerry.fadeOutSpring -import br.alexandregpereira.jerry.fadeSpring +import br.alexandregpereira.jerry.animation.elevationSpring +import br.alexandregpereira.jerry.animation.fadeInSpring +import br.alexandregpereira.jerry.animation.fadeOutSpring +import br.alexandregpereira.jerry.animation.fadeSpring import br.alexandregpereira.jerry.force import br.alexandregpereira.jerry.start -import br.alexandregpereira.jerry.translationXSpring -import br.alexandregpereira.jerry.translationYSpring +import br.alexandregpereira.jerry.animation.translationXSpring +import br.alexandregpereira.jerry.animation.translationYSpring @RequiresApi(21) class ElevationSpringItemAnimator( From 9ff5dd30f2dbc099b0c0f8b3abd9b5b53ba38b00 Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 02:48:13 -0300 Subject: [PATCH 09/14] Create translation section --- app/src/main/AndroidManifest.xml | 2 + .../jerry/app/MainActivity.kt | 5 ++ .../animation/TranslationAnimationActivity.kt | 82 +++++++++++++++++++ .../layout/activity_translation_animation.xml | 37 +++++++++ app/src/main/res/values/strings.xml | 2 + .../jerry/SpringAnimation.kt | 9 ++ 6 files changed, 137 insertions(+) create mode 100644 app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt create mode 100644 app/src/main/res/layout/activity_translation_animation.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3bb21c3..cfd9b56 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -36,6 +36,8 @@ + + \ No newline at end of file diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt index 78b29cd..0fb1597 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt @@ -13,6 +13,7 @@ import br.alexandregpereira.jerry.app.animation.ExpandFadingSpringAnimationActiv import br.alexandregpereira.jerry.app.animation.ExpandSpringAnimationActivity import br.alexandregpereira.jerry.app.animation.FadeSpringAnimationActivity import br.alexandregpereira.jerry.app.animation.TextExpandableSpringAnimationActivity +import br.alexandregpereira.jerry.app.animation.TranslationAnimationActivity import br.alexandregpereira.jerry.app.recyclerview.GridRecyclerViewActivity import br.alexandregpereira.jerry.app.recyclerview.GridRecyclerViewSpringActivity import br.alexandregpereira.jerry.app.recyclerview.RecyclerViewActivity @@ -60,6 +61,9 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { AnimationComponent.GRID_RECYCLER_SPRING.ordinal -> startActivity( GridRecyclerViewSpringActivity.getStartIntent(this@MainActivity) ) + AnimationComponent.TRANSLATION.ordinal -> startActivity( + TranslationAnimationActivity.getStartIntent(this@MainActivity) + ) } } } @@ -77,6 +81,7 @@ enum class AnimationComponent { EXPAND_FADING_SPRING, FADE_SPRING, TEXT_EXPANDABLE_SPRING, + TRANSLATION, RECYCLER, RECYCLER_SPRING, GRID_RECYCLER, diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt new file mode 100644 index 0000000..a9887fa --- /dev/null +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt @@ -0,0 +1,82 @@ +package br.alexandregpereira.jerry.app.animation + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.view.MotionEvent +import android.view.animation.AccelerateDecelerateInterpolator +import androidx.appcompat.app.AppCompatActivity +import br.alexandregpereira.jerry.animation.translationXSpring +import br.alexandregpereira.jerry.animation.translationYSpring +import br.alexandregpereira.jerry.animationSetForce +import br.alexandregpereira.jerry.app.R +import br.alexandregpereira.jerry.cancelSpringAnimation +import br.alexandregpereira.jerry.start +import kotlinx.android.synthetic.main.activity_translation_animation.* + +class TranslationAnimationActivity : AppCompatActivity(R.layout.activity_translation_animation) { + + private var isSpring = true + + companion object { + fun getStartIntent(context: Context): Intent { + return Intent(context, TranslationAnimationActivity::class.java) + } + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + container.viewTreeObserver.addOnGlobalLayoutListener { + val buttonHeight = moveButton.height + resources.getDimension(R.dimen.small) * 2 + val point1 = 0f to (container.height - buttonHeight - moveView.height) + val point2 = (container.width / 2f - moveView.width / 2) to (container.height / 2f - buttonHeight - moveView.height / 2) + val point3 = (container.width - moveView.width).toFloat() to point1.second + val point4 = point3.first to 0f + val point6 = 0f to 0f + val points = listOf>( + point1, point2, point3, point4, point2, point6 + ).circularIterator() + + moveButton.setOnClickListener { + isSpring = true + val point = points.next() + startSpringAnimation(point) + } + + moveNormalButton.setOnClickListener { + isSpring = false + val point = points.next() + startNormalAnimation(point) + } + } + } + + private fun startSpringAnimation(point: Pair) { + moveView.animate().cancel() + moveView.translationXSpring(targetValue = point.first) + .translationYSpring(targetValue = point.second) + .animationSetForce(stiffness = 60f, dampingRatio = 0.8f) + .start() + } + + private fun startNormalAnimation(point: Pair) { + moveView.cancelSpringAnimation() + moveView.animate() + .translationX(point.first) + .translationY(point.second) + .setInterpolator(AccelerateDecelerateInterpolator()) + .setDuration(500) + .start() + } + + override fun onTouchEvent(event: MotionEvent?): Boolean { + when (event?.actionMasked) { + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE -> { + val point = event.rawX - moveView.width / 2 to event.rawY - moveView.height / 2 + if (isSpring) startSpringAnimation(point) else startNormalAnimation(point) + } + } + return true + } +} diff --git a/app/src/main/res/layout/activity_translation_animation.xml b/app/src/main/res/layout/activity_translation_animation.xml new file mode 100644 index 0000000..64de67b --- /dev/null +++ b/app/src/main/res/layout/activity_translation_animation.xml @@ -0,0 +1,37 @@ + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d8a5d81..50565b4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -46,5 +46,7 @@ Remove Shuffle Change + Spring animation + Normal animation \ No newline at end of file diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt index 293c983..b4d22c5 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt @@ -97,6 +97,15 @@ fun JerryAnimationSet.force( } } +fun JerryAnimationSet.animationSetForce( + stiffness: Float, + dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY +): JerryAnimationSet { + return this.apply { + jerryAnimations.forEach { it.force(stiffness, dampingRatio) } + } +} + private fun JerryAnimation.animationSet(): JerryAnimationSet { return JerryAnimationSet(jerryAnimations = listOf(this)) } From 9d2ed4a8405c15a66e27b346c6b4b8d2a8f2a692 Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 03:01:20 -0300 Subject: [PATCH 10/14] create rotation spring animation --- .../jerry/SpringAnimationPropertyKey.kt | 3 + .../animation/RotationSpringAnimation.kt | 80 +++++++++++++++++++ jerry/src/main/res/values/strings.xml | 3 + 3 files changed, 86 insertions(+) create mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/animation/RotationSpringAnimation.kt diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt index fbaa6d0..ebe9929 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt @@ -7,6 +7,9 @@ enum class SpringAnimationPropertyKey(@StringRes val id: Int) { ELEVATION(R.string.elevation_spring_key), TRANSLATION_X(R.string.translation_x_spring_key), TRANSLATION_Y(R.string.translation_y_spring_key), + ROTATION(R.string.rotation_spring_key), + ROTATION_X(R.string.rotation_x_spring_key), + ROTATION_Y(R.string.rotation_y_spring_key), HEIGHT(R.string.expanding_collapsing_height_spring_key), WIDTH(R.string.expanding_collapsing_width_spring_key) } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animation/RotationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/RotationSpringAnimation.kt new file mode 100644 index 0000000..efa5b4d --- /dev/null +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/RotationSpringAnimation.kt @@ -0,0 +1,80 @@ +package br.alexandregpereira.jerry.animation + +import android.view.View +import androidx.dynamicanimation.animation.DynamicAnimation +import br.alexandregpereira.jerry.JerryAnimation +import br.alexandregpereira.jerry.JerryAnimationSet +import br.alexandregpereira.jerry.SpringAnimationPropertyKey +import br.alexandregpereira.jerry.spring + +fun JerryAnimation.rotationSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION.id, + property = DynamicAnimation.ROTATION, + targetValue = targetValue +) + +fun JerryAnimationSet.rotationSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION.id, + property = DynamicAnimation.ROTATION, + targetValue = targetValue +) + +fun View.rotationSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION.id, + property = DynamicAnimation.ROTATION, + targetValue = targetValue +) + +fun JerryAnimation.rotationXSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION_X.id, + property = DynamicAnimation.ROTATION_X, + targetValue = targetValue +) + +fun JerryAnimationSet.rotationXSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION_X.id, + property = DynamicAnimation.ROTATION_X, + targetValue = targetValue +) + +fun View.rotationXSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION_X.id, + property = DynamicAnimation.ROTATION_X, + targetValue = targetValue +) + +fun JerryAnimation.rotationYSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION_Y.id, + property = DynamicAnimation.ROTATION_Y, + targetValue = targetValue +) + +fun JerryAnimationSet.rotationYSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION_Y.id, + property = DynamicAnimation.ROTATION_Y, + targetValue = targetValue +) + +fun View.rotationYSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.ROTATION_Y.id, + property = DynamicAnimation.ROTATION_Y, + targetValue = targetValue +) diff --git a/jerry/src/main/res/values/strings.xml b/jerry/src/main/res/values/strings.xml index 7911ff0..740c034 100644 --- a/jerry/src/main/res/values/strings.xml +++ b/jerry/src/main/res/values/strings.xml @@ -8,6 +8,9 @@ elevation_spring_key translation_x_spring_key translation_y_spring_key + rotation_spring_key + rotation_x_spring_key + rotation_y_spring_key expanding_collapsing_height_original_value_key expanding_collapsing_height_spring_key From 76e7f1f9f08395e04d6d94c7033cab14287db52b Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 03:14:48 -0300 Subject: [PATCH 11/14] create scale spring animation --- .../jerry/SpringAnimationPropertyKey.kt | 2 + .../jerry/animation/ScaleSpringAnimation.kt | 56 +++++++++++++++++++ jerry/src/main/res/values/strings.xml | 2 + 3 files changed, 60 insertions(+) create mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/animation/ScaleSpringAnimation.kt diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt index ebe9929..5c35953 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimationPropertyKey.kt @@ -10,6 +10,8 @@ enum class SpringAnimationPropertyKey(@StringRes val id: Int) { ROTATION(R.string.rotation_spring_key), ROTATION_X(R.string.rotation_x_spring_key), ROTATION_Y(R.string.rotation_y_spring_key), + SCALE_X(R.string.scale_x_spring_key), + SCALE_Y(R.string.scale_y_spring_key), HEIGHT(R.string.expanding_collapsing_height_spring_key), WIDTH(R.string.expanding_collapsing_width_spring_key) } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animation/ScaleSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ScaleSpringAnimation.kt new file mode 100644 index 0000000..a42b5d7 --- /dev/null +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ScaleSpringAnimation.kt @@ -0,0 +1,56 @@ +package br.alexandregpereira.jerry.animation + +import android.view.View +import androidx.dynamicanimation.animation.DynamicAnimation +import br.alexandregpereira.jerry.JerryAnimation +import br.alexandregpereira.jerry.JerryAnimationSet +import br.alexandregpereira.jerry.SpringAnimationPropertyKey +import br.alexandregpereira.jerry.spring + +fun JerryAnimation.scaleXSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.SCALE_X.id, + property = DynamicAnimation.SCALE_X, + targetValue = targetValue +) + +fun JerryAnimationSet.scaleXSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.SCALE_X.id, + property = DynamicAnimation.SCALE_X, + targetValue = targetValue +) + +fun View.scaleXSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.SCALE_X.id, + property = DynamicAnimation.SCALE_X, + targetValue = targetValue +) + +fun JerryAnimation.scaleYSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.SCALE_Y.id, + property = DynamicAnimation.SCALE_Y, + targetValue = targetValue +) + +fun JerryAnimationSet.scaleYSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.SCALE_Y.id, + property = DynamicAnimation.SCALE_Y, + targetValue = targetValue +) + +fun View.scaleYSpring( + targetValue: Float +) = spring( + key = SpringAnimationPropertyKey.SCALE_Y.id, + property = DynamicAnimation.SCALE_Y, + targetValue = targetValue +) diff --git a/jerry/src/main/res/values/strings.xml b/jerry/src/main/res/values/strings.xml index 740c034..101d16d 100644 --- a/jerry/src/main/res/values/strings.xml +++ b/jerry/src/main/res/values/strings.xml @@ -11,6 +11,8 @@ rotation_spring_key rotation_x_spring_key rotation_y_spring_key + scale_x_spring_key + scale_y_spring_key expanding_collapsing_height_original_value_key expanding_collapsing_height_spring_key From cace170dc2fb3f0ae3f2cdcf2ae4f4fcb46a4fde Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 04:36:35 -0300 Subject: [PATCH 12/14] Implement scale animation on collpase animation on End --- app/build.gradle | 1 + .../CollapseSpringAnimationActivity.kt | 31 +++++++++++++++++-- .../jerry/SpringAnimation.kt | 26 ++++++++++------ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 01e90a8..95ddd63 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -35,4 +35,5 @@ dependencies { androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' implementation project(":jerry") + implementation 'androidx.dynamicanimation:dynamicanimation:1.0.0' } \ No newline at end of file diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt index a4727c6..4132d62 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt @@ -5,12 +5,20 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.core.view.ViewCompat +import androidx.dynamicanimation.animation.DynamicAnimation +import br.alexandregpereira.jerry.after import br.alexandregpereira.jerry.animation.expandable.visibleOrGoneExpandableWidth import br.alexandregpereira.jerry.app.R import br.alexandregpereira.jerry.animation.expandable.goneCollapseHeight import br.alexandregpereira.jerry.animation.expandable.goneCollapseWidth import br.alexandregpereira.jerry.animation.expandable.visibleExpandHeight import br.alexandregpereira.jerry.animation.expandable.visibleExpandWidth +import br.alexandregpereira.jerry.animation.scaleXSpring +import br.alexandregpereira.jerry.animation.scaleYSpring +import br.alexandregpereira.jerry.animationSetForce +import br.alexandregpereira.jerry.cancelSpringAnimation +import br.alexandregpereira.jerry.spring +import br.alexandregpereira.jerry.start import kotlinx.android.synthetic.main.activity_collapse_animation.* import kotlinx.android.synthetic.main.container_animation_info.view.* @@ -42,7 +50,7 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll collapseAnimationInfo.percentageTextView.text = (interpolatedTime * 100).toInt().toString() } ) { - collapseAnimationInfo.countTextView.text = collapseTextViewCount++.toString() + startScaleAnimation() } } @@ -52,7 +60,7 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll collapseAnimationInfo.percentageTextView.text = (interpolatedTime * 100).toInt().toString() } ) { - collapseAnimationInfo.countTextView.text = collapseTextViewCount++.toString() + startScaleAnimation() } } @@ -96,4 +104,23 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll collapseFixedWidthView.visibleExpandWidth() } } + + private fun startScaleAnimation() { + collapseAnimationInfo.countTextView.apply { + text = collapseTextViewCount++.toString() + cancelSpringAnimation() + .scaleXSpring(targetValue = 0.8f) + .scaleYSpring(targetValue = 0.8f) + .after( + spring(key = id, targetValue = 1f, property = DynamicAnimation.SCALE_X) + .spring( + key = collapseAnimationInfo.id, + targetValue = 1f, + property = DynamicAnimation.SCALE_Y + ) + .animationSetForce(dampingRatio = 0.15f) + ) + .start() + } + } } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt index b4d22c5..c0862c6 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt @@ -8,24 +8,28 @@ import androidx.dynamicanimation.animation.SpringForce const val ANIMATION_STIFFNESS = 600f -fun View.cancelSpringAnimation() { +fun View.cancelSpringAnimation(): View { getSpringAnimationKeys().forEach { cancelSpringAnimation(key = it) } + return this } -fun View.cancelSpringAnimation(key: Int) { +fun View.cancelSpringAnimation(key: Int): View { getSpringAnimation(key)?.cancel() + return this } -fun View.skipToEndSpringAnimation() { +fun View.skipToEndSpringAnimation(): View { getSpringAnimationKeys().forEach { skipToEndSpringAnimation(key = it) } + return this } -fun View.skipToEndSpringAnimation(key: Int) { +fun View.skipToEndSpringAnimation(key: Int): View { getSpringAnimation(key)?.takeIf { it.canSkipToEnd() }?.skipToEnd() + return this } /** @@ -70,14 +74,16 @@ fun JerryAnimationSet.spring( ): JerryAnimationSet { return this.copy( jerryAnimations = jerryAnimations + - listOf(jerryAnimations.last().view.spring( - key, property, targetValue - )) + listOf( + jerryAnimations.last().view.spring( + key, property, targetValue + ) + ) ) } fun JerryAnimation.force( - stiffness: Float, + stiffness: Float = ANIMATION_STIFFNESS, dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimation { return this.apply { @@ -89,7 +95,7 @@ fun JerryAnimation.force( } fun JerryAnimationSet.force( - stiffness: Float, + stiffness: Float = ANIMATION_STIFFNESS, dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { return this.apply { @@ -98,7 +104,7 @@ fun JerryAnimationSet.force( } fun JerryAnimationSet.animationSetForce( - stiffness: Float, + stiffness: Float = ANIMATION_STIFFNESS, dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { return this.apply { From 4be1c5e4337a24f49a4bc9203441f7604147fec6 Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 04:46:48 -0300 Subject: [PATCH 13/14] Add add and lastForce function. Change animationSetForce to force --- .../CollapseSpringAnimationActivity.kt | 4 ++-- .../animation/TranslationAnimationActivity.kt | 4 ++-- .../alexandregpereira/jerry/SpringAnimation.kt | 18 +++++++++++++++--- .../animator/ElevationSpringItemAnimator.kt | 11 ++++++----- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt index 4132d62..56941a5 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/CollapseSpringAnimationActivity.kt @@ -15,7 +15,7 @@ import br.alexandregpereira.jerry.animation.expandable.visibleExpandHeight import br.alexandregpereira.jerry.animation.expandable.visibleExpandWidth import br.alexandregpereira.jerry.animation.scaleXSpring import br.alexandregpereira.jerry.animation.scaleYSpring -import br.alexandregpereira.jerry.animationSetForce +import br.alexandregpereira.jerry.force import br.alexandregpereira.jerry.cancelSpringAnimation import br.alexandregpereira.jerry.spring import br.alexandregpereira.jerry.start @@ -118,7 +118,7 @@ class CollapseSpringAnimationActivity : AppCompatActivity(R.layout.activity_coll targetValue = 1f, property = DynamicAnimation.SCALE_Y ) - .animationSetForce(dampingRatio = 0.15f) + .force(dampingRatio = 0.15f) ) .start() } diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt index a9887fa..7a64c94 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/animation/TranslationAnimationActivity.kt @@ -8,7 +8,7 @@ import android.view.animation.AccelerateDecelerateInterpolator import androidx.appcompat.app.AppCompatActivity import br.alexandregpereira.jerry.animation.translationXSpring import br.alexandregpereira.jerry.animation.translationYSpring -import br.alexandregpereira.jerry.animationSetForce +import br.alexandregpereira.jerry.force import br.alexandregpereira.jerry.app.R import br.alexandregpereira.jerry.cancelSpringAnimation import br.alexandregpereira.jerry.start @@ -56,7 +56,7 @@ class TranslationAnimationActivity : AppCompatActivity(R.layout.activity_transla moveView.animate().cancel() moveView.translationXSpring(targetValue = point.first) .translationYSpring(targetValue = point.second) - .animationSetForce(stiffness = 60f, dampingRatio = 0.8f) + .force(stiffness = 60f, dampingRatio = 0.8f) .start() } diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt index c0862c6..4d1ab9a 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/SpringAnimation.kt @@ -99,16 +99,16 @@ fun JerryAnimationSet.force( dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { return this.apply { - jerryAnimations.last().force(stiffness, dampingRatio) + jerryAnimations.forEach { it.force(stiffness, dampingRatio) } } } -fun JerryAnimationSet.animationSetForce( +fun JerryAnimationSet.lastForce( stiffness: Float = ANIMATION_STIFFNESS, dampingRatio: Float = SpringForce.DAMPING_RATIO_NO_BOUNCY ): JerryAnimationSet { return this.apply { - jerryAnimations.forEach { it.force(stiffness, dampingRatio) } + jerryAnimations.last().force(stiffness, dampingRatio) } } @@ -132,6 +132,18 @@ fun JerryAnimationSet.after(jerryAnimation: JerryAnimation): JerryAnimationSet { return this.copy(jerryAfterAnimationSet = jerryAnimation.animationSet()) } +fun JerryAnimation.add( + jerryAnimationSet: JerryAnimationSet +): JerryAnimationSet { + return animationSet().add(jerryAnimationSet) +} + +fun JerryAnimationSet.add( + jerryAnimationSet: JerryAnimationSet +): JerryAnimationSet { + return this.copy(jerryAnimations = this.jerryAnimations + jerryAnimationSet.jerryAnimations) +} + fun JerryAnimationSet.start( onAnimationEnd: ((canceled: Boolean) -> Unit)? = null ) { diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt index 9eb2985..b796afd 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animator/ElevationSpringItemAnimator.kt @@ -5,6 +5,7 @@ import androidx.annotation.RequiresApi import androidx.dynamicanimation.animation.SpringForce.DAMPING_RATIO_NO_BOUNCY import androidx.recyclerview.widget.RecyclerView import br.alexandregpereira.jerry.ANIMATION_STIFFNESS +import br.alexandregpereira.jerry.add import br.alexandregpereira.jerry.after import br.alexandregpereira.jerry.dpToPx import br.alexandregpereira.jerry.animation.elevationSpring @@ -71,7 +72,6 @@ class ElevationSpringItemAnimator( val translationYTargetValue = if (deltaY != 0) translationOrigin else translationY translationXSpring(translationXTargetValue) - .force(translationStiffness, translationDampingRatio) .translationYSpring(translationYTargetValue) .force(translationStiffness, translationDampingRatio) .start { canceled -> @@ -128,10 +128,11 @@ class ElevationSpringItemAnimator( .after( fadeSpring(alphaTargetValue) .force(alphaStiffness, alphaDampingRatio) - .translationXSpring(translationXTargetValue) - .force(translationStiffness, translationDampingRatio) - .translationYSpring(translationYTargetValue) - .force(translationStiffness, translationDampingRatio) + .add( + translationXSpring(translationXTargetValue) + .translationYSpring(translationYTargetValue) + .force(translationStiffness, translationDampingRatio) + ) .after( elevationSpring(targetValue = elevationFull) .force(elevationStiffness, elevationDampingRatio) From 83740085e1c1b4bf956ea4308708dc0f51dbc57f Mon Sep 17 00:00:00 2001 From: Alexandre Pereira Date: Tue, 20 Oct 2020 17:25:54 -0300 Subject: [PATCH 14/14] Add maven publish and recycler scroll animation --- .../jerry/app/MainActivity.kt | 5 ++-- jerry/build.gradle | 19 +++++++++++++++ .../java/br/alexandregpereira/jerry/Scroll.kt | 18 +++++++++++++++ .../animation/ElevationSpringAnimation.kt | 23 +++++++++++++++++++ 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 jerry/src/main/java/br/alexandregpereira/jerry/Scroll.kt diff --git a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt index 0fb1597..e035a4e 100644 --- a/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt +++ b/app/src/main/java/br/alexandregpereira/jerry/app/MainActivity.kt @@ -7,6 +7,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import br.alexandregpereira.jerry.animation.setupLiftViewOnScrollCompat import br.alexandregpereira.jerry.app.animation.CollapseFadingSpringAnimationActivity import br.alexandregpereira.jerry.app.animation.CollapseSpringAnimationActivity import br.alexandregpereira.jerry.app.animation.ExpandFadingSpringAnimationActivity @@ -19,14 +20,13 @@ import br.alexandregpereira.jerry.app.recyclerview.GridRecyclerViewSpringActivit import br.alexandregpereira.jerry.app.recyclerview.RecyclerViewActivity import br.alexandregpereira.jerry.app.recyclerview.RecyclerViewSpringActivity import br.alexandregpereira.jerry.app.widgets.configMaterialShapeDrawable +import br.alexandregpereira.jerry.dpToPx import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity(R.layout.activity_main) { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - appBarLayout.isLiftOnScroll = true - componentsRecycler.apply { layoutManager = LinearLayoutManager(this@MainActivity) adapter = MainAdapter(getAnimationComponentsName()) { @@ -67,6 +67,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) { } } } + componentsRecycler.setupLiftViewOnScrollCompat(appBarLayout, 2f.dpToPx(resources)) } } diff --git a/jerry/build.gradle b/jerry/build.gradle index 20ee522..2cd07fd 100644 --- a/jerry/build.gradle +++ b/jerry/build.gradle @@ -2,6 +2,7 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'com.github.dcendents.android-maven' +apply plugin: 'maven-publish' group = 'com.github.alexandregpereira' @@ -29,4 +30,22 @@ dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.recyclerview:recyclerview:1.1.0' implementation 'androidx.dynamicanimation:dynamicanimation:1.0.0' +} + +project.afterEvaluate { + publishing { + publications { + library(MavenPublication) { + setGroupId groupId + setArtifactId 'jerry' + version versionSuffix() + + artifact bundleReleaseAar + } + } + } +} + +static def versionSuffix() { + return new Date().format('yyyyMMddHHmmss') } \ No newline at end of file diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/Scroll.kt b/jerry/src/main/java/br/alexandregpereira/jerry/Scroll.kt new file mode 100644 index 0000000..63d98e7 --- /dev/null +++ b/jerry/src/main/java/br/alexandregpereira/jerry/Scroll.kt @@ -0,0 +1,18 @@ +package br.alexandregpereira.jerry + +import androidx.recyclerview.widget.RecyclerView + +fun RecyclerView.addOnScrolled(block: () -> Unit) { + this.addOnScrolledWithPixels { _, _ -> + block() + } +} + +fun RecyclerView.addOnScrolledWithPixels(block: (dx: Int, dy: Int) -> Unit) { + this.addOnScrollListener(object : RecyclerView.OnScrollListener() { + + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + block(dx, dy) + } + }) +} diff --git a/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt index 6199247..a163f6f 100644 --- a/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt +++ b/jerry/src/main/java/br/alexandregpereira/jerry/animation/ElevationSpringAnimation.kt @@ -3,10 +3,14 @@ package br.alexandregpereira.jerry.animation import android.view.View import androidx.annotation.RequiresApi import androidx.dynamicanimation.animation.FloatPropertyCompat +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import br.alexandregpereira.jerry.JerryAnimation import br.alexandregpereira.jerry.JerryAnimationSet import br.alexandregpereira.jerry.SpringAnimationPropertyKey +import br.alexandregpereira.jerry.addOnScrolled import br.alexandregpereira.jerry.spring +import br.alexandregpereira.jerry.start @RequiresApi(21) fun JerryAnimationSet.elevationSpring( @@ -35,6 +39,25 @@ fun View.elevationSpring( targetValue = targetValue ) +@RequiresApi(21) +fun RecyclerView.setupLiftViewOnScroll(liftView: View, elevation: Float) { + val layoutManager = layoutManager as? LinearLayoutManager ?: return + addOnScrolled { + val animation = if (layoutManager.findFirstCompletelyVisibleItemPosition() != 0) { + liftView.elevationSpring(targetValue = elevation) + } else { + liftView.elevationSpring(targetValue = 0f) + } + animation.start() + } +} + +fun RecyclerView.setupLiftViewOnScrollCompat(liftView: View, elevation: Float) { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { + this.setupLiftViewOnScroll(liftView, elevation) + } +} + @RequiresApi(21) fun elevationViewProperty() = object : FloatPropertyCompat( "viewProperty"