Lottie是一個支持Android、iOS、React Native,並由 Adobe After Effects製做aep格式的動畫,而後經由bodymovin插件轉化渲染爲json格式可被移動端本地識別解析的Airbnb開源庫。 Lottie實時呈現After Effects動畫效果,讓應用程序能夠像使用靜態圖片同樣輕鬆地使用動畫。 Lottie支持API 14及以上。html
在本身項目module的build.gradle文件中添加以下代碼:react
dependencies {
compile 'com.airbnb.android:lottie:2.0.0-beta4'
}
複製代碼
LottieAnimationView使用最簡單的方法是:android
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/animation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:lottie_fileName="hello-world.json"
app:lottie_loop="true"
app:lottie_autoPlay="true" />
複製代碼
其中lottie_loop屬性爲是否重複無限期動畫,當爲true時,動畫無限次數播放,爲false時,播放一次。ios
或者把json資源放在app/src/main/assets下,也能夠這樣使用它:git
LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
animationView.setAnimation("hello-world.json");
animationView.loop(true);
animationView.playAnimation();
複製代碼
該方法將加載文件並在後臺解析動畫,在完成後異步開始呈現。github
若是您但願重用一個動畫,例如在列表的每一個項目中,或者從一個網絡請求JSONObject中加載它:json
LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
...
Cancellable compositionCancellable = LottieComposition.Factory.fromJson(getResources(), jsonObject, (composition) -> {
animationView.setComposition(composition);
animationView.playAnimation();
});
// 取消異步加載
// compositionCancellable.cancel();
複製代碼
你也能夠控制動畫添加監聽:react-native
animationView.addAnimatorUpdateListener((animation) -> {
// Do something.
});
animationView.playAnimation();
...
if (animationView.isAnimating()) {
// Do something.
}
...
animationView.setProgress(0.5f);
...
// 自定義動畫速度和時長
ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f)
.setDuration(500);
animator.addUpdateListener(animation -> {
animationView.setProgress(animation.getAnimatedValue());
});
animator.start();
...
animationView.cancelAnimation();
複製代碼
你能夠給整個動畫,一個特定的圖層,或者一個圖層的特定內容添加一個顏色過濾器。緩存
// 任何符合顏色過濾界面的類
final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.LIGHTEN);
// 在整個視圖中添加一個顏色過濾器
animationView.addColorFilter(colorFilter);
//在特定的圖層中添加一個顏色濾鏡
animationView.addColorFilterToLayer("hello_layer", colorFilter);
// 添加一個彩色過濾器特效「hello_layer」上的內容
animationView.addColorFilterToContent("hello_layer", "hello", colorFilter);
// 清除全部的顏色濾鏡
animationView.clearColorFilters();
複製代碼
注意:顏色過濾器只適用於圖層,如圖像層和實層,以及包含填充、描邊或組內容的內容。bash
在引擎蓋下,LottieAnimationView使用LottieDrawable呈現其動畫。若是須要,您能夠直接使用可繪製的表單:
LottieDrawable drawable = new LottieDrawable();
LottieComposition.Factory.fromAssetFileName(getContext(), "hello-world.json", (composition) -> {
drawable.setComposition(composition);
});
複製代碼
若是你的動畫會常常重用,LottieAnimationView內置了一個可選的緩存策略。使用LottieAnimationView .setAnimation(String,CacheStrategy)。CacheStrategy能夠Strong, Weak, 或者None。LottieAnimationView對加載和解析的動畫持有強或弱的參考。弱或強表示緩存中組合的GC參考強度。
若是您的動畫是從assets中加載的,而且您的圖像文件位於assets 的子目錄中,那麼您能夠對圖像進行動畫。你能夠用LottieAnimationView或者LottieDrawable對象調用setImageAssetsFolder(String)方法,明確assets相對文件夾內的路徑,確保圖像bodymovin出口與他們的名字不變,文件夾應該img_ 開頭。若是直接使用LottieDrawable,當你完成時您必須調用recycleBitmaps。 若是你須要提供你本身的位圖,若是你從網絡或其餘地方下載,你能夠提供一個委託來作這個:
animationView.setImageAssetDelegate(new ImageAssetDelegate() {
@Override public Bitmap fetchBitmap(LottieImageAsset asset) {
getBitmap(asset);
}
});
複製代碼
若是該組合沒有遮罩或mattes,那麼性能和內存開銷應該至關不錯。沒有建立任何位圖,大多數操做都是簡單的畫布繪製操做。 若是這個組合有遮罩或mattes,就會使用屏幕外的緩衝區,而且會有一個性能打擊。 若是在你的動畫列表中使用,推薦使用CacheStrategy,在調用LottieAnimationView.setAnimation(String, CacheStrategy)的時候,因此動畫不須要每次都反序列化。
https://fir.im/lottiedemo
複製代碼
http://airbnb.design/lottie/
http://www.lottiefiles.com/
https://github.com/airbnb/lottie-android
http://www.adobe.com/cn/products/aftereffects.html
https://github.com/bodymovin/bodymovin
https://github.com/airbnb/lottie-react-native
https://github.com/airbnb/lottie-ios
https://github.com/airbnb
複製代碼