一、簡介
Lottie是Airbnb開源的一個面向IOS、Android、React Native的動畫庫,能分析Adobe After Effects導出的動畫,並且能讓原生App像使用靜態素材一樣使用這些動畫,完美實現動畫效果。
現在使用個平台的native代碼實現一套復雜的動畫是一件很困難並且很耗時的事,需要為不同尺寸的屏幕加載不同的素材資源,還需要寫大量難維護的代碼,而Lottie可以做到同一個通話文件在不同平台上實現相同的效果,極少減少開發時間,實現不同的動畫,只需要設置不同的動畫文件即可,極少減少開發和維護成本。
二、使用
1.在項目的build.gradle文件添加依賴
dependencies {
compile 'com.airbnb.android:lottie:2.1.0'
}
2.在布局文件中使用
動畫的文件可以在https://www.lottiefiles.com/這個網址下載。
<com.airbnb.lottie.LottieAnimationView android:id="@+id/animator_view" android:layout_width="wrap_content" android:layout_height="wrap_content" app:lottie_fileName="trophy.json" app:lottie_loop="true" app:lottie_autoPlay="true" />
3.常用的方法
3.1.animationView.loop(true)
設置動畫循環演示。
3.2.animationView.setAnimation("trophy.json")
設置動畫文件。文件在assets文件夾中。
3.3.animationView.isAnimating()
是否在演示中。
3.4.animationView.setProgress(0.5f)
設置演示的進度。
3.5.animationView.getProgress()
獲取演示的進度。
3.6.animationView.getDuration()
獲取演示的時間。
3.7.animationView.palyAnimation()
運行動畫。
3.8.animationView.pauseAnimation()
暫停動畫。
3.9.animationView.cancleAnimation()
關閉動畫。我寫的例子,運行animationView.pauseAniamtion()與cancleAnimation()的效果是一樣,運行完cacleAnimation()之后,再運行playAnimation()動畫不是從頭開始,而是接着演示動畫,查看源碼,查看pauseAniamtion()與cancleAniamtion()的實現,差別只是pauseAnimation()方法多了一個setProgress(progress)而已,而cancleAnimation()沒有將progress設置為0,所以顯示是一樣的。如果要解決,可以在使用cancleAniamtion()之前,加上animationView.setProgress(0)。
參考文章:https://mp.weixin.qq.com/s/LrkZtDZY3SE8IUQ-x1hsmQ