Allow you to update the Bitmap asset dynamically
Fixes #294
diff --git a/lottie/src/main/java/com/airbnb/lottie/ImageAssetBitmapManager.java b/lottie/src/main/java/com/airbnb/lottie/ImageAssetBitmapManager.java
index e42fb42..537843a 100644
--- a/lottie/src/main/java/com/airbnb/lottie/ImageAssetBitmapManager.java
+++ b/lottie/src/main/java/com/airbnb/lottie/ImageAssetBitmapManager.java
@@ -51,6 +51,11 @@
}
@Nullable
+ Bitmap updateBitmap(String id, @Nullable Bitmap bitmap) {
+ return bitmaps.put(id, bitmap);
+ }
+
+ @Nullable
Bitmap bitmapForId(String id) {
Bitmap bitmap = bitmaps.get(id);
if (bitmap == null) {
diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
index ecfed3e..e808d72 100644
--- a/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
+++ b/lottie/src/main/java/com/airbnb/lottie/LottieAnimationView.java
@@ -4,6 +4,7 @@
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
+import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.drawable.Drawable;
@@ -476,6 +477,18 @@
}
/**
+ * Allows you to modify or clear a bitmap that was loaded for an image either automatically
+ * through {@link #setImageAssetsFolder(String)} or with an {@link ImageAssetDelegate}.
+ *
+ * @return the previous Bitmap or null.
+ */
+ @Nullable
+ @SuppressWarnings({"unused", "WeakerAccess"})
+ public Bitmap updateBitmap(String id, @Nullable Bitmap bitmap) {
+ return lottieDrawable.updateBitmap(id, bitmap);
+ }
+
+ /**
* Use this if you can't bundle images with your app. This may be useful if you download the
* animations from the network or have the images saved to an SD Card. In that case, Lottie
* will defer the loading of the bitmap to this delegate.
diff --git a/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java b/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
index 79f9949..55e59cc 100644
--- a/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
+++ b/lottie/src/main/java/com/airbnb/lottie/LottieDrawable.java
@@ -437,6 +437,26 @@
return composition == null ? -1 : (int) (composition.getBounds().height() * scale);
}
+ /**
+ * Allows you to modify or clear a bitmap that was loaded for an image either automatically
+ * through {@link #setImagesAssetsFolder(String)} or with an {@link ImageAssetDelegate}.
+ *
+ * @return the previous Bitmap or null.
+ */
+ @Nullable
+ @SuppressWarnings({"unused", "WeakerAccess"})
+ public Bitmap updateBitmap(String id, @Nullable Bitmap bitmap) {
+ ImageAssetBitmapManager bm = getImageAssetBitmapManager();
+ if (bm == null) {
+ Log.w(L.TAG, "Cannot update bitmap. Most likely the drawable is not added to a View " +
+ "which prevents Lottie from getting a Context.");
+ return null;
+ }
+ Bitmap ret = bm.updateBitmap(id, bitmap);
+ invalidateSelf();
+ return ret;
+ }
+
@Nullable
Bitmap getImageAsset(String id) {
ImageAssetBitmapManager bm = getImageAssetBitmapManager();