From 451181551dbe8029856d946e1e6efc17dafbfb94 Mon Sep 17 00:00:00 2001 From: podarsmarty Date: Thu, 25 Apr 2019 08:50:35 -0700 Subject: [PATCH] Have software rendering fallback on hardware acceleration when too large (#1190) In cases where LottieAnimationView is large and software rendering is used, Android may not be able to generate a bitmap large enough which causes nothing to be shown. Rather than have this happen, fall back on hardware acceleration which is supported > api 11. Potentially taking a perf hit and displaying is better than a blank screen and not displaying at all. Encountered this where upon rotation on a tablet, the view became big enough that generation of the bitmap failed. --- .../com/airbnb/lottie/LottieAnimationView.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java index 2d7f9822ed..a00879a5d7 100644 --- a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java +++ b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java @@ -34,6 +34,9 @@ import java.util.List; import java.util.Set; +import static com.airbnb.lottie.RenderMode.HARDWARE; +import static com.airbnb.lottie.RenderMode.SOFTWARE; + /** * This view will load, deserialize, and display an After Effects animation exported with * bodymovin (https://github.com/bodymovin/bodymovin). @@ -806,6 +809,21 @@ private void clearComposition() { lottieDrawable.clearComposition(); } + /** + * If rendering via software, Android will fail to generate a bitmap if the view is too large. Rather than displaying + * nothing, fallback on hardware acceleration which may incur a performance hit. + * + * @see #setRenderMode(RenderMode) + * @see com.airbnb.lottie.LottieDrawable#draw(android.graphics.Canvas) + */ + @Override + public void buildDrawingCache(boolean autoScale) { + super.buildDrawingCache(autoScale); + if (getLayerType() == LAYER_TYPE_SOFTWARE && getDrawingCache(autoScale) == null) { + setRenderMode(HARDWARE); + } + } + /** * Call this to set whether or not to render with hardware or software acceleration. * Lottie defaults to Automatic which will use hardware acceleration unless: