Class LottieCompositionFactory


  • public class LottieCompositionFactory
    extends Object
    Helpers 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 Detail

      • setMaxCacheSize

        public static void setMaxCacheSize​(int size)
        Set the maximum number of compositions to keep cached in memory. This must be > 0.
      • clearCache

        public static void clearCache​(Context context)
      • fromUrl

        public 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 for future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you might need an animation in the future.

        To skip the cache, add null as a third parameter.

      • fromUrl

        public 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 for future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you might need an animation in the future.
      • fromUrlSync

        @WorkerThread
        public 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 for future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you might need an animation in the future.
      • fromUrlSync

        @WorkerThread
        public 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 for future use. Because of this, you may call `fromUrl` ahead of time to warm the cache if you think you might need an animation in the future.
      • fromAsset

        public static LottieTask<LottieComposition> fromAsset​(Context context,
                                                              String fileName)
        Parse an animation from src/main/assets. It is recommended to use fromRawRes(Context, int) 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.

        See Also:
        fromZipStream(ZipInputStream, String)
      • fromAsset

        public static LottieTask<LottieComposition> fromAsset​(Context context,
                                                              String fileName,
                                                              @Nullable
                                                              String cacheKey)
        Parse an animation from src/main/assets. It is recommended to use fromRawRes(Context, int) 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.

        See Also:
        fromZipStream(ZipInputStream, String)
      • fromAssetSync

        @WorkerThread
        public static LottieResult<LottieComposition> fromAssetSync​(Context context,
                                                                    String fileName)
        Parse an animation from src/main/assets. It is recommended to use fromRawRes(Context, int) 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.

        See Also:
        fromZipStreamSync(ZipInputStream, String)
      • fromAssetSync

        @WorkerThread
        public static LottieResult<LottieComposition> fromAssetSync​(Context context,
                                                                    String fileName,
                                                                    @Nullable
                                                                    String cacheKey)
        Parse an animation from src/main/assets. It is recommended to use fromRawRes(Context, int) 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.

        See Also:
        fromZipStreamSync(ZipInputStream, String)
      • fromRawRes

        public static LottieTask<LottieComposition> fromRawRes​(Context context,
                                                               @RawRes
                                                               int rawRes)
        Parse an animation from raw/res. This is recommended over putting your animation in assets because it 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

        public 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 because it 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
        public static LottieResult<LottieComposition> fromRawResSync​(Context context,
                                                                     @RawRes
                                                                     int rawRes)
        Parse an animation from raw/res. This is recommended over putting your animation in assets because it 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
        public 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 because it 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.

      • fromJsonSync

        @Deprecated
        @WorkerThread
        public static LottieResult<LottieComposition> fromJsonSync​(JSONObject json,
                                                                   @Nullable
                                                                   String cacheKey)
        Deprecated.
        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 of parsing it first for improved performance.
      • fromJsonStringSync

        @WorkerThread
        public 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.
      • fromZipStreamSync

        @WorkerThread
        public 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.