com.airbnb.lottie.model.KeyPath Java Examples

The following examples show how to use com.airbnb.lottie.model.KeyPath. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ContentGroup.java    From lottie-android with Apache License 2.0 6 votes vote down vote up
@Override public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  if (!keyPath.matches(getName(), depth)) {
    return;
  }

  if (!"__container".equals(getName())) {
    currentPartialKeyPath = currentPartialKeyPath.addKey(getName());

    if (keyPath.fullyResolvesTo(getName(), depth)) {
      accumulator.add(currentPartialKeyPath.resolve(this));
    }
  }

  if (keyPath.propagateToChildren(getName(), depth)) {
    int newDepth = depth + keyPath.incrementDepthBy(getName(), depth);
    for (int i = 0; i < contents.size(); i++) {
      Content content = contents.get(i);
      if (content instanceof KeyPathElement) {
        KeyPathElement element = (KeyPathElement) content;
        element.resolveKeyPath(keyPath, newDepth, accumulator, currentPartialKeyPath);
      }
    }
  }
}
 
Example #2
Source File: BaseLayer.java    From lottie-android with Apache License 2.0 6 votes vote down vote up
@Override
public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  if (!keyPath.matches(getName(), depth)) {
    return;
  }

  if (!"__container".equals(getName())) {
    currentPartialKeyPath = currentPartialKeyPath.addKey(getName());

    if (keyPath.fullyResolvesTo(getName(), depth)) {
      accumulator.add(currentPartialKeyPath.resolve(this));
    }
  }

  if (keyPath.propagateToChildren(getName(), depth)) {
    int newDepth = depth + keyPath.incrementDepthBy(getName(), depth);
    resolveChildKeyPath(keyPath, newDepth, accumulator, currentPartialKeyPath);
  }
}
 
Example #3
Source File: AudioView.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public void setTint(int foregroundTint) {
  post(()-> this.playPauseButton.addValueCallback(new KeyPath("**"),
                                                  LottieProperty.COLOR_FILTER,
                                                  new LottieValueCallback<>(new SimpleColorFilter(foregroundTint))));

  this.downloadButton.setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
  this.circleProgress.setBarColor(foregroundTint);

  if (this.duration != null) {
    this.duration.setTextColor(foregroundTint);
  }
  this.seekBar.getProgressDrawable().setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
  this.seekBar.getThumb().setColorFilter(foregroundTint, PorterDuff.Mode.SRC_IN);
}
 
Example #4
Source File: LottieAnimationView.java    From lottie-android with Apache License 2.0 5 votes vote down vote up
/**
 * Overload of {@link #addValueCallback(KeyPath, Object, LottieValueCallback)} that takes an interface. This allows you to use a single abstract
 * method code block in Kotlin such as:
 * animationView.addValueCallback(yourKeyPath, LottieProperty.COLOR) { yourColor }
 */
public <T> void addValueCallback(KeyPath keyPath, T property,
    final SimpleLottieValueCallback<T> callback) {
  lottieDrawable.addValueCallback(keyPath, property, new LottieValueCallback<T>() {
    @Override public T getValue(LottieFrameInfo<T> frameInfo) {
      return callback.getValue(frameInfo);
    }
  });
}
 
Example #5
Source File: CompositionLayer.java    From lottie-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void resolveChildKeyPath(KeyPath keyPath, int depth, List<KeyPath> accumulator,
    KeyPath currentPartialKeyPath) {
  for (int i = 0; i < layers.size(); i++) {
    layers.get(i).resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath);
  }
}
 
Example #6
Source File: LottieDrawable.java    From lottie-android with Apache License 2.0 5 votes vote down vote up
/**
 * Overload of {@link #addValueCallback(KeyPath, Object, LottieValueCallback)} that takes an interface. This allows you to use a single abstract
 * method code block in Kotlin such as:
 * drawable.addValueCallback(yourKeyPath, LottieProperty.COLOR) { yourColor }
 */
public <T> void addValueCallback(KeyPath keyPath, T property,
                                 final SimpleLottieValueCallback<T> callback) {
  addValueCallback(keyPath, property, new LottieValueCallback<T>() {
    @Override
    public T getValue(LottieFrameInfo<T> frameInfo) {
      return callback.getValue(frameInfo);
    }
  });
}
 
Example #7
Source File: LottieDrawable.java    From lottie-android with Apache License 2.0 5 votes vote down vote up
/**
 * Takes a {@link KeyPath}, potentially with wildcards or globstars and resolve it to a list of
 * zero or more actual {@link KeyPath Keypaths} that exist in the current animation.
 * <p>
 * If you want to set value callbacks for any of these values, it is recommend to use the
 * returned {@link KeyPath} objects because they will be internally resolved to their content
 * and won't trigger a tree walk of the animation contents when applied.
 */
public List<KeyPath> resolveKeyPath(KeyPath keyPath) {
  if (compositionLayer == null) {
    Logger.warning("Cannot resolve KeyPath. Composition is not set yet.");
    return Collections.emptyList();
  }
  List<KeyPath> keyPaths = new ArrayList<>();
  compositionLayer.resolveKeyPath(keyPath, 0, keyPaths, new KeyPath());
  return keyPaths;
}
 
Example #8
Source File: LottieDrawable.java    From lottie-android with Apache License 2.0 5 votes vote down vote up
/**
 * Add an property callback for the specified {@link KeyPath}. This {@link KeyPath} can resolve
 * to multiple contents. In that case, the callbacks's value will apply to all of them.
 * <p>
 * Internally, this will check if the {@link KeyPath} has already been resolved with
 * {@link #resolveKeyPath(KeyPath)} and will resolve it if it hasn't.
 */
public <T> void addValueCallback(
    final KeyPath keyPath, final T property, final LottieValueCallback<T> callback) {
  if (compositionLayer == null) {
    lazyCompositionTasks.add(new LazyCompositionTask() {
      @Override
      public void run(LottieComposition composition) {
        addValueCallback(keyPath, property, callback);
      }
    });
    return;
  }
  boolean invalidate;
  if (keyPath == KeyPath.COMPOSITION) {
    compositionLayer.addValueCallback(property, callback);
    invalidate = true;
  } else if (keyPath.getResolvedElement() != null) {
    keyPath.getResolvedElement().addValueCallback(property, callback);
    invalidate = true;
  } else {
    List<KeyPath> elements = resolveKeyPath(keyPath);

    for (int i = 0; i < elements.size(); i++) {
      //noinspection ConstantConditions
      elements.get(i).getResolvedElement().addValueCallback(property, callback);
    }
    invalidate = !elements.isEmpty();
  }
  if (invalidate) {
    invalidateSelf();
    if (property == LottieProperty.TIME_REMAP) {
      // Time remapping values are read in setProgress. In order for the new value
      // to apply, we have to re-set the progress with the current progress so that the
      // time remapping can be reapplied.
      setProgress(getProgress());
    }
  }
}
 
Example #9
Source File: KeyPathTest.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
private void assertSize(int size, String... keys) {
  KeyPath keyPath = new KeyPath(keys);
  List<KeyPath> resolvedKeyPaths = lottieDrawable.resolveKeyPath(keyPath);
  assertEquals(size, resolvedKeyPaths.size());
}
 
Example #10
Source File: LottieAnimationView.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
private void init(@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
  TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.LottieAnimationView, defStyleAttr, 0);
  if (!isInEditMode()) {
    cacheComposition = ta.getBoolean(R.styleable.LottieAnimationView_lottie_cacheComposition, true);
    boolean hasRawRes = ta.hasValue(R.styleable.LottieAnimationView_lottie_rawRes);
    boolean hasFileName = ta.hasValue(R.styleable.LottieAnimationView_lottie_fileName);
    boolean hasUrl = ta.hasValue(R.styleable.LottieAnimationView_lottie_url);
    if (hasRawRes && hasFileName) {
      throw new IllegalArgumentException("lottie_rawRes and lottie_fileName cannot be used at " +
          "the same time. Please use only one at once.");
    } else if (hasRawRes) {
      int rawResId = ta.getResourceId(R.styleable.LottieAnimationView_lottie_rawRes, 0);
      if (rawResId != 0) {
        setAnimation(rawResId);
      }
    } else if (hasFileName) {
      String fileName = ta.getString(R.styleable.LottieAnimationView_lottie_fileName);
      if (fileName != null) {
        setAnimation(fileName);
      }
    } else if (hasUrl) {
      String url = ta.getString(R.styleable.LottieAnimationView_lottie_url);
      if (url != null) {
        setAnimationFromUrl(url);
      }
    }

    setFallbackResource(ta.getResourceId(R.styleable.LottieAnimationView_lottie_fallbackRes, 0));
  }
  if (ta.getBoolean(R.styleable.LottieAnimationView_lottie_autoPlay, false)) {
    wasAnimatingWhenDetached = true;
    autoPlay = true;
  }

  if (ta.getBoolean(R.styleable.LottieAnimationView_lottie_loop, false)) {
    lottieDrawable.setRepeatCount(LottieDrawable.INFINITE);
  }

  if (ta.hasValue(R.styleable.LottieAnimationView_lottie_repeatMode)) {
    setRepeatMode(ta.getInt(R.styleable.LottieAnimationView_lottie_repeatMode,
        LottieDrawable.RESTART));
  }

  if (ta.hasValue(R.styleable.LottieAnimationView_lottie_repeatCount)) {
    setRepeatCount(ta.getInt(R.styleable.LottieAnimationView_lottie_repeatCount,
        LottieDrawable.INFINITE));
  }

  if (ta.hasValue(R.styleable.LottieAnimationView_lottie_speed)) {
    setSpeed(ta.getFloat(R.styleable.LottieAnimationView_lottie_speed, 1f));
  }

  setImageAssetsFolder(ta.getString(R.styleable.LottieAnimationView_lottie_imageAssetsFolder));
  setProgress(ta.getFloat(R.styleable.LottieAnimationView_lottie_progress, 0));
  enableMergePathsForKitKatAndAbove(ta.getBoolean(
      R.styleable.LottieAnimationView_lottie_enableMergePathsForKitKatAndAbove, false));
  if (ta.hasValue(R.styleable.LottieAnimationView_lottie_colorFilter)) {
    SimpleColorFilter filter = new SimpleColorFilter(
        ta.getColor(R.styleable.LottieAnimationView_lottie_colorFilter, Color.TRANSPARENT));
    KeyPath keyPath = new KeyPath("**");
    LottieValueCallback<ColorFilter> callback = new LottieValueCallback<ColorFilter>(filter);
    addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback);
  }
  if (ta.hasValue(R.styleable.LottieAnimationView_lottie_scale)) {
    lottieDrawable.setScale(ta.getFloat(R.styleable.LottieAnimationView_lottie_scale, 1f));
  }

  if (ta.hasValue(R.styleable.LottieAnimationView_lottie_renderMode)) {
    int renderModeOrdinal = ta.getInt(R.styleable.LottieAnimationView_lottie_renderMode, RenderMode.AUTOMATIC.ordinal());
    if (renderModeOrdinal >= RenderMode.values().length) {
      renderModeOrdinal = RenderMode.AUTOMATIC.ordinal();
    }
    setRenderMode(RenderMode.values()[renderModeOrdinal]);
  }

  if (getScaleType() != null) {
    lottieDrawable.setScaleType(getScaleType());
  }
  ta.recycle();

  lottieDrawable.setSystemAnimationsAreEnabled(Utils.getAnimationScale(getContext()) != 0f);

  enableOrDisableHardwareLayer();
  isInitialized = true;
}
 
Example #11
Source File: BaseLayer.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
void resolveChildKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
}
 
Example #12
Source File: ShapeLayer.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void resolveChildKeyPath(KeyPath keyPath, int depth, List<KeyPath> accumulator,
    KeyPath currentPartialKeyPath) {
  contentGroup.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath);
}
 
Example #13
Source File: RepeaterContent.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  MiscUtils.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath, this);
}
 
Example #14
Source File: EllipseContent.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  MiscUtils.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath, this);
}
 
Example #15
Source File: RectangleContent.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override
public void resolveKeyPath(KeyPath keyPath, int depth, List<KeyPath> accumulator,
                           KeyPath currentPartialKeyPath) {
  MiscUtils.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath, this);
}
 
Example #16
Source File: GradientFillContent.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  MiscUtils.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath, this);
}
 
Example #17
Source File: FillContent.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  MiscUtils.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath, this);
}
 
Example #18
Source File: PolystarContent.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  MiscUtils.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath, this);
}
 
Example #19
Source File: BaseStrokeContent.java    From lottie-android with Apache License 2.0 4 votes vote down vote up
@Override public void resolveKeyPath(
    KeyPath keyPath, int depth, List<KeyPath> accumulator, KeyPath currentPartialKeyPath) {
  MiscUtils.resolveKeyPath(keyPath, depth, accumulator, currentPartialKeyPath, this);
}
 
Example #20
Source File: LottieAnimationViewPropertyManager.java    From lottie-react-native with Apache License 2.0 4 votes vote down vote up
/**
 * Updates the view with changed fields.
 * Majority of the properties here are independent so they are has to be reset to null
 * as soon as view is updated with the value.
 *
 * The only exception from this rule is the group of the properties for the animation.
 * For now this is animationName and cacheStrategy. These two properties are should be set
 * simultaneously if the dirty flag is set.
 */
public void commitChanges() {
  LottieAnimationView view = viewWeakReference.get();
  if (view == null) {
    return;
  }

  if (animationJson != null) {
    view.setAnimationFromJson(animationJson, Integer.toString(animationJson.hashCode()));
    animationJson = null;
  }

  if (animationNameDirty) {
    view.setAnimation(animationName);
    animationNameDirty = false;
  }

  if (progress != null) {
    view.setProgress(progress);
    progress = null;
  }

  if (loop != null) {
    view.setRepeatCount(loop ? LottieDrawable.INFINITE : 0);
    loop = null;
  }

  if (speed != null) {
    view.setSpeed(speed);
    speed = null;
  }

  if (scaleType != null) {
    view.setScaleType(scaleType);
    scaleType = null;
  }

  if (renderMode != null) {
    view.setRenderMode(renderMode);
    renderMode = null;
  }

  if (imageAssetsFolder != null) {
    view.setImageAssetsFolder(imageAssetsFolder);
    imageAssetsFolder = null;
  }

  if (enableMergePaths != null) {
    view.enableMergePathsForKitKatAndAbove(enableMergePaths);
    enableMergePaths = null;
  }

  if (colorFilters != null && colorFilters.size() > 0) {
    for (int i = 0 ; i < colorFilters.size() ; i++) {
      ReadableMap current = colorFilters.getMap(i);
      String color = current.getString("color");
      String path = current.getString("keypath");
      SimpleColorFilter colorFilter = new SimpleColorFilter(Color.parseColor(color));
      KeyPath keyPath = new KeyPath(path, "**");
      LottieValueCallback<ColorFilter> callback = new LottieValueCallback<>(colorFilter);
      view.addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback);
    }
  }
}
 
Example #21
Source File: MiscUtils.java    From lottie-android with Apache License 2.0 3 votes vote down vote up
/**
 * Helper method for any {@link KeyPathElementContent} that will check if the content
 * fully matches the keypath then will add itself as the final key, resolve it, and add
 * it to the accumulator list.
 *
 * Any {@link KeyPathElementContent} should call through to this as its implementation of
 * {@link KeyPathElementContent#resolveKeyPath(KeyPath, int, List, KeyPath)}.
 */
public static void resolveKeyPath(KeyPath keyPath, int depth, List<KeyPath> accumulator,
    KeyPath currentPartialKeyPath, KeyPathElementContent content) {
  if (keyPath.fullyResolvesTo(content.getName(), depth)) {
    currentPartialKeyPath = currentPartialKeyPath.addKey(content.getName());
    accumulator.add(currentPartialKeyPath.resolve(content));
  }
}
 
Example #22
Source File: LottieAnimationView.java    From lottie-android with Apache License 2.0 2 votes vote down vote up
/**
 * Takes a {@link KeyPath}, potentially with wildcards or globstars and resolve it to a list of
 * zero or more actual {@link KeyPath Keypaths} that exist in the current animation.
 *
 * If you want to set value callbacks for any of these values, it is recommended to use the
 * returned {@link KeyPath} objects because they will be internally resolved to their content
 * and won't trigger a tree walk of the animation contents when applied.
 */
public List<KeyPath> resolveKeyPath(KeyPath keyPath) {
  return lottieDrawable.resolveKeyPath(keyPath);
}
 
Example #23
Source File: LottieAnimationView.java    From lottie-android with Apache License 2.0 2 votes vote down vote up
/**
 * Add a property callback for the specified {@link KeyPath}. This {@link KeyPath} can resolve
 * to multiple contents. In that case, the callback's value will apply to all of them.
 *
 * Internally, this will check if the {@link KeyPath} has already been resolved with
 * {@link #resolveKeyPath(KeyPath)} and will resolve it if it hasn't.
 */
public <T> void addValueCallback(KeyPath keyPath, T property, LottieValueCallback<T> callback) {
  lottieDrawable.addValueCallback(keyPath, property, callback);
}