私はAndroidStudioを初めて使用するので、複数の座標で複数の画像を同時にアニメーション化したいと思います。
画像Aを0/0から100/100に移動し、次に200/200に移動したいとします。同時に(ボタンをクリックするなどして)、画像Bを0/100から100/100に移動し、次に200/100に移動します。すべて簡単な翻訳で。
(プラスとして、動きの持続時間を独立して設定できるようにしたいと思います。)
アニメーションが終了すると、スタートボタンを再度有効にするなど、他のことをトリガーするイベント(OnAnimationFinishなど)が発生するはずです。
これを行うための最も効果的な方法は何でしょうか?Androidに複数のアニメーションを保存するAnimationSetがあることは知っていますが、これがここで役立つかどうかはわかりません。
前もって感謝します!
ObjectAnimator
withAnimatorSet
を使用して、適切なリスナーと同時に複数のアニメーションを再生できます。
例えば():
val imageXAnimator = ObjectAnimator.ofFloat(imageView, "translationX", translateX)
val imageYAnimator = ObjectAnimator.ofFloat(imageView, "translationY", translateY)
val imageAlphaAnimator = ObjectAnimator.ofFloat(imageView, "alpha", if (reverse) 0F else 1F)
val animationSet = AnimatorSet()
animationSet.playTogether(
imageXAnimator,
imageYAnimator,
imageAlphaAnimator)
animationSet.interpolator = DecelerateInterpolator()
animationSet.duration = 1000
animationSet.addListener(
onStart = {
//When animation is started
},
onEnd = {
//When animation finishes
}
)
animationSet.start()
または、kotlin
特定ので呼び出すことができる単純な拡張View
:
inline fun View.animateTranslationY(translationY: Float, duration: Long = 1000, startDelay: Long = 0) {
val translationYAnimator = ObjectAnimator.ofFloat(this, "translationY", translationY)
translationYAnimator.duration = duration
translationYAnimator.startDelay = startDelay
translationYAnimator.interpolator = LinearInterpolator()
translationYAnimator.addAnimatorListener(
onStart = { },
onEnd = { }
)
translationYAnimator.start()
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加