ui: revamp item drag anim
This commit is contained in:
@@ -23,8 +23,10 @@ import android.graphics.drawable.Drawable
|
|||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.animation.AccelerateDecelerateInterpolator
|
import android.view.animation.AccelerateDecelerateInterpolator
|
||||||
import androidx.core.view.isInvisible
|
import androidx.core.view.isInvisible
|
||||||
|
import androidx.dynamicanimation.animation.SpringAnimation
|
||||||
import androidx.recyclerview.widget.ItemTouchHelper
|
import androidx.recyclerview.widget.ItemTouchHelper
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.google.android.material.shape.MaterialShapeDrawable
|
||||||
import com.google.android.material.R as MR
|
import com.google.android.material.R as MR
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
@@ -32,6 +34,8 @@ import kotlin.math.min
|
|||||||
import kotlin.math.sign
|
import kotlin.math.sign
|
||||||
import org.oxycblt.auxio.R
|
import org.oxycblt.auxio.R
|
||||||
import org.oxycblt.auxio.list.recycler.MaterialDragCallback.ViewHolder
|
import org.oxycblt.auxio.list.recycler.MaterialDragCallback.ViewHolder
|
||||||
|
import org.oxycblt.auxio.ui.Effect
|
||||||
|
import org.oxycblt.auxio.ui.Spatial
|
||||||
import org.oxycblt.auxio.util.getDimen
|
import org.oxycblt.auxio.util.getDimen
|
||||||
import org.oxycblt.auxio.util.getInteger
|
import org.oxycblt.auxio.util.getInteger
|
||||||
import timber.log.Timber as L
|
import timber.log.Timber as L
|
||||||
@@ -44,7 +48,29 @@ import timber.log.Timber as L
|
|||||||
* @author Alexander Capehart (OxygenCobalt)
|
* @author Alexander Capehart (OxygenCobalt)
|
||||||
*/
|
*/
|
||||||
abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
|
abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
|
||||||
|
data class AnimBundle(
|
||||||
|
val cornerAnim: SpringAnimation,
|
||||||
|
val elevationAnim: SpringAnimation,
|
||||||
|
val translateZAnim: SpringAnimation,
|
||||||
|
val alphaAnim: SpringAnimation,
|
||||||
|
val causedBy: CausedBy
|
||||||
|
) {
|
||||||
|
fun cancel() {
|
||||||
|
cornerAnim.cancel()
|
||||||
|
elevationAnim.cancel()
|
||||||
|
translateZAnim.cancel()
|
||||||
|
alphaAnim.cancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
enum class CausedBy {
|
||||||
|
CLICK,
|
||||||
|
RELEASE
|
||||||
|
}
|
||||||
private var shouldLift = true
|
private var shouldLift = true
|
||||||
|
private val itemCornerSpring = Spatial.DEFAULT
|
||||||
|
private val itemAlphaSpring = Effect.DEFAULT
|
||||||
|
private val itemTranslateZString = Spatial.DEFAULT
|
||||||
|
private val itemAnims = mutableMapOf<View, AnimBundle>()
|
||||||
|
|
||||||
final override fun getMovementFlags(
|
final override fun getMovementFlags(
|
||||||
recyclerView: RecyclerView,
|
recyclerView: RecyclerView,
|
||||||
@@ -99,24 +125,22 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
|
|||||||
|
|
||||||
// Hook drag events to "lifting" the item (i.e raising it's elevation). Make sure
|
// Hook drag events to "lifting" the item (i.e raising it's elevation). Make sure
|
||||||
// this is only done once when the item is initially picked up.
|
// this is only done once when the item is initially picked up.
|
||||||
if (shouldLift && isCurrentlyActive && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
|
if (isCurrentlyActive && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
|
||||||
L.d("Lifting ViewHolder")
|
L.d("Lifting ViewHolder")
|
||||||
|
val bg = holder.background as MaterialShapeDrawable
|
||||||
val bg = holder.background
|
|
||||||
val elevation = recyclerView.context.getDimen(MR.dimen.m3_sys_elevation_level4)
|
val elevation = recyclerView.context.getDimen(MR.dimen.m3_sys_elevation_level4)
|
||||||
holder.root
|
val cornerSize = recyclerView.context.getDimen(R.dimen.spacing_mid_large)
|
||||||
.animate()
|
val itemAnim = itemAnims[holder.root]
|
||||||
.translationZ(elevation)
|
if (itemAnim?.causedBy != CausedBy.CLICK) {
|
||||||
.setDuration(
|
itemAnim?.cancel()
|
||||||
recyclerView.context.getInteger(R.integer.anim_fade_exit_duration).toLong()
|
itemAnims[holder.root] = AnimBundle(
|
||||||
|
cornerAnim = itemCornerSpring.corners(holder.root.context, bg, cornerSize),
|
||||||
|
elevationAnim = itemCornerSpring.elevation(holder.root.context, bg, elevation),
|
||||||
|
translateZAnim = itemTranslateZString.translateZ(holder.root, elevation),
|
||||||
|
alphaAnim = itemAlphaSpring.alpha(holder.root.context, bg, 255),
|
||||||
|
causedBy = CausedBy.CLICK
|
||||||
)
|
)
|
||||||
.setUpdateListener {
|
}
|
||||||
bg.alpha = ((holder.root.translationZ / elevation) * 255).toInt()
|
|
||||||
}
|
|
||||||
.setInterpolator(AccelerateDecelerateInterpolator())
|
|
||||||
.start()
|
|
||||||
|
|
||||||
shouldLift = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We show a background with a delete icon behind the item each time one is swiped
|
// We show a background with a delete icon behind the item each time one is swiped
|
||||||
@@ -144,20 +168,18 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
|
|||||||
// translationZ is already non-zero.
|
// translationZ is already non-zero.
|
||||||
if (holder.root.translationZ != 0f) {
|
if (holder.root.translationZ != 0f) {
|
||||||
L.d("Lifting ViewHolder")
|
L.d("Lifting ViewHolder")
|
||||||
|
val bg = holder.background as MaterialShapeDrawable
|
||||||
val bg = holder.background
|
val itemAnim = itemAnims[holder.root]
|
||||||
val elevation = recyclerView.context.getDimen(MR.dimen.m3_sys_elevation_level4)
|
if (itemAnim?.causedBy != CausedBy.RELEASE) {
|
||||||
holder.root
|
itemAnim?.cancel()
|
||||||
.animate()
|
itemAnims[holder.root] = AnimBundle(
|
||||||
.translationZ(0f)
|
cornerAnim = itemCornerSpring.corners(holder.root.context, bg, 0f),
|
||||||
.setDuration(
|
elevationAnim = itemCornerSpring.elevation(holder.root.context, bg, 0f),
|
||||||
recyclerView.context.getInteger(R.integer.anim_fade_exit_duration).toLong()
|
translateZAnim = itemTranslateZString.translateZ(holder.root, 0f),
|
||||||
|
alphaAnim = itemAlphaSpring.alpha(holder.root.context, bg, 0),
|
||||||
|
causedBy = CausedBy.RELEASE
|
||||||
)
|
)
|
||||||
.setUpdateListener {
|
}
|
||||||
bg.alpha = ((holder.root.translationZ / elevation) * 255).toInt()
|
|
||||||
}
|
|
||||||
.setInterpolator(AccelerateDecelerateInterpolator())
|
|
||||||
.start()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shouldLift = true
|
shouldLift = true
|
||||||
|
|||||||
@@ -19,13 +19,16 @@
|
|||||||
package org.oxycblt.auxio.ui
|
package org.oxycblt.auxio.ui
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.graphics.drawable.Drawable
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.annotation.AttrRes
|
import androidx.annotation.AttrRes
|
||||||
import androidx.annotation.StyleRes
|
import androidx.annotation.StyleRes
|
||||||
|
import androidx.core.view.isInvisible
|
||||||
import androidx.dynamicanimation.animation.FloatValueHolder
|
import androidx.dynamicanimation.animation.FloatValueHolder
|
||||||
import androidx.dynamicanimation.animation.SpringAnimation
|
import androidx.dynamicanimation.animation.SpringAnimation
|
||||||
import com.google.android.material.R as MR
|
import com.google.android.material.R as MR
|
||||||
import com.google.android.material.motion.MotionUtils
|
import com.google.android.material.motion.MotionUtils
|
||||||
|
import com.google.android.material.shape.MaterialShapeDrawable
|
||||||
import org.oxycblt.auxio.util.scale
|
import org.oxycblt.auxio.util.scale
|
||||||
|
|
||||||
class Spatial private constructor(@AttrRes val attr: Int, val defaultStyle: Int) {
|
class Spatial private constructor(@AttrRes val attr: Int, val defaultStyle: Int) {
|
||||||
@@ -62,6 +65,51 @@ class Spatial private constructor(@AttrRes val attr: Int, val defaultStyle: Int)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun translateZ(view: View, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation {
|
||||||
|
val from = view.translationZ
|
||||||
|
val springForce = resolve(view.context)
|
||||||
|
return SpringAnimation(FloatValueHolder(from)).apply {
|
||||||
|
spring = springForce
|
||||||
|
setStartValue(from)
|
||||||
|
setMinimumVisibleChange(0.0001f)
|
||||||
|
addUpdateListener { _, value, _ -> view.translationZ = value }
|
||||||
|
addEndListener { _, canceled, value, _ ->
|
||||||
|
view.translationZ = if (!canceled || jumpOnCancellation) to else value
|
||||||
|
}
|
||||||
|
animateToFinalPosition(to)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun elevation(context: Context, drawable: MaterialShapeDrawable, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation {
|
||||||
|
val from = drawable.elevation
|
||||||
|
val springForce = resolve(context)
|
||||||
|
return SpringAnimation(FloatValueHolder(from)).apply {
|
||||||
|
spring = springForce
|
||||||
|
setStartValue(from)
|
||||||
|
setMinimumVisibleChange(0.0001f)
|
||||||
|
addUpdateListener { _, value, _ -> drawable.elevation = value }
|
||||||
|
addEndListener { _, canceled, value, _ ->
|
||||||
|
drawable.elevation = if (!canceled || jumpOnCancellation) to else value
|
||||||
|
}
|
||||||
|
animateToFinalPosition(to)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun corners(context: Context, drawable: MaterialShapeDrawable, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation {
|
||||||
|
val from = drawable.topRightCornerResolvedSize
|
||||||
|
val springForce = resolve(context)
|
||||||
|
return SpringAnimation(FloatValueHolder(from)).apply {
|
||||||
|
spring = springForce
|
||||||
|
setStartValue(from)
|
||||||
|
setMinimumVisibleChange(0.0001f)
|
||||||
|
addUpdateListener { _, value, _ -> drawable.setCornerSize(value) }
|
||||||
|
addEndListener { _, canceled, value, _ ->
|
||||||
|
drawable.setCornerSize(if (!canceled || jumpOnCancellation) to else value)
|
||||||
|
}
|
||||||
|
animateToFinalPosition(to)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val FAST =
|
val FAST =
|
||||||
Spatial(
|
Spatial(
|
||||||
@@ -99,6 +147,21 @@ class Effect private constructor(@AttrRes val attr: Int, @StyleRes val defaultSt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun alpha(context: Context, drawable: Drawable, to: Int, jumpOnCancellation: Boolean = false): SpringAnimation {
|
||||||
|
val from = drawable.alpha
|
||||||
|
val springForce = resolve(context)
|
||||||
|
return SpringAnimation(FloatValueHolder(from.toFloat())).apply {
|
||||||
|
spring = springForce
|
||||||
|
setStartValue(from.toFloat())
|
||||||
|
setMinimumVisibleChange(1f / 255f)
|
||||||
|
addUpdateListener { _, value, _ -> drawable.alpha = value.toInt() }
|
||||||
|
addEndListener { _, canceled, value, _ ->
|
||||||
|
drawable.alpha = (if (!canceled || jumpOnCancellation) to else value).toInt()
|
||||||
|
}
|
||||||
|
animateToFinalPosition(to.toFloat())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
val DEFAULT =
|
val DEFAULT =
|
||||||
Effect(
|
Effect(
|
||||||
|
|||||||
Reference in New Issue
Block a user