Search in sources :

Example 1 with Layer

use of com.airbnb.lottie.model.layer.Layer in project lottie-android by airbnb.

the class LottieCompositionMoshiParser method parseLayers.

private static void parseLayers(JsonReader reader, LottieComposition composition, List<Layer> layers, LongSparseArray<Layer> layerMap) throws IOException {
    int imageCount = 0;
    reader.beginArray();
    while (reader.hasNext()) {
        Layer layer = LayerParser.parse(reader, composition);
        if (layer.getLayerType() == Layer.LayerType.IMAGE) {
            imageCount++;
        }
        layers.add(layer);
        layerMap.put(layer.getId(), layer);
        if (imageCount > 4) {
            Logger.warning("You have " + imageCount + " images. Lottie should primarily be " + "used with shapes. If you are using Adobe Illustrator, convert the Illustrator layers" + " to shape layers.");
        }
    }
    reader.endArray();
}
Also used : Layer(com.airbnb.lottie.model.layer.Layer)

Example 2 with Layer

use of com.airbnb.lottie.model.layer.Layer in project lottie-android by airbnb.

the class LottieDrawableTest method createComposition.

@SuppressWarnings("SameParameterValue")
private LottieComposition createComposition(int startFrame, int endFrame) {
    LottieComposition composition = new LottieComposition();
    composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList<Layer>(), new LongSparseArray<Layer>(0), new HashMap<String, List<Layer>>(0), new HashMap<String, LottieImageAsset>(0), new SparseArrayCompat<FontCharacter>(0), new HashMap<String, Font>(0), new ArrayList<Marker>());
    return composition;
}
Also used : Rect(android.graphics.Rect) Marker(com.airbnb.lottie.model.Marker) Layer(com.airbnb.lottie.model.layer.Layer) FontCharacter(com.airbnb.lottie.model.FontCharacter) Font(com.airbnb.lottie.model.Font) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with Layer

use of com.airbnb.lottie.model.layer.Layer in project lottie-android by airbnb.

the class LottieValueAnimatorUnitTest method createComposition.

private LottieComposition createComposition(int startFrame, int endFrame) {
    LottieComposition composition = new LottieComposition();
    composition.init(new Rect(), startFrame, endFrame, 1000, new ArrayList<Layer>(), new LongSparseArray<Layer>(0), new HashMap<String, List<Layer>>(0), new HashMap<String, LottieImageAsset>(0), new SparseArrayCompat<FontCharacter>(0), new HashMap<String, Font>(0), new ArrayList<Marker>());
    return composition;
}
Also used : Rect(android.graphics.Rect) Marker(com.airbnb.lottie.model.Marker) Layer(com.airbnb.lottie.model.layer.Layer) FontCharacter(com.airbnb.lottie.model.FontCharacter) Font(com.airbnb.lottie.model.Font) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with Layer

use of com.airbnb.lottie.model.layer.Layer in project lottie-android by airbnb.

the class LayerParser method parse.

public static Layer parse(JsonReader reader, LottieComposition composition) throws IOException {
    // This should always be set by After Effects. However, if somebody wants to minify
    // and optimize their json, the name isn't critical for most cases so it can be removed.
    String layerName = "UNSET";
    Layer.LayerType layerType = null;
    String refId = null;
    long layerId = 0;
    int solidWidth = 0;
    int solidHeight = 0;
    int solidColor = 0;
    int preCompWidth = 0;
    int preCompHeight = 0;
    long parentId = -1;
    float timeStretch = 1f;
    float startFrame = 0f;
    float inFrame = 0f;
    float outFrame = 0f;
    String cl = null;
    boolean hidden = false;
    BlurEffect blurEffect = null;
    DropShadowEffect dropShadowEffect = null;
    Layer.MatteType matteType = Layer.MatteType.NONE;
    AnimatableTransform transform = null;
    AnimatableTextFrame text = null;
    AnimatableTextProperties textProperties = null;
    AnimatableFloatValue timeRemapping = null;
    List<Mask> masks = new ArrayList<>();
    List<ContentModel> shapes = new ArrayList<>();
    reader.beginObject();
    while (reader.hasNext()) {
        switch(reader.selectName(NAMES)) {
            case 0:
                layerName = reader.nextString();
                break;
            case 1:
                layerId = reader.nextInt();
                break;
            case 2:
                refId = reader.nextString();
                break;
            case 3:
                int layerTypeInt = reader.nextInt();
                if (layerTypeInt < Layer.LayerType.UNKNOWN.ordinal()) {
                    layerType = Layer.LayerType.values()[layerTypeInt];
                } else {
                    layerType = Layer.LayerType.UNKNOWN;
                }
                break;
            case 4:
                parentId = reader.nextInt();
                break;
            case 5:
                solidWidth = (int) (reader.nextInt() * Utils.dpScale());
                break;
            case 6:
                solidHeight = (int) (reader.nextInt() * Utils.dpScale());
                break;
            case 7:
                solidColor = Color.parseColor(reader.nextString());
                break;
            case 8:
                transform = AnimatableTransformParser.parse(reader, composition);
                break;
            case 9:
                int matteTypeIndex = reader.nextInt();
                if (matteTypeIndex >= Layer.MatteType.values().length) {
                    composition.addWarning("Unsupported matte type: " + matteTypeIndex);
                    break;
                }
                matteType = Layer.MatteType.values()[matteTypeIndex];
                switch(matteType) {
                    case LUMA:
                        composition.addWarning("Unsupported matte type: Luma");
                        break;
                    case LUMA_INVERTED:
                        composition.addWarning("Unsupported matte type: Luma Inverted");
                        break;
                }
                composition.incrementMatteOrMaskCount(1);
                break;
            case 10:
                reader.beginArray();
                while (reader.hasNext()) {
                    masks.add(MaskParser.parse(reader, composition));
                }
                composition.incrementMatteOrMaskCount(masks.size());
                reader.endArray();
                break;
            case 11:
                reader.beginArray();
                while (reader.hasNext()) {
                    ContentModel shape = ContentModelParser.parse(reader, composition);
                    if (shape != null) {
                        shapes.add(shape);
                    }
                }
                reader.endArray();
                break;
            case 12:
                reader.beginObject();
                while (reader.hasNext()) {
                    switch(reader.selectName(TEXT_NAMES)) {
                        case 0:
                            text = AnimatableValueParser.parseDocumentData(reader, composition);
                            break;
                        case 1:
                            reader.beginArray();
                            if (reader.hasNext()) {
                                textProperties = AnimatableTextPropertiesParser.parse(reader, composition);
                            }
                            while (reader.hasNext()) {
                                reader.skipValue();
                            }
                            reader.endArray();
                            break;
                        default:
                            reader.skipName();
                            reader.skipValue();
                    }
                }
                reader.endObject();
                break;
            case 13:
                reader.beginArray();
                List<String> effectNames = new ArrayList<>();
                while (reader.hasNext()) {
                    reader.beginObject();
                    while (reader.hasNext()) {
                        switch(reader.selectName(EFFECTS_NAMES)) {
                            case 0:
                                int type = reader.nextInt();
                                if (type == 29) {
                                    blurEffect = BlurEffectParser.parse(reader, composition);
                                } else if (type == 25) {
                                    dropShadowEffect = new DropShadowEffectParser().parse(reader, composition);
                                }
                                break;
                            case 1:
                                String effectName = reader.nextString();
                                effectNames.add(effectName);
                                break;
                            default:
                                reader.skipName();
                                reader.skipValue();
                        }
                    }
                    reader.endObject();
                }
                reader.endArray();
                composition.addWarning("Lottie doesn't support layer effects. If you are using them for " + " fills, strokes, trim paths etc. then try adding them directly as contents " + " in your shape. Found: " + effectNames);
                break;
            case 14:
                timeStretch = (float) reader.nextDouble();
                break;
            case 15:
                startFrame = (float) reader.nextDouble();
                break;
            case 16:
                preCompWidth = (int) (reader.nextInt() * Utils.dpScale());
                break;
            case 17:
                preCompHeight = (int) (reader.nextInt() * Utils.dpScale());
                break;
            case 18:
                inFrame = (float) reader.nextDouble();
                break;
            case 19:
                outFrame = (float) reader.nextDouble();
                break;
            case 20:
                timeRemapping = AnimatableValueParser.parseFloat(reader, composition, false);
                break;
            case 21:
                cl = reader.nextString();
                break;
            case 22:
                hidden = reader.nextBoolean();
                break;
            default:
                reader.skipName();
                reader.skipValue();
        }
    }
    reader.endObject();
    List<Keyframe<Float>> inOutKeyframes = new ArrayList<>();
    // Before the in frame
    if (inFrame > 0) {
        Keyframe<Float> preKeyframe = new Keyframe<>(composition, 0f, 0f, null, 0f, inFrame);
        inOutKeyframes.add(preKeyframe);
    }
    // The + 1 is because the animation should be visible on the out frame itself.
    outFrame = (outFrame > 0 ? outFrame : composition.getEndFrame());
    Keyframe<Float> visibleKeyframe = new Keyframe<>(composition, 1f, 1f, null, inFrame, outFrame);
    inOutKeyframes.add(visibleKeyframe);
    Keyframe<Float> outKeyframe = new Keyframe<>(composition, 0f, 0f, null, outFrame, Float.MAX_VALUE);
    inOutKeyframes.add(outKeyframe);
    if (layerName.endsWith(".ai") || "ai".equals(cl)) {
        composition.addWarning("Convert your Illustrator layers to shape layers.");
    }
    return new Layer(shapes, composition, layerName, layerId, layerType, parentId, refId, masks, transform, solidWidth, solidHeight, solidColor, timeStretch, startFrame, preCompWidth, preCompHeight, text, textProperties, inOutKeyframes, matteType, timeRemapping, hidden, blurEffect, dropShadowEffect);
}
Also used : AnimatableTextProperties(com.airbnb.lottie.model.animatable.AnimatableTextProperties) ArrayList(java.util.ArrayList) AnimatableTextFrame(com.airbnb.lottie.model.animatable.AnimatableTextFrame) Keyframe(com.airbnb.lottie.value.Keyframe) AnimatableTransform(com.airbnb.lottie.model.animatable.AnimatableTransform) AnimatableFloatValue(com.airbnb.lottie.model.animatable.AnimatableFloatValue) Mask(com.airbnb.lottie.model.content.Mask) Layer(com.airbnb.lottie.model.layer.Layer) ContentModel(com.airbnb.lottie.model.content.ContentModel) BlurEffect(com.airbnb.lottie.model.content.BlurEffect)

Example 5 with Layer

use of com.airbnb.lottie.model.layer.Layer 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)

Aggregations

Layer (com.airbnb.lottie.model.layer.Layer)6 ArrayList (java.util.ArrayList)5 Rect (android.graphics.Rect)3 Font (com.airbnb.lottie.model.Font)3 FontCharacter (com.airbnb.lottie.model.FontCharacter)3 Marker (com.airbnb.lottie.model.Marker)3 List (java.util.List)3 LongSparseArray (androidx.collection.LongSparseArray)2 LottieImageAsset (com.airbnb.lottie.LottieImageAsset)2 SparseArrayCompat (androidx.collection.SparseArrayCompat)1 LottieComposition (com.airbnb.lottie.LottieComposition)1 AnimatableFloatValue (com.airbnb.lottie.model.animatable.AnimatableFloatValue)1 AnimatableTextFrame (com.airbnb.lottie.model.animatable.AnimatableTextFrame)1 AnimatableTextProperties (com.airbnb.lottie.model.animatable.AnimatableTextProperties)1 AnimatableTransform (com.airbnb.lottie.model.animatable.AnimatableTransform)1 BlurEffect (com.airbnb.lottie.model.content.BlurEffect)1 ContentModel (com.airbnb.lottie.model.content.ContentModel)1 Mask (com.airbnb.lottie.model.content.Mask)1 Keyframe (com.airbnb.lottie.value.Keyframe)1 HashMap (java.util.HashMap)1