Search in sources :

Example 1 with Mask

use of com.airbnb.lottie.model.content.Mask in project lottie-android by airbnb.

the class BaseLayer method intersectBoundsWithMask.

private void intersectBoundsWithMask(RectF rect, Matrix matrix) {
    maskBoundsRect.set(0, 0, 0, 0);
    if (!hasMasksOnThisLayer()) {
        return;
    }
    // noinspection ConstantConditions
    int size = mask.getMasks().size();
    for (int i = 0; i < size; i++) {
        Mask mask = this.mask.getMasks().get(i);
        BaseKeyframeAnimation<?, Path> maskAnimation = this.mask.getMaskAnimations().get(i);
        Path maskPath = maskAnimation.getValue();
        if (maskPath == null) {
            // https://github.com/airbnb/lottie-android/issues/1879
            continue;
        }
        path.set(maskPath);
        path.transform(matrix);
        switch(mask.getMaskMode()) {
            case MASK_MODE_NONE:
                // Mask mode none will just render the original content so it is the whole bounds.
                return;
            case MASK_MODE_SUBTRACT:
                // canvas so we can't use the mask bounds.
                return;
            case MASK_MODE_INTERSECT:
            case MASK_MODE_ADD:
                if (mask.isInverted()) {
                    return;
                }
            default:
                path.computeBounds(tempMaskBoundsRect, false);
                // the rect will always extend to (0,0).
                if (i == 0) {
                    maskBoundsRect.set(tempMaskBoundsRect);
                } else {
                    maskBoundsRect.set(Math.min(maskBoundsRect.left, tempMaskBoundsRect.left), Math.min(maskBoundsRect.top, tempMaskBoundsRect.top), Math.max(maskBoundsRect.right, tempMaskBoundsRect.right), Math.max(maskBoundsRect.bottom, tempMaskBoundsRect.bottom));
                }
        }
    }
    boolean intersects = rect.intersect(maskBoundsRect);
    if (!intersects) {
        rect.set(0f, 0f, 0f, 0f);
    }
}
Also used : Path(android.graphics.Path) KeyPath(com.airbnb.lottie.model.KeyPath) Mask(com.airbnb.lottie.model.content.Mask) LPaint(com.airbnb.lottie.animation.LPaint) Paint(android.graphics.Paint)

Example 2 with Mask

use of com.airbnb.lottie.model.content.Mask in project lottie-android by airbnb.

the class BaseLayer method applyMasks.

private void applyMasks(Canvas canvas, Matrix matrix) {
    L.beginSection("Layer#saveLayer");
    Utils.saveLayerCompat(canvas, rect, dstInPaint, SAVE_FLAGS);
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
        // Pre-Pie, offscreen buffers were opaque which meant that outer border of a mask
        // might get drawn depending on the result of float rounding.
        clearCanvas(canvas);
    }
    L.endSection("Layer#saveLayer");
    for (int i = 0; i < mask.getMasks().size(); i++) {
        Mask mask = this.mask.getMasks().get(i);
        BaseKeyframeAnimation<ShapeData, Path> maskAnimation = this.mask.getMaskAnimations().get(i);
        BaseKeyframeAnimation<Integer, Integer> opacityAnimation = this.mask.getOpacityAnimations().get(i);
        switch(mask.getMaskMode()) {
            case MASK_MODE_NONE:
                // this should noop.
                if (areAllMasksNone()) {
                    contentPaint.setAlpha(255);
                    canvas.drawRect(rect, contentPaint);
                }
                break;
            case MASK_MODE_ADD:
                if (mask.isInverted()) {
                    applyInvertedAddMask(canvas, matrix, maskAnimation, opacityAnimation);
                } else {
                    applyAddMask(canvas, matrix, maskAnimation, opacityAnimation);
                }
                break;
            case MASK_MODE_SUBTRACT:
                if (i == 0) {
                    contentPaint.setColor(Color.BLACK);
                    contentPaint.setAlpha(255);
                    canvas.drawRect(rect, contentPaint);
                }
                if (mask.isInverted()) {
                    applyInvertedSubtractMask(canvas, matrix, maskAnimation, opacityAnimation);
                } else {
                    applySubtractMask(canvas, matrix, maskAnimation);
                }
                break;
            case MASK_MODE_INTERSECT:
                if (mask.isInverted()) {
                    applyInvertedIntersectMask(canvas, matrix, maskAnimation, opacityAnimation);
                } else {
                    applyIntersectMask(canvas, matrix, maskAnimation, opacityAnimation);
                }
                break;
        }
    }
    L.beginSection("Layer#restoreLayer");
    canvas.restore();
    L.endSection("Layer#restoreLayer");
}
Also used : Path(android.graphics.Path) KeyPath(com.airbnb.lottie.model.KeyPath) Mask(com.airbnb.lottie.model.content.Mask) LPaint(com.airbnb.lottie.animation.LPaint) Paint(android.graphics.Paint) ShapeData(com.airbnb.lottie.model.content.ShapeData)

Example 3 with Mask

use of com.airbnb.lottie.model.content.Mask 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 4 with Mask

use of com.airbnb.lottie.model.content.Mask in project lottie-android by airbnb.

the class MaskParser method parse.

static Mask parse(JsonReader reader, LottieComposition composition) throws IOException {
    Mask.MaskMode maskMode = null;
    AnimatableShapeValue maskPath = null;
    AnimatableIntegerValue opacity = null;
    boolean inverted = false;
    reader.beginObject();
    while (reader.hasNext()) {
        String mode = reader.nextName();
        switch(mode) {
            case "mode":
                switch(reader.nextString()) {
                    case "a":
                        maskMode = Mask.MaskMode.MASK_MODE_ADD;
                        break;
                    case "s":
                        maskMode = Mask.MaskMode.MASK_MODE_SUBTRACT;
                        break;
                    case "n":
                        maskMode = Mask.MaskMode.MASK_MODE_NONE;
                        break;
                    case "i":
                        composition.addWarning("Animation contains intersect masks. They are not supported but will be treated like add masks.");
                        maskMode = Mask.MaskMode.MASK_MODE_INTERSECT;
                        break;
                    default:
                        Logger.warning("Unknown mask mode " + mode + ". Defaulting to Add.");
                        maskMode = Mask.MaskMode.MASK_MODE_ADD;
                }
                break;
            case "pt":
                maskPath = AnimatableValueParser.parseShapeData(reader, composition);
                break;
            case "o":
                opacity = AnimatableValueParser.parseInteger(reader, composition);
                break;
            case "inv":
                inverted = reader.nextBoolean();
                break;
            default:
                reader.skipValue();
        }
    }
    reader.endObject();
    return new Mask(maskMode, maskPath, opacity, inverted);
}
Also used : AnimatableIntegerValue(com.airbnb.lottie.model.animatable.AnimatableIntegerValue) Mask(com.airbnb.lottie.model.content.Mask) AnimatableShapeValue(com.airbnb.lottie.model.animatable.AnimatableShapeValue)

Aggregations

Mask (com.airbnb.lottie.model.content.Mask)4 Paint (android.graphics.Paint)2 Path (android.graphics.Path)2 LPaint (com.airbnb.lottie.animation.LPaint)2 KeyPath (com.airbnb.lottie.model.KeyPath)2 AnimatableFloatValue (com.airbnb.lottie.model.animatable.AnimatableFloatValue)1 AnimatableIntegerValue (com.airbnb.lottie.model.animatable.AnimatableIntegerValue)1 AnimatableShapeValue (com.airbnb.lottie.model.animatable.AnimatableShapeValue)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 ShapeData (com.airbnb.lottie.model.content.ShapeData)1 Layer (com.airbnb.lottie.model.layer.Layer)1 Keyframe (com.airbnb.lottie.value.Keyframe)1 ArrayList (java.util.ArrayList)1