Search in sources :

Example 1 with LottieImageAsset

use of com.airbnb.lottie.LottieImageAsset in project lottie-android by airbnb.

the class ImageLayer method getBitmap.

@Nullable
private Bitmap getBitmap() {
    if (imageAnimation != null) {
        Bitmap callbackBitmap = imageAnimation.getValue();
        if (callbackBitmap != null) {
            return callbackBitmap;
        }
    }
    String refId = layerModel.getRefId();
    Bitmap bitmapFromDrawable = lottieDrawable.getBitmapForId(refId);
    if (bitmapFromDrawable != null) {
        return bitmapFromDrawable;
    }
    LottieImageAsset asset = this.lottieImageAsset;
    if (asset != null) {
        return asset.getBitmap();
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) LottieImageAsset(com.airbnb.lottie.LottieImageAsset) Nullable(androidx.annotation.Nullable)

Example 2 with LottieImageAsset

use of com.airbnb.lottie.LottieImageAsset in project lottie-android by airbnb.

the class ImageAssetManager method bitmapForId.

@Nullable
public Bitmap bitmapForId(String id) {
    LottieImageAsset asset = imageAssets.get(id);
    if (asset == null) {
        return null;
    }
    Bitmap bitmap = asset.getBitmap();
    if (bitmap != null) {
        return bitmap;
    }
    if (delegate != null) {
        bitmap = delegate.fetchBitmap(asset);
        if (bitmap != null) {
            putBitmap(id, bitmap);
        }
        return bitmap;
    }
    String filename = asset.getFileName();
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inScaled = true;
    opts.inDensity = 160;
    if (filename.startsWith("data:") && filename.indexOf("base64,") > 0) {
        // Contents look like a base64 data URI, with the format data:image/png;base64,<data>.
        byte[] data;
        try {
            data = Base64.decode(filename.substring(filename.indexOf(',') + 1), Base64.DEFAULT);
        } catch (IllegalArgumentException e) {
            Logger.warning("data URL did not have correct base64 format.", e);
            return null;
        }
        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
        return putBitmap(id, bitmap);
    }
    InputStream is;
    try {
        if (TextUtils.isEmpty(imagesFolder)) {
            throw new IllegalStateException("You must set an images folder before loading an image." + " Set it with LottieComposition#setImagesFolder or LottieDrawable#setImagesFolder");
        }
        is = context.getAssets().open(imagesFolder + filename);
    } catch (IOException e) {
        Logger.warning("Unable to open asset.", e);
        return null;
    }
    try {
        bitmap = BitmapFactory.decodeStream(is, null, opts);
    } catch (IllegalArgumentException e) {
        Logger.warning("Unable to decode image.", e);
        return null;
    }
    bitmap = Utils.resizeBitmapIfNeeded(bitmap, asset.getWidth(), asset.getHeight());
    return putBitmap(id, bitmap);
}
Also used : Bitmap(android.graphics.Bitmap) LottieImageAsset(com.airbnb.lottie.LottieImageAsset) InputStream(java.io.InputStream) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException) Nullable(androidx.annotation.Nullable)

Example 3 with LottieImageAsset

use of com.airbnb.lottie.LottieImageAsset in project lottie-android by airbnb.

the class ImageAssetManager method updateBitmap.

/**
 * Returns the previously set bitmap or null.
 */
@Nullable
public Bitmap updateBitmap(String id, @Nullable Bitmap bitmap) {
    if (bitmap == null) {
        LottieImageAsset asset = imageAssets.get(id);
        Bitmap ret = asset.getBitmap();
        asset.setBitmap(null);
        return ret;
    }
    Bitmap prevBitmap = imageAssets.get(id).getBitmap();
    putBitmap(id, bitmap);
    return prevBitmap;
}
Also used : Bitmap(android.graphics.Bitmap) LottieImageAsset(com.airbnb.lottie.LottieImageAsset) Nullable(androidx.annotation.Nullable)

Example 4 with LottieImageAsset

use of com.airbnb.lottie.LottieImageAsset in project lottie-android by airbnb.

the class LottieCompositionMoshiParser method parse.

public static LottieComposition parse(JsonReader reader) throws IOException {
    float scale = Utils.dpScale();
    float startFrame = 0f;
    float endFrame = 0f;
    float frameRate = 0f;
    final LongSparseArray<Layer> layerMap = new LongSparseArray<>();
    final List<Layer> layers = new ArrayList<>();
    int width = 0;
    int height = 0;
    Map<String, List<Layer>> precomps = new HashMap<>();
    Map<String, LottieImageAsset> images = new HashMap<>();
    Map<String, Font> fonts = new HashMap<>();
    List<Marker> markers = new ArrayList<>();
    SparseArrayCompat<FontCharacter> characters = new SparseArrayCompat<>();
    LottieComposition composition = new LottieComposition();
    reader.beginObject();
    while (reader.hasNext()) {
        switch(reader.selectName(NAMES)) {
            case 0:
                width = reader.nextInt();
                break;
            case 1:
                height = reader.nextInt();
                break;
            case 2:
                startFrame = (float) reader.nextDouble();
                break;
            case 3:
                endFrame = (float) reader.nextDouble() - 0.01f;
                break;
            case 4:
                frameRate = (float) reader.nextDouble();
                break;
            case 5:
                String version = reader.nextString();
                String[] versions = version.split("\\.");
                int majorVersion = Integer.parseInt(versions[0]);
                int minorVersion = Integer.parseInt(versions[1]);
                int patchVersion = Integer.parseInt(versions[2]);
                if (!Utils.isAtLeastVersion(majorVersion, minorVersion, patchVersion, 4, 4, 0)) {
                    composition.addWarning("Lottie only supports bodymovin >= 4.4.0");
                }
                break;
            case 6:
                parseLayers(reader, composition, layers, layerMap);
                break;
            case 7:
                parseAssets(reader, composition, precomps, images);
                break;
            case 8:
                parseFonts(reader, fonts);
                break;
            case 9:
                parseChars(reader, composition, characters);
                break;
            case 10:
                parseMarkers(reader, markers);
                break;
            default:
                reader.skipName();
                reader.skipValue();
        }
    }
    int scaledWidth = (int) (width * scale);
    int scaledHeight = (int) (height * scale);
    Rect bounds = new Rect(0, 0, scaledWidth, scaledHeight);
    composition.init(bounds, startFrame, endFrame, frameRate, layers, layerMap, precomps, images, characters, fonts, markers);
    return composition;
}
Also used : LongSparseArray(androidx.collection.LongSparseArray) Rect(android.graphics.Rect) HashMap(java.util.HashMap) LottieImageAsset(com.airbnb.lottie.LottieImageAsset) ArrayList(java.util.ArrayList) Marker(com.airbnb.lottie.model.Marker) Layer(com.airbnb.lottie.model.layer.Layer) Font(com.airbnb.lottie.model.Font) FontCharacter(com.airbnb.lottie.model.FontCharacter) ArrayList(java.util.ArrayList) List(java.util.List) LottieComposition(com.airbnb.lottie.LottieComposition) SparseArrayCompat(androidx.collection.SparseArrayCompat)

Example 5 with LottieImageAsset

use of com.airbnb.lottie.LottieImageAsset in project lottie-android by airbnb.

the class LottieCompositionMoshiParser method parseAssets.

private static void parseAssets(JsonReader reader, LottieComposition composition, Map<String, List<Layer>> precomps, Map<String, LottieImageAsset> images) throws IOException {
    reader.beginArray();
    while (reader.hasNext()) {
        String id = null;
        // For precomps
        List<Layer> layers = new ArrayList<>();
        LongSparseArray<Layer> layerMap = new LongSparseArray<>();
        // For images
        int width = 0;
        int height = 0;
        String imageFileName = null;
        String relativeFolder = null;
        reader.beginObject();
        while (reader.hasNext()) {
            switch(reader.selectName(ASSETS_NAMES)) {
                case 0:
                    id = reader.nextString();
                    break;
                case 1:
                    reader.beginArray();
                    while (reader.hasNext()) {
                        Layer layer = LayerParser.parse(reader, composition);
                        layerMap.put(layer.getId(), layer);
                        layers.add(layer);
                    }
                    reader.endArray();
                    break;
                case 2:
                    width = reader.nextInt();
                    break;
                case 3:
                    height = reader.nextInt();
                    break;
                case 4:
                    imageFileName = reader.nextString();
                    break;
                case 5:
                    relativeFolder = reader.nextString();
                    break;
                default:
                    reader.skipName();
                    reader.skipValue();
            }
        }
        reader.endObject();
        if (imageFileName != null) {
            LottieImageAsset image = new LottieImageAsset(width, height, id, imageFileName, relativeFolder);
            images.put(image.getId(), image);
        } else {
            precomps.put(id, layers);
        }
    }
    reader.endArray();
}
Also used : LongSparseArray(androidx.collection.LongSparseArray) LottieImageAsset(com.airbnb.lottie.LottieImageAsset) ArrayList(java.util.ArrayList) Layer(com.airbnb.lottie.model.layer.Layer)

Aggregations

LottieImageAsset (com.airbnb.lottie.LottieImageAsset)5 Bitmap (android.graphics.Bitmap)3 Nullable (androidx.annotation.Nullable)3 LongSparseArray (androidx.collection.LongSparseArray)2 Layer (com.airbnb.lottie.model.layer.Layer)2 ArrayList (java.util.ArrayList)2 BitmapFactory (android.graphics.BitmapFactory)1 Rect (android.graphics.Rect)1 SparseArrayCompat (androidx.collection.SparseArrayCompat)1 LottieComposition (com.airbnb.lottie.LottieComposition)1 Font (com.airbnb.lottie.model.Font)1 FontCharacter (com.airbnb.lottie.model.FontCharacter)1 Marker (com.airbnb.lottie.model.Marker)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1 List (java.util.List)1