Видимая анимация кнопок поверх AVD
This commit is contained in:
1 parent
0b22117e08
commit
dd19e21323
1 file changed
+48
-6
@@ -1,7 +1,10 @@
|
||||
package com.example.helponizer
|
||||
|
||||
import android.animation.AnimatorSet
|
||||
import android.animation.ObjectAnimator
|
||||
import android.graphics.drawable.AnimatedVectorDrawable
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.graphics.drawable.LayerDrawable
|
||||
import android.view.View
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.vectordrawable.graphics.drawable.AnimatedVectorDrawableCompat
|
||||
@@ -12,18 +15,57 @@ object IconAnimator {
|
||||
private val animatables = WeakHashMap<MaterialButton, Drawable>()
|
||||
|
||||
fun setup(button: MaterialButton, @DrawableRes resId: Int) {
|
||||
val drawable = AnimatedVectorDrawableCompat.create(button.context, resId)
|
||||
button.icon = drawable
|
||||
if (drawable != null) {
|
||||
animatables[button] = drawable
|
||||
}
|
||||
val avd = AnimatedVectorDrawableCompat.create(button.context, resId)
|
||||
button.icon = avd
|
||||
val drawn = findAnimatedVector(button.icon) ?: avd ?: return
|
||||
animatables[button] = drawn
|
||||
}
|
||||
|
||||
fun start(view: View) {
|
||||
val drawable = animatables[view as? MaterialButton] ?: return
|
||||
val drawable = animatables[view as? MaterialButton]
|
||||
when (drawable) {
|
||||
is AnimatedVectorDrawableCompat -> drawable.start()
|
||||
is AnimatedVectorDrawable -> drawable.start()
|
||||
}
|
||||
animateView(view)
|
||||
}
|
||||
|
||||
private fun animateView(view: View) {
|
||||
AnimatorSet().apply {
|
||||
playTogether(
|
||||
ObjectAnimator.ofFloat(view, View.SCALE_X, 1f, 1.06f, 1f),
|
||||
ObjectAnimator.ofFloat(view, View.SCALE_Y, 1f, 1.06f, 1f)
|
||||
)
|
||||
duration = 220
|
||||
start()
|
||||
}
|
||||
}
|
||||
|
||||
private fun findAnimatedVector(drawable: Drawable?): Drawable? {
|
||||
if (drawable == null) return null
|
||||
if (drawable is AnimatedVectorDrawableCompat || drawable is AnimatedVectorDrawable) {
|
||||
return drawable
|
||||
}
|
||||
if (drawable is LayerDrawable) {
|
||||
for (i in 0 until drawable.numberOfLayers) {
|
||||
val found = findAnimatedVector(drawable.getDrawable(i))
|
||||
if (found != null) return found
|
||||
}
|
||||
}
|
||||
var clazz: Class<*>? = drawable.javaClass
|
||||
while (clazz != null) {
|
||||
try {
|
||||
val method = clazz.getDeclaredMethod("getDrawable")
|
||||
method.isAccessible = true
|
||||
val inner = method.invoke(drawable) as? Drawable
|
||||
if (inner != null && inner !== drawable) {
|
||||
val found = findAnimatedVector(inner)
|
||||
if (found != null) return found
|
||||
}
|
||||
} catch (_: Throwable) {
|
||||
}
|
||||
clazz = clazz.superclass
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user