Animate swapping of play/pause buttons.

This commit is contained in:
Nicholas
2022-12-21 17:23:36 -05:00
committed by Greyson Parrelli
parent dd3bad858d
commit d471647e12
5 changed files with 112 additions and 14 deletions

View File

@@ -4,11 +4,14 @@ import android.animation.Animator
import android.animation.Animator.AnimatorListener
import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.PathInterpolator
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageButton
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.airbnb.lottie.LottieAnimationView
@@ -122,3 +125,66 @@ class LottieAnimatedButton @JvmOverloads constructor(
playAnimation()
}
}
class AnimatedInOutImageButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : AppCompatImageButton(context, attrs) {
private val rotationWhenVisible: Float
private val rotationWhenHidden: Float
init {
val styledAttrs = getContext().obtainStyledAttributes(attrs, R.styleable.AnimatedInOutImageButton)
rotationWhenVisible = styledAttrs.getFloat(R.styleable.AnimatedInOutImageButton_rotationWhenVisible, 0f)
rotationWhenHidden = styledAttrs.getFloat(R.styleable.AnimatedInOutImageButton_rotationWhenHidden, 0f)
styledAttrs.recycle()
}
override fun setVisibility(visibility: Int) {
if (visibility == VISIBLE) {
super.setVisibility(visibility)
animateIn()
} else {
animateOut { super.setVisibility(visibility) }
}
}
private fun animateIn() {
this.rotation = rotationWhenHidden
this.scaleX = 0.5f
this.scaleY = 0.5f
this.alpha = 0f
val animator = this.animate()
.setDuration(animationDurationMs)
.alpha(1f)
.rotation(rotationWhenVisible)
.scaleX(1f)
.scaleY(1f)
if (Build.VERSION.SDK_INT >= 21) {
animator.interpolator = PathInterpolator(0.4f, 0.0f, 0.2f, 1f)
}
animator.start()
}
private fun animateOut(endAction: Runnable) {
val animator = this.animate()
.setDuration(animationDurationMs)
.alpha(0f)
.rotation(rotationWhenHidden)
.scaleX(0.5f)
.scaleY(0.5f)
.withEndAction(endAction)
if (Build.VERSION.SDK_INT >= 21) {
animator.interpolator = PathInterpolator(0.4f, 0.0f, 0.2f, 1f)
}
animator.start()
}
companion object {
const val animationDurationMs: Long = 150
}
}