Lottie支持多平台,使用同一个JSON动画文件,可在不同平台实现相同的效果。Android 通过Airbnb的开源项目lottie-android实现,最低支持 API 16;
在项目的 build.gradle 文件添加依赖
dependencies { implementation 'com.airbnb.android:lottie:$lottieVersion'}
最新的版本号可以到官网或者github查询,lottie仅支持api16及以上。
src/main/assets
lottieAnimationView = findViewById(R.id.lottieAnimationView); lottieAnimationView.setImageAssetsFolder("images"); lottieAnimationView.setAnimation("data.json"); lottieAnimationView.loop(true); lottieAnimationView.playAnimation();
<com.airbnb.lottie.LottieAnimationView android:id="@+id/lottieAnimationView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:lottie_fileName="hello-world.json" app:lottie_loop="true" app:lottie_autoPlay="true" />
加载服务器上的.json文件,若有图片可以设置本地代理文件夹或者将图片资源放入 JSON。
private void loadUrl(String url) { Request request = new Request.Builder().url(url).build(); OkHttpClient client = new OkHttpClient(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) {} @Override public void onResponse(Call call, Response response) throws IOException { try { JSONObject json = new JSONObject(response.body().string()); LottieComposition.Factory .fromJson(getResources(), json, new OnCompositionLoadedListener() { @Override public void onCompositionLoaded(LottieComposition composition) { lottieAnimationView.setComposition(composition); lottieAnimationView.playAnimation(); } }); } catch (JSONException e) { e.printStackTrace(); } } }); }
通过url直接加载
lottieAnimationView.setAnimationFromUrl(url); lottieAnimationView.playAnimation();
下载到本地解压后指定文件夹代理
// 资源zip public final static File LOTTIE_FILES = new File(Environment.getExternalStorageDirectory()+"/ctclient/lottie/lottie.zip"); // 动效图片资源 public final static File IMAGES_FILES = new File(Environment.getExternalStorageDirectory()+"/ctclient/lottie/images"); // data.json路径 public final static File JSON_FILE = new File(Environment.getExternalStorageDirectory()+"/ctclient/lottie/data.json"); FileInputStream fis = null; if (JSON_FILE.exists()) { try { fis = new FileInputStream(JSON_FILE); } catch (FileNotFoundException e) { e.printStackTrace(); } } if (fis == null || !IMAGES_FILES.exists()) { Log.i("huangssh", "动画资源不存在"); return; } final String absolutePath = IMAGES_FILES.getAbsolutePath(); // 开启硬件加速 lottieAnimationView.useHardwareAcceleration(true); // 设置动画文件夹代理类 lottieAnimationView.setImageAssetDelegate(new ImageAssetDelegate() { @Override public Bitmap fetchBitmap(LottieImageAsset asset) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inScaled = true; opts.inDensity = UtilPhoneParam.densityDpi; Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeFile(absolutePath + File.separator + asset.getFileName(), opts); }catch (Exception e){ e.printStackTrace(); } return bitmap; } }); // 设置动画 LottieComposition.Factory.fromInputStream(fis, new OnCompositionLoadedListener() { @Override public void onCompositionLoaded(@Nullable LottieComposition composition) { lottieAnimationView.setComposition(composition); lottieAnimationView.playAnimation(); } });
lottieAnimationView.setFontAssetDelegate(new FontAssetDelegate(){ public Typeface fetchFont(String fontFamily) { Typeface customFont = Typeface.createFromFile(FONT_PATH + fontFamily); return customFont; }});
lottieAnimationView.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { // 判断动画加载结束 if (valueAnimator.getAnimatedFraction() == 1f) { if (dialog.isShowing() && getActivity() != null) dialog.dismiss(); } } });
lottieAnimationView.pauseAnimation();lottieAnimationView.cancelAnimation();lottieAnimationView.playAnimation();
lottieAnimationView.loop(true);
像ValueAnimator
一样使用setRepeatMode(...)
或 setRepeatCount(...)
scaleType :只支持centerCrop
和 centerInside
播放动画的某个部分
setMinFrame(...)setMaxFrame(...)setMinProgress(...)setMaxProgress(...)setMinAndMaxFrame(...)setMinAndMaxProgress(...)
lottieAnimationView.useHardwareAcceleration(true);
/** Lottie内部有两个缓存map(强引用缓存,弱引用缓存),在动画文件加载完成后会根据设置的缓存策略缓存动画,方便下次使用。*/lottieAnimationView.setAnimation(animation, LottieAnimationView.CacheStrategy.Strong); //强缓存lottieAnimationView.setAnimation(animation, LottieAnimationView.CacheStrategy.Weak); //弱缓存
根据进度缓存,并为下次播放作准备
lottieAnimationView.setProgress(progress); //设置当前进度lottieAnimationView.buildDrawingCache(); //强制缓存绘制数据Bitmap image = lottieAnimationView.getDrawingCache(); //获取当前绘制数据
需要先将图片转换成矢量图 SVG 格式,这个设计师一定有办法。
使用 Illustrator 将 SVG 文件另存为 .ai 文件。
使用 .ai 文件在 AE 中做动画处理。
最后通过 Bodymovin 插件,输出动画资源。
官方文档: artwork-to-lottie-walkthrough
联系客服