-
public class LottieCompositionFactoryHelpers to create or cache a LottieComposition.
All factory methods take a cache key. The animation will be stored in an LRU cache for future use. In-progress tasks will also be held so they can be returned for subsequent requests for the same animation prior to the cache being populated.
-
-
Method Summary
Modifier and Type Method Description static voidsetMaxCacheSize(int size)Set the maximum number of compositions to keep cached in memory. static voidclearCache(Context context)static voidregisterLottieTaskIdleListener(LottieTaskIdleListener listener)Use this to register a callback for when the composition factory is idle or not.This can be used to provide data to an espresso idling resource.Refer to FragmentVisibilityTests and its LottieIdlingResource in the Lottie repo foran example. static voidunregisterLottieTaskIdleListener(LottieTaskIdleListener listener)static LottieTask<LottieComposition>fromUrl(Context context, String url)Fetch an animation from an http url. static LottieTask<LottieComposition>fromUrl(Context context, String url, @Nullable() String cacheKey)Fetch an animation from an http url. static LottieResult<LottieComposition>fromUrlSync(Context context, String url)Fetch an animation from an http url. static LottieResult<LottieComposition>fromUrlSync(Context context, String url, @Nullable() String cacheKey)Fetch an animation from an http url. static LottieTask<LottieComposition>fromAsset(Context context, String fileName)Parse an animation from src/main/assets. static LottieTask<LottieComposition>fromAsset(Context context, String fileName, @Nullable() String cacheKey)Parse an animation from src/main/assets. static LottieResult<LottieComposition>fromAssetSync(Context context, String fileName)Parse an animation from src/main/assets. static LottieResult<LottieComposition>fromAssetSync(Context context, String fileName, @Nullable() String cacheKey)Parse an animation from src/main/assets. static LottieTask<LottieComposition>fromRawRes(Context context, @RawRes() int rawRes)Parse an animation from raw/res. static LottieTask<LottieComposition>fromRawRes(Context context, @RawRes() int rawRes, @Nullable() String cacheKey)Parse an animation from raw/res. static LottieResult<LottieComposition>fromRawResSync(Context context, @RawRes() int rawRes)Parse an animation from raw/res. static LottieResult<LottieComposition>fromRawResSync(Context context, @RawRes() int rawRes, @Nullable() String cacheKey)Parse an animation from raw/res. static LottieTask<LottieComposition>fromJsonInputStream(InputStream stream, @Nullable() String cacheKey)Auto-closes the stream. static LottieTask<LottieComposition>fromJsonInputStream(InputStream stream, @Nullable() String cacheKey, boolean close)static LottieResult<LottieComposition>fromJsonInputStreamSync(InputStream stream, @Nullable() String cacheKey)Return a LottieComposition for the given InputStream to json. static LottieResult<LottieComposition>fromJsonInputStreamSync(InputStream stream, @Nullable() String cacheKey, boolean close)Return a LottieComposition for the given InputStream to json. static LottieTask<LottieComposition>fromJson(JSONObject json, @Nullable() String cacheKey)static LottieResult<LottieComposition>fromJsonSync(JSONObject json, @Nullable() String cacheKey)Prefer passing in the json string directly. static LottieTask<LottieComposition>fromJsonString(String json, @Nullable() String cacheKey)static LottieResult<LottieComposition>fromJsonStringSync(String json, @Nullable() String cacheKey)Return a LottieComposition for the specified raw json string.If loading from a file, it is preferable to use the InputStream or rawRes version. static LottieTask<LottieComposition>fromJsonSource(Source source, @Nullable() String cacheKey)static LottieResult<LottieComposition>fromJsonSourceSync(Source source, @Nullable() String cacheKey)static LottieResult<LottieComposition>fromJsonSourceSync(Source source, @Nullable() String cacheKey, boolean close)static LottieTask<LottieComposition>fromJsonReader(JsonReader reader, @Nullable() String cacheKey)static LottieResult<LottieComposition>fromJsonReaderSync(JsonReader reader, @Nullable() String cacheKey)static LottieResult<LottieComposition>fromJsonReaderSync(JsonReader reader, @Nullable() String cacheKey, boolean close)static LottieTask<LottieComposition>fromZipStream(ZipInputStream inputStream, @Nullable() String cacheKey)In this overload, embedded fonts will NOT be parsed. static LottieTask<LottieComposition>fromZipStream(ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)In this overload, embedded fonts will NOT be parsed. static LottieTask<LottieComposition>fromZipStream(Context context, ZipInputStream inputStream, @Nullable() String cacheKey)static LottieTask<LottieComposition>fromZipStream(Context context, ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)static LottieResult<LottieComposition>fromZipStreamSync(ZipInputStream inputStream, @Nullable() String cacheKey)Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist. static LottieResult<LottieComposition>fromZipStreamSync(ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist. static LottieResult<LottieComposition>fromZipStreamSync(@Nullable() Context context, ZipInputStream inputStream, @Nullable() String cacheKey)Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist. static LottieResult<LottieComposition>fromZipStreamSync(@Nullable() Context context, ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist. -
-
Method Detail
-
setMaxCacheSize
static void setMaxCacheSize(int size)
Set the maximum number of compositions to keep cached in memory.This must be
{@literal >}0.
-
clearCache
static void clearCache(Context context)
-
registerLottieTaskIdleListener
static void registerLottieTaskIdleListener(LottieTaskIdleListener listener)
Use this to register a callback for when the composition factory is idle or not.This can be used to provide data to an espresso idling resource.Refer to FragmentVisibilityTests and its LottieIdlingResource in the Lottie repo foran example.
-
unregisterLottieTaskIdleListener
static void unregisterLottieTaskIdleListener(LottieTaskIdleListener listener)
-
fromUrl
static LottieTask<LottieComposition> fromUrl(Context context, String url)
Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk forfuture use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think youmight need an animation in the future.
To skip the cache, add null as a third parameter.
-
fromUrl
static LottieTask<LottieComposition> fromUrl(Context context, String url, @Nullable() String cacheKey)
Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk forfuture use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think youmight need an animation in the future.
-
fromUrlSync
@WorkerThread() static LottieResult<LottieComposition> fromUrlSync(Context context, String url)
Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk forfuture use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think youmight need an animation in the future.
-
fromUrlSync
@WorkerThread() static LottieResult<LottieComposition> fromUrlSync(Context context, String url, @Nullable() String cacheKey)
Fetch an animation from an http url. Once it is downloaded once, Lottie will cache the file to disk forfuture use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think youmight need an animation in the future.
-
fromAsset
static LottieTask<LottieComposition> fromAsset(Context context, String fileName)
Parse an animation from src/main/assets. It is recommended to use fromRawRes instead.The asset file name will be used as a cache key so future usages won't have to parse the json again.However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
To skip the cache, add null as a third parameter.
-
fromAsset
static LottieTask<LottieComposition> fromAsset(Context context, String fileName, @Nullable() String cacheKey)
Parse an animation from src/main/assets. It is recommended to use fromRawRes instead.The asset file name will be used as a cache key so future usages won't have to parse the json again.However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
Pass null as the cache key to skip the cache.
-
fromAssetSync
@WorkerThread() static LottieResult<LottieComposition> fromAssetSync(Context context, String fileName)
Parse an animation from src/main/assets. It is recommended to use fromRawRes instead.The asset file name will be used as a cache key so future usages won't have to parse the json again.However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
To skip the cache, add null as a third parameter.
-
fromAssetSync
@WorkerThread() static LottieResult<LottieComposition> fromAssetSync(Context context, String fileName, @Nullable() String cacheKey)
Parse an animation from src/main/assets. It is recommended to use fromRawRes instead.The asset file name will be used as a cache key so future usages won't have to parse the json again.However, if your animation has images, you may package the json and images as a single flattened zip file in assets.
Pass null as the cache key to skip the cache.
-
fromRawRes
static LottieTask<LottieComposition> fromRawRes(Context context, @RawRes() int rawRes)
Parse an animation from raw/res. This is recommended over putting your animation in assets becauseit uses a hard reference to R.The resource id will be used as a cache key so future usages won't parse the json again.Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).The Activity won't be leaked.
To skip the cache, add null as a third parameter.
-
fromRawRes
static LottieTask<LottieComposition> fromRawRes(Context context, @RawRes() int rawRes, @Nullable() String cacheKey)
Parse an animation from raw/res. This is recommended over putting your animation in assets becauseit uses a hard reference to R.The resource id will be used as a cache key so future usages won't parse the json again.Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).The Activity won't be leaked.
Pass null as the cache key to skip caching.
-
fromRawResSync
@WorkerThread() static LottieResult<LottieComposition> fromRawResSync(Context context, @RawRes() int rawRes)
Parse an animation from raw/res. This is recommended over putting your animation in assets becauseit uses a hard reference to R.The resource id will be used as a cache key so future usages won't parse the json again.Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).The Activity won't be leaked.
To skip the cache, add null as a third parameter.
-
fromRawResSync
@WorkerThread() static LottieResult<LottieComposition> fromRawResSync(Context context, @RawRes() int rawRes, @Nullable() String cacheKey)
Parse an animation from raw/res. This is recommended over putting your animation in assets becauseit uses a hard reference to R.The resource id will be used as a cache key so future usages won't parse the json again.Note: to correctly load dark mode (-night) resources, make sure you pass Activity as a context (instead of e.g. the application context).The Activity won't be leaked.
Pass null as the cache key to skip caching.
-
fromJsonInputStream
static LottieTask<LottieComposition> fromJsonInputStream(InputStream stream, @Nullable() String cacheKey)
Auto-closes the stream.
-
fromJsonInputStream
static LottieTask<LottieComposition> fromJsonInputStream(InputStream stream, @Nullable() String cacheKey, boolean close)
-
fromJsonInputStreamSync
@WorkerThread() static LottieResult<LottieComposition> fromJsonInputStreamSync(InputStream stream, @Nullable() String cacheKey)
Return a LottieComposition for the given InputStream to json.
-
fromJsonInputStreamSync
@WorkerThread() static LottieResult<LottieComposition> fromJsonInputStreamSync(InputStream stream, @Nullable() String cacheKey, boolean close)
Return a LottieComposition for the given InputStream to json.
-
fromJson
@Deprecated() static LottieTask<LottieComposition> fromJson(JSONObject json, @Nullable() String cacheKey)
-
fromJsonSync
@Deprecated()@WorkerThread() static LottieResult<LottieComposition> fromJsonSync(JSONObject json, @Nullable() String cacheKey)
Prefer passing in the json string directly. This method just calls `toString()` on your JSONObject.If you are loading this animation from the network, just use the response body string instead ofparsing it first for improved performance.
-
fromJsonString
static LottieTask<LottieComposition> fromJsonString(String json, @Nullable() String cacheKey)
-
fromJsonStringSync
@WorkerThread() static LottieResult<LottieComposition> fromJsonStringSync(String json, @Nullable() String cacheKey)
Return a LottieComposition for the specified raw json string.If loading from a file, it is preferable to use the InputStream or rawRes version.
-
fromJsonSource
static LottieTask<LottieComposition> fromJsonSource(Source source, @Nullable() String cacheKey)
-
fromJsonSourceSync
@WorkerThread() static LottieResult<LottieComposition> fromJsonSourceSync(Source source, @Nullable() String cacheKey)
-
fromJsonSourceSync
@WorkerThread() static LottieResult<LottieComposition> fromJsonSourceSync(Source source, @Nullable() String cacheKey, boolean close)
-
fromJsonReader
static LottieTask<LottieComposition> fromJsonReader(JsonReader reader, @Nullable() String cacheKey)
-
fromJsonReaderSync
@WorkerThread() static LottieResult<LottieComposition> fromJsonReaderSync(JsonReader reader, @Nullable() String cacheKey)
-
fromJsonReaderSync
@WorkerThread() static LottieResult<LottieComposition> fromJsonReaderSync(JsonReader reader, @Nullable() String cacheKey, boolean close)
-
fromZipStream
static LottieTask<LottieComposition> fromZipStream(ZipInputStream inputStream, @Nullable() String cacheKey)
In this overload, embedded fonts will NOT be parsed. If your zip file has custom fonts, use the overloadthat takes Context as the first parameter.
-
fromZipStream
static LottieTask<LottieComposition> fromZipStream(ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)
In this overload, embedded fonts will NOT be parsed. If your zip file has custom fonts, use the overloadthat takes Context as the first parameter.
-
fromZipStream
static LottieTask<LottieComposition> fromZipStream(Context context, ZipInputStream inputStream, @Nullable() String cacheKey)
-
fromZipStream
static LottieTask<LottieComposition> fromZipStream(Context context, ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)
-
fromZipStreamSync
static LottieResult<LottieComposition> fromZipStreamSync(ZipInputStream inputStream, @Nullable() String cacheKey)
Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist.
In this overload, embedded fonts will NOT be parsed. If your zip file has custom fonts, use the overloadthat takes Context as the first parameter.
The ZipInputStream will be automatically closed at the end. If you would like to keep it open, use the overloadwith a close parameter and pass in false.
-
fromZipStreamSync
static LottieResult<LottieComposition> fromZipStreamSync(ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)
Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist.
In this overload, embedded fonts will NOT be parsed. If your zip file has custom fonts, use the overloadthat takes Context as the first parameter.
-
fromZipStreamSync
@WorkerThread() static LottieResult<LottieComposition> fromZipStreamSync(@Nullable() Context context, ZipInputStream inputStream, @Nullable() String cacheKey)
Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist.
The ZipInputStream will be automatically closed at the end. If you would like to keep it open, use the overloadwith a close parameter and pass in false.
- Parameters:
context- is optional and only needed if your zip file contains ttf or otf fonts.
-
fromZipStreamSync
@WorkerThread() static LottieResult<LottieComposition> fromZipStreamSync(@Nullable() Context context, ZipInputStream inputStream, @Nullable() String cacheKey, boolean close)
Parses a zip input stream into a Lottie composition.Your zip file should just be a folder with your json file and images zipped together.It will automatically store and configure any images inside the animation if they exist.
- Parameters:
context- is optional and only needed if your zip file contains ttf or otf fonts.
-
-
-
-