1 package com.airbnb.lottie; 2 3 import android.graphics.Bitmap; 4 5 import androidx.annotation.Nullable; 6 import androidx.annotation.RestrictTo; 7 8 /** 9 * Data class describing an image asset embedded in a Lottie json file. 10 */ 11 public class LottieImageAsset { 12 private final int width; 13 private final int height; 14 private final String id; 15 private final String fileName; 16 private final String dirName; 17 /** 18 * Pre-set a bitmap for this asset 19 */ 20 @Nullable private Bitmap bitmap; 21 22 @RestrictTo(RestrictTo.Scope.LIBRARY) LottieImageAsset(int width, int height, String id, String fileName, String dirName)23 public LottieImageAsset(int width, int height, String id, String fileName, String dirName) { 24 this.width = width; 25 this.height = height; 26 this.id = id; 27 this.fileName = fileName; 28 this.dirName = dirName; 29 } 30 getWidth()31 public int getWidth() { 32 return width; 33 } 34 getHeight()35 public int getHeight() { 36 return height; 37 } 38 39 /** 40 * The reference id in the json file. 41 */ getId()42 public String getId() { 43 return id; 44 } 45 getFileName()46 public String getFileName() { 47 return fileName; 48 } 49 getDirName()50 @SuppressWarnings("unused") public String getDirName() { 51 return dirName; 52 } 53 54 /** 55 * Returns the bitmap that has been stored for this image asset if one was explicitly set. 56 */ getBitmap()57 @Nullable public Bitmap getBitmap() { 58 return bitmap; 59 } 60 61 /** 62 * Permanently sets the bitmap on this LottieImageAsset. This will: 63 * 1) Overwrite any existing Bitmaps. 64 * 2) Apply to *all* animations that use this LottieComposition. 65 * 66 * If you only want to replace the bitmap for this animation, use dynamic properties 67 * with {@link LottieProperty#IMAGE}. 68 */ setBitmap(@ullable Bitmap bitmap)69 public void setBitmap(@Nullable Bitmap bitmap) { 70 this.bitmap = bitmap; 71 } 72 73 /** 74 * Returns a new {@link LottieImageAsset} with the same properties as this one but with the 75 * dimensions and bitmap scaled. 76 */ copyWithScale(float scale)77 public LottieImageAsset copyWithScale(float scale) { 78 LottieImageAsset newAsset = new LottieImageAsset((int) (width * scale), (int) (height * scale), id, fileName, dirName); 79 if (bitmap != null) { 80 Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, newAsset.width, newAsset.height, true); 81 newAsset.setBitmap(scaledBitmap); 82 } 83 return newAsset; 84 } 85 86 /** 87 * Returns whether this asset has an embedded Bitmap or whether the fileName is a base64 encoded bitmap. 88 */ hasBitmap()89 public boolean hasBitmap() { 90 return bitmap != null || (fileName.startsWith("data:") && fileName.indexOf("base64,") > 0); 91 } 92 } 93