Animating layer lists (https://blog.jakelee.co.uk/programmatically-creating-and-scheduling-animations-for-android-drawable-layers-with-objectanimator/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private var animatorSet: AnimatorSet? = null | |
| private fun startAnimation(target: ImageView) { | |
| val animationLayers = target.drawable as LayerDrawable | |
| val moveImageViewLeft = ObjectAnimator.ofFloat(target, View.TRANSLATION_X, -200f).setDuration(6000) | |
| val moveImageViewRight = ObjectAnimator.ofFloat(target, View.TRANSLATION_X, 0f).setDuration(6000) | |
| val redCircle = animationLayers.findDrawableByLayerId(R.id.red_circle) as GradientDrawable | |
| val redCircleFadeOut = ObjectAnimator.ofInt(redCircle, "alpha", 255, 100).setDuration(1000) | |
| val redCircleFadeIn = ObjectAnimator.ofInt(redCircle, "alpha", 100, 255).setDuration(100) | |
| val icon = animationLayers.findDrawableByLayerId(R.id.icon) as RotateDrawable | |
| val iconRotateLeft = ObjectAnimator.ofInt(icon, "level", 10000, 1000).setDuration(3000) | |
| val iconRotateRight = ObjectAnimator.ofInt(icon, "level", 1000, 10000).setDuration(300) | |
| val animationReset = { target.translationX = 0f } | |
| animatorSet?.cancel() | |
| animatorSet = AnimatorSet().apply { | |
| play(moveImageViewLeft) | |
| .with(redCircleFadeOut) | |
| .with(iconRotateLeft) | |
| play(redCircleFadeIn) | |
| .with(iconRotateRight) | |
| .after(iconRotateLeft) | |
| play(moveImageViewRight) | |
| .after(moveImageViewLeft) | |
| doOnCancel { animationReset.invoke() } | |
| start() | |
| } | |
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
| <item android:id="@+id/red_circle"> | |
| <shape android:shape="oval" > | |
| <solid android:color="@android:color/holo_red_dark" /> | |
| </shape> | |
| </item> | |
| <item android:id="@+id/icon"> | |
| <rotate android:drawable="@drawable/ic_launcher_foreground" /> | |
| </item> | |
| </layer-list> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment