Lottie-ios: how to use colorfilter like android?

Created on 26 Jun 2017  ·  12Comments  ·  Source: airbnb/lottie-ios

in Android, You can add a color filter to the whole animation, a specific layer, or specific content within a layer:

// Any class that conforms to the ColorFilter interface
final PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.LIGHTEN);

// Adding a color filter to the whole view
animationView.addColorFilter(colorFilter);

// Adding a color filter to a specific layer
animationView.addColorFilterToLayer("hello_layer", colorFilter);

// Adding a color filter to specfic content on the "hello_layer"
animationView.addColorFilterToContent("hello_layer", "hello", colorFilter);

// Clear all color filters
animationView.clearColorFilters();

but i can't find code in this framework

Most helpful comment

Hi, thank you very much for the great library. You can change the color of animation directly in json file editing the following line
"c":{"k":[0,0.7,1,1]}
where the parameters are the color ;)

All 12 comments

Lottie 2.0 is out and supports dynamically changing colors!
The interface is slightly different than Android. You have to know the path to the color in after effects. Example:
[lottieAnimation setValue:[UIColor greenColor] forKeypath:@"Layer 1.Shape 1.Fill 1" atFrame:@0];

Hi, thank you very much for the great library. You can change the color of animation directly in json file editing the following line
"c":{"k":[0,0.7,1,1]}
where the parameters are the color ;)

@agueroveraalvaro Yes, but that will change the color for ALL animations that reference the file. Also if the color is animated the value for K will not be a number array but another dictionary, so the modification will invalidate the JSON. A more efficient, less destructive and safer way is to use our programatic recoloring that is documented here https://github.com/airbnb/lottie-ios#changing-animations-at-runtime
It supports colors that are animated too.

Yes exactly, i was forgotten that it only works in simple animation and your documentation is so much better. I have a question, in android i can't found the correct syntax to change color
animationView.addColorFilter(colorFilter);
addColorFilter doesn't work and i try to use
animation_view.setColorFilter(colorFilter);
with the following error :

java.lang.IllegalArgumentException: This shouldn't be used.

Ok, thanks very much for the orientation, great library :)

@agueroveraalvaro thanks for sharing this knowledge, it work successfully. "c":{"k":[0,0.7,1,1]}, need to divide original RGB values from 255 and put result.

LOTAnimationView provides - (void)logHierarchyKeypaths which will recursively log all settable keypaths for the animation. This is helpful for changing animationations at runtime.

@englishsister
animationView.resolveKeyPath(KeyPath("**")) will return a list of all available keypaths. You can iterate through them and log them.

In fact, we do that here.

animationView.resolveKeyPath(KeyPath("**")) will return a list of all available keypaths. You can iterate through them and log them.

This return an error in latest SDK that Value of type 'LOTAnimationView' has no member 'resolveKeyPath'

@kashiftriffort
Can you share some code how you did it?

@gpeal 非常感谢,我当时用了logHierarchyKeypaths获取到了我需要更改的路径, 因为再最新的SDK中我没有找到“ resolveKeyPath”

Was this page helpful?
0 / 5 - 0 ratings

Related issues

loganblevins picture loganblevins  ·  3Comments

CentrumGuy picture CentrumGuy  ·  4Comments

rajeshbeats picture rajeshbeats  ·  3Comments

Ushaka picture Ushaka  ·  5Comments

ricardohochman picture ricardohochman  ·  4Comments