LoginSignup
0
1

More than 1 year has passed since last update.

Lottie-Androidを使用する方法

Posted at

今回はLottie-Androidを使用してアニメーションの実装する方法です。

綺麗なアニメーションを簡単に実装することができます。

ただし、アニメーションファイルは別途必要です。

Lottie for Androidの導入

Lottie for Android
を導入します。

build.gradle
dependencies {
    implementation "com.airbnb.android:lottie:${latest.version}"
}

アニメーションファイル

res.rawフォルダにアニメーションファイルを追加する。
今回はlottie_animationと命名。

LottieAnimationView

lottie_animation_view.xml
 <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/animation_view"
        android:layout_width="26dp"
        android:layout_height="26dp"
        app:lottie_rawRes="@raw/lottie_animation" />

※ 表示後すぐにアニメーションさせたい場合

app:lottie_autoPlay="true"

※アニメーションをループさせたい場合

app:lottie_loop="true"

基本的にあまりこのような使い方をする場合は少ないのかもしれません。

自分が実際に使用したいタイミングはタップした後でした。
なので、任意のタイミングでアニメーションさせたい場合があると思います。

AnimationView.kt
binding.animationView.playAnimation()

playAnimation()を呼ぶとアニメーションが実行されます。なのでクリック時など任意のタイミングでこちらを呼ぶと良いでしょう!

AnimatorListenerにも対応してます。

AnimationView.kt
 binding.animationView.addAnimatorListener(object : Animator.AnimatorListener {
     override fun onAnimationStart(animator: Animator) {
        //アニメーションの開始を通知します。
     }

     override fun onAnimationEnd(animator: Animator) {
        //アニメーションの終了を通知します。
     }

     override fun onAnimationCancel(animator: Animator) {
        //アニメーションのキャンセルを通知します。
    }
     
    override fun onAnimationRepeat(animator: Animator) {
        //アニメーションの繰り返しを通知します。
    }
})

アニメーションの開始やアニメーションの終了タイミングがわかるのはいいですね!

以上です!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1