我正在使用Airbnb的新库Lottie在我的应用程序中制作动画。
该动画由一个70kb的JSON文件和一个328kb的图像文件夹组成。这个文件夹中有13个小pngs。
根据GitHub代码库的指示,我声明我的观点如下
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/lottie_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:lottie_fileName="animation.json"
android:layout_gravity="bottom"
app:lottie_loop="false"
app:lottie_autoPlay="true"/>然后,在我调用的相关java类上:
mLottieView.setImageAssetsFolder("images");然而,我有一个问题。动画是笨重和缓慢的,我的内存使用量跃升到屋顶。它从13MB增加到89,所有这些都发生在主线程上。
你能告诉我有没有办法解决这个问题?
谢谢
发布于 2017-03-13 10:24:51
documentation提到了几个影响性能的项目
还有一些通用的Android/移动问题需要考虑:
width="match_parent", height="wrap_content"组合,图像将被放大。使用wrap、wrap或固定大小。PNG上的的额外开销
如果你的UI线程按照你的建议做了太多的工作,你能推迟开始你的动画吗?如果可以这样做,那么LottieComposition可以使用诸如LottieComposition.fromJson()之类的静态方法。
您可以在后台线程上手动设置合成(然后可以选择创建LottieDrawable并设置合成)。完成后,您可以切换到UI线程和LottieAnimationView上的setComposition (或setImageDrawable
发布于 2017-06-27 01:43:28
我使用下面这两行代码解决了这个问题
final LottieAnimationView animationView = (LottieAnimationView) findViewById(R.id.animation_view);
animationView.useExperimentalHardwareAcceleration(true);
animationView.enableMergePathsForKitKatAndAbove(true);更新03-2021 (为了省去你挖掘评论的麻烦)
// deprecated
animationView.useExperimentalHardwareAcceleration(true)
// it was this for a bit, but now this is removed too
animationView.useHardwareAcceleration(true)
// confirmed this works on Lottie 3.2.0
animationView.setRenderMode(RenderMode.HARDWARE)发布于 2017-03-12 07:55:06
关于你的“小图像”和内存消耗问题,我已经回答了一个非常类似的问题:
Huge spike in memory consumption when using png with lot of transparent area
不要被328kb的图片文件夹搞糊涂了。在内存中,这些图像将占用更多-更多空间。这就是内存消耗大幅增加的原因。
在动画过程中分配和垃圾回收这些数量的位图将始终伴随着延迟。
https://stackoverflow.com/questions/42668527
复制相似问题