1 package com.airbnb.lottie.model.layer; 2 3 import androidx.annotation.Nullable; 4 5 import com.airbnb.lottie.LottieComposition; 6 import com.airbnb.lottie.model.animatable.AnimatableFloatValue; 7 import com.airbnb.lottie.model.animatable.AnimatableTextFrame; 8 import com.airbnb.lottie.model.animatable.AnimatableTextProperties; 9 import com.airbnb.lottie.model.animatable.AnimatableTransform; 10 import com.airbnb.lottie.model.content.BlurEffect; 11 import com.airbnb.lottie.model.content.ContentModel; 12 import com.airbnb.lottie.model.content.Mask; 13 import com.airbnb.lottie.parser.DropShadowEffect; 14 import com.airbnb.lottie.value.Keyframe; 15 16 import java.util.List; 17 import java.util.Locale; 18 19 public class Layer { 20 21 public enum LayerType { 22 PRE_COMP, 23 SOLID, 24 IMAGE, 25 NULL, 26 SHAPE, 27 TEXT, 28 UNKNOWN 29 } 30 31 public enum MatteType { 32 NONE, 33 ADD, 34 INVERT, 35 LUMA, 36 LUMA_INVERTED, 37 UNKNOWN 38 } 39 40 private final List<ContentModel> shapes; 41 private final LottieComposition composition; 42 private final String layerName; 43 private final long layerId; 44 private final LayerType layerType; 45 private final long parentId; 46 @Nullable private final String refId; 47 private final List<Mask> masks; 48 private final AnimatableTransform transform; 49 private final int solidWidth; 50 private final int solidHeight; 51 private final int solidColor; 52 private final float timeStretch; 53 private final float startFrame; 54 private final int preCompWidth; 55 private final int preCompHeight; 56 @Nullable private final AnimatableTextFrame text; 57 @Nullable private final AnimatableTextProperties textProperties; 58 @Nullable private final AnimatableFloatValue timeRemapping; 59 private final List<Keyframe<Float>> inOutKeyframes; 60 private final MatteType matteType; 61 private final boolean hidden; 62 @Nullable private final BlurEffect blurEffect; 63 @Nullable private final DropShadowEffect dropShadowEffect; 64 Layer(List<ContentModel> shapes, LottieComposition composition, String layerName, long layerId, LayerType layerType, long parentId, @Nullable String refId, List<Mask> masks, AnimatableTransform transform, int solidWidth, int solidHeight, int solidColor, float timeStretch, float startFrame, int preCompWidth, int preCompHeight, @Nullable AnimatableTextFrame text, @Nullable AnimatableTextProperties textProperties, List<Keyframe<Float>> inOutKeyframes, MatteType matteType, @Nullable AnimatableFloatValue timeRemapping, boolean hidden, @Nullable BlurEffect blurEffect, @Nullable DropShadowEffect dropShadowEffect)65 public Layer(List<ContentModel> shapes, LottieComposition composition, String layerName, long layerId, 66 LayerType layerType, long parentId, @Nullable String refId, List<Mask> masks, 67 AnimatableTransform transform, int solidWidth, int solidHeight, int solidColor, 68 float timeStretch, float startFrame, int preCompWidth, int preCompHeight, 69 @Nullable AnimatableTextFrame text, @Nullable AnimatableTextProperties textProperties, 70 List<Keyframe<Float>> inOutKeyframes, MatteType matteType, 71 @Nullable AnimatableFloatValue timeRemapping, boolean hidden, @Nullable BlurEffect blurEffect, 72 @Nullable DropShadowEffect dropShadowEffect) { 73 this.shapes = shapes; 74 this.composition = composition; 75 this.layerName = layerName; 76 this.layerId = layerId; 77 this.layerType = layerType; 78 this.parentId = parentId; 79 this.refId = refId; 80 this.masks = masks; 81 this.transform = transform; 82 this.solidWidth = solidWidth; 83 this.solidHeight = solidHeight; 84 this.solidColor = solidColor; 85 this.timeStretch = timeStretch; 86 this.startFrame = startFrame; 87 this.preCompWidth = preCompWidth; 88 this.preCompHeight = preCompHeight; 89 this.text = text; 90 this.textProperties = textProperties; 91 this.inOutKeyframes = inOutKeyframes; 92 this.matteType = matteType; 93 this.timeRemapping = timeRemapping; 94 this.hidden = hidden; 95 this.blurEffect = blurEffect; 96 this.dropShadowEffect = dropShadowEffect; 97 } 98 getComposition()99 LottieComposition getComposition() { 100 return composition; 101 } 102 getTimeStretch()103 float getTimeStretch() { 104 return timeStretch; 105 } 106 getStartProgress()107 float getStartProgress() { 108 return startFrame / composition.getDurationFrames(); 109 } 110 getInOutKeyframes()111 List<Keyframe<Float>> getInOutKeyframes() { 112 return inOutKeyframes; 113 } 114 getId()115 public long getId() { 116 return layerId; 117 } 118 getName()119 String getName() { 120 return layerName; 121 } 122 getRefId()123 @Nullable String getRefId() { 124 return refId; 125 } 126 getPreCompWidth()127 int getPreCompWidth() { 128 return preCompWidth; 129 } 130 getPreCompHeight()131 int getPreCompHeight() { 132 return preCompHeight; 133 } 134 getMasks()135 List<Mask> getMasks() { 136 return masks; 137 } 138 getLayerType()139 public LayerType getLayerType() { 140 return layerType; 141 } 142 getMatteType()143 MatteType getMatteType() { 144 return matteType; 145 } 146 getParentId()147 long getParentId() { 148 return parentId; 149 } 150 getShapes()151 List<ContentModel> getShapes() { 152 return shapes; 153 } 154 getTransform()155 AnimatableTransform getTransform() { 156 return transform; 157 } 158 getSolidColor()159 int getSolidColor() { 160 return solidColor; 161 } 162 getSolidHeight()163 int getSolidHeight() { 164 return solidHeight; 165 } 166 getSolidWidth()167 int getSolidWidth() { 168 return solidWidth; 169 } 170 getText()171 @Nullable AnimatableTextFrame getText() { 172 return text; 173 } 174 getTextProperties()175 @Nullable AnimatableTextProperties getTextProperties() { 176 return textProperties; 177 } 178 getTimeRemapping()179 @Nullable AnimatableFloatValue getTimeRemapping() { 180 return timeRemapping; 181 } 182 toString()183 @Override public String toString() { 184 return toString(""); 185 } 186 isHidden()187 public boolean isHidden() { 188 return hidden; 189 } 190 getBlurEffect()191 @Nullable public BlurEffect getBlurEffect() { 192 return blurEffect; 193 } 194 getDropShadowEffect()195 @Nullable public DropShadowEffect getDropShadowEffect() { 196 return dropShadowEffect; 197 } 198 toString(String prefix)199 public String toString(String prefix) { 200 StringBuilder sb = new StringBuilder(); 201 sb.append(prefix).append(getName()).append("\n"); 202 Layer parent = composition.layerModelForId(getParentId()); 203 if (parent != null) { 204 sb.append("\t\tParents: ").append(parent.getName()); 205 parent = composition.layerModelForId(parent.getParentId()); 206 while (parent != null) { 207 sb.append("->").append(parent.getName()); 208 parent = composition.layerModelForId(parent.getParentId()); 209 } 210 sb.append(prefix).append("\n"); 211 } 212 if (!getMasks().isEmpty()) { 213 sb.append(prefix).append("\tMasks: ").append(getMasks().size()).append("\n"); 214 } 215 if (getSolidWidth() != 0 && getSolidHeight() != 0) { 216 sb.append(prefix).append("\tBackground: ").append(String 217 .format(Locale.US, "%dx%d %X\n", getSolidWidth(), getSolidHeight(), getSolidColor())); 218 } 219 if (!shapes.isEmpty()) { 220 sb.append(prefix).append("\tShapes:\n"); 221 for (Object shape : shapes) { 222 sb.append(prefix).append("\t\t").append(shape).append("\n"); 223 } 224 } 225 return sb.toString(); 226 } 227 } 228