Class LottieAnimationView

    • Constructor Detail

      • LottieAnimationView

        public LottieAnimationView​(Context context)
      • LottieAnimationView

        public LottieAnimationView​(Context context,
                                   AttributeSet attrs,
                                   int defStyleAttr)
    • Method Detail

      • setImageResource

        public void setImageResource​(int resId)
        Overrides:
        setImageResource in class androidx.appcompat.widget.AppCompatImageView
      • setImageDrawable

        public void setImageDrawable​(Drawable drawable)
        Overrides:
        setImageDrawable in class androidx.appcompat.widget.AppCompatImageView
      • setImageBitmap

        public void setImageBitmap​(Bitmap bm)
        Overrides:
        setImageBitmap in class androidx.appcompat.widget.AppCompatImageView
      • invalidate

        public void invalidate()
        Overrides:
        invalidate in class View
      • setIgnoreDisabledSystemAnimations

        public void setIgnoreDisabledSystemAnimations​(boolean ignore)
        Allows ignoring system animations settings, therefore allowing animations to run even if they are disabled.

        Defaults to false.

        Parameters:
        ignore - if true animations will run even when they are disabled in the system settings.
      • enableMergePathsForKitKatAndAbove

        public void enableMergePathsForKitKatAndAbove​(boolean enable)
        Enable this to get merge path support for devices running KitKat (19) and above.

        Merge paths currently don't work if the the operand shape is entirely contained within the first shape. If you need to cut out one shape from another shape, use an even-odd fill type instead of using merge paths.

      • isMergePathsEnabledForKitKatAndAbove

        public boolean isMergePathsEnabledForKitKatAndAbove()
        Returns whether merge paths are enabled for KitKat and above.
      • setClipToCompositionBounds

        public void setClipToCompositionBounds​(boolean clipToCompositionBounds)
        Sets whether or not Lottie should clip to the original animation composition bounds. When set to true, the parent view may need to disable clipChildren so Lottie can render outside of the LottieAnimationView bounds. Defaults to true.
      • getClipToCompositionBounds

        public boolean getClipToCompositionBounds()
        Gets whether or not Lottie should clip to the original animation composition bounds. Defaults to true.
      • setCacheComposition

        public void setCacheComposition​(boolean cacheComposition)
        If set to true, all future compositions that are set will be cached so that they don't need to be parsed next time they are loaded. This won't apply to compositions that have already been loaded.

        Defaults to true.

        R.attr.lottie_cacheComposition

      • setOutlineMasksAndMattes

        public void setOutlineMasksAndMattes​(boolean outline)
        Enable this to debug slow animations by outlining masks and mattes. The performance overhead of the masks and mattes will be proportional to the surface area of all of the masks/mattes combined.

        DO NOT leave this enabled in production.

      • setAnimation

        public void setAnimation​(@RawRes
                                 int rawRes)
        Sets the animation from a file in the raw directory. This will load and deserialize the file asynchronously.
      • setAnimation

        public void setAnimation​(String assetName)
      • setAnimationFromJson

        public void setAnimationFromJson​(String jsonString,
                                         @Nullable
                                         String cacheKey)
        Sets the animation from json string. This is the ideal API to use when loading an animation over the network because you can use the raw response body here and a conversion to a JSONObject never has to be done.
      • setAnimation

        public void setAnimation​(InputStream stream,
                                 @Nullable
                                 String cacheKey)
        Sets the animation from an arbitrary InputStream. This will load and deserialize the file asynchronously.

        This is particularly useful for animations loaded from the network. You can fetch the bodymovin json from the network and pass it directly here.

      • setAnimationFromUrl

        public void setAnimationFromUrl​(String url)
        Load a lottie animation from a url. The url can be a json file or a zip file. Use a zip file if you have images. Simply zip them together and lottie will unzip and link the images automatically.

        Under the hood, Lottie uses Java HttpURLConnection because it doesn't require any transitive networking dependencies. It will download the file to the application cache under a temporary name. If the file successfully parses to a composition, it will rename the temporary file to one that can be accessed immediately for subsequent requests. If the file does not parse to a composition, the temporary file will be deleted.

        You can replace the default network stack or cache handling with a global LottieConfig

        See Also:
        LottieConfig.Builder, Lottie.initialize(LottieConfig)
      • setAnimationFromUrl

        public void setAnimationFromUrl​(String url,
                                        @Nullable
                                        String cacheKey)
        Load a lottie animation from a url. The url can be a json file or a zip file. Use a zip file if you have images. Simply zip them together and lottie will unzip and link the images automatically.

        Under the hood, Lottie uses Java HttpURLConnection because it doesn't require any transitive networking dependencies. It will download the file to the application cache under a temporary name. If the file successfully parses to a composition, it will rename the temporary file to one that can be accessed immediately for subsequent requests. If the file does not parse to a composition, the temporary file will be deleted.

        You can replace the default network stack or cache handling with a global LottieConfig

        See Also:
        LottieConfig.Builder, Lottie.initialize(LottieConfig)
      • setFailureListener

        public void setFailureListener​(@Nullable
                                       LottieListener<Throwable> failureListener)
        Set a default failure listener that will be called if any of the setAnimation APIs fail for any reason. This can be used to replace the default behavior.

        The default behavior will log any network errors and rethrow all other exceptions.

        If you are loading an animation from the network, errors may occur if your user has no internet. You can use this listener to retry the download or you can have it default to an error drawable with setFallbackResource(int).

        Unless you are using setAnimationFromUrl(String), errors are unexpected.

        Set the listener to null to revert to the default behavior.

      • setFallbackResource

        public void setFallbackResource​(@DrawableRes
                                        int fallbackResource)
        Set a drawable that will be rendered if the LottieComposition fails to load for any reason. Unless you are using setAnimationFromUrl(String), this is an unexpected error and you should handle it with setFailureListener(LottieListener).

        If this is a network animation, you may use this to show an error to the user or you can use a failure listener to retry the download.

      • setComposition

        public void setComposition​(@NonNull
                                   LottieComposition composition)
        Sets a composition. You can set a default cache strategy if this view was inflated with xml by using R.attr.lottie_cacheComposition.
      • hasMasks

        public boolean hasMasks()
        Returns whether or not any layers in this composition has masks.
      • hasMatte

        public boolean hasMatte()
        Returns whether or not any layers in this composition has a matte layer.
      • playAnimation

        @MainThread
        public void playAnimation()
        Plays the animation from the beginning. If speed is < 0, it will start at the end and play towards the beginning
      • resumeAnimation

        @MainThread
        public void resumeAnimation()
        Continues playing the animation from its current position. If speed < 0, it will play backwards from the current position.
      • setMinFrame

        public void setMinFrame​(int startFrame)
        Sets the minimum frame that the animation will start from when playing or looping.
      • setMinProgress

        public void setMinProgress​(float startProgress)
        Sets the minimum progress that the animation will start from when playing or looping.
      • setMaxFrame

        public void setMaxFrame​(int endFrame)
        Sets the maximum frame that the animation will end at when playing or looping.

        The value will be clamped to the composition bounds. For example, setting Integer.MAX_VALUE would result in the same thing as composition.endFrame.

      • setMaxProgress

        public void setMaxProgress​(@FloatRange(from=0.0,to=1.0)
                                   float endProgress)
        Sets the maximum progress that the animation will end at when playing or looping.
      • setMinFrame

        public void setMinFrame​(String markerName)
        Sets the minimum frame to the start time of the specified marker.
        Throws:
        IllegalArgumentException - if the marker is not found.
      • setMaxFrame

        public void setMaxFrame​(String markerName)
        Sets the maximum frame to the start time + duration of the specified marker.
        Throws:
        IllegalArgumentException - if the marker is not found.
      • setMinAndMaxFrame

        public void setMinAndMaxFrame​(String markerName)
        Sets the minimum and maximum frame to the start time and start time + duration of the specified marker.
        Throws:
        IllegalArgumentException - if the marker is not found.
      • setMinAndMaxFrame

        public void setMinAndMaxFrame​(String startMarkerName,
                                      String endMarkerName,
                                      boolean playEndMarkerStartFrame)
        Sets the minimum and maximum frame to the start marker start and the maximum frame to the end marker start. playEndMarkerStartFrame determines whether or not to play the frame that the end marker is on. If the end marker represents the end of the section that you want, it should be true. If the marker represents the beginning of the next section, it should be false.
        Throws:
        IllegalArgumentException - if either marker is not found.
      • setMinAndMaxProgress

        public void setMinAndMaxProgress​(@FloatRange(from=0.0,to=1.0)
                                         float minProgress,
                                         @FloatRange(from=0.0,to=1.0)
                                         float maxProgress)
        See Also:
        setMinProgress(float), setMaxProgress(float)
      • setSpeed

        public void setSpeed​(float speed)
        Sets the playback speed. If speed < 0, the animation will play backwards.
      • getSpeed

        public float getSpeed()
        Returns the current playback speed. This will be < 0 if the animation is playing backwards.
      • removeAllUpdateListeners

        public void removeAllUpdateListeners()
      • removeAllAnimatorListeners

        public void removeAllAnimatorListeners()
      • setRepeatCount

        public void setRepeatCount​(int count)
        Sets how many times the animation should be repeated. If the repeat count is 0, the animation is never repeated. If the repeat count is greater than 0 or LottieDrawable.INFINITE, the repeat mode will be taken into account. The repeat count is 0 by default.
        Parameters:
        count - the number of times the animation should be repeated
      • getRepeatCount

        public int getRepeatCount()
        Defines how many times the animation should repeat. The default value is 0.
        Returns:
        the number of times the animation should repeat, or LottieDrawable.INFINITE
      • isAnimating

        public boolean isAnimating()
      • setImageAssetsFolder

        public void setImageAssetsFolder​(String imageAssetsFolder)
        If you use image assets, you must explicitly specify the folder in assets/ in which they are located because bodymovin uses the name filenames across all compositions (img_#). Do NOT rename the images themselves.

        If your images are located in src/main/assets/airbnb_loader/ then call `setImageAssetsFolder("airbnb_loader/");`.

        Be wary if you are using many images, however. Lottie is designed to work with vector shapes from After Effects. If your images look like they could be represented with vector shapes, see if it is possible to convert them to shape layers and re-export your animation. Check the documentation at http://airbnb.io/lottie for more information about importing shapes from Sketch or Illustrator to avoid this.

      • getImageAssetsFolder

        @Nullable
        public String getImageAssetsFolder()
      • setMaintainOriginalImageBounds

        public void setMaintainOriginalImageBounds​(boolean maintainOriginalImageBounds)
        When true, dynamically set bitmaps will be drawn with the exact bounds of the original animation, regardless of the bitmap size. When false, dynamically set bitmaps will be drawn at the top left of the original image but with its own bounds. Defaults to false.
      • getMaintainOriginalImageBounds

        public boolean getMaintainOriginalImageBounds()
        When true, dynamically set bitmaps will be drawn with the exact bounds of the original animation, regardless of the bitmap size. When false, dynamically set bitmaps will be drawn at the top left of the original image but with its own bounds. Defaults to false.
      • setImageAssetDelegate

        public void setImageAssetDelegate​(ImageAssetDelegate assetDelegate)
        Use this if you can't bundle images with your app. This may be useful if you download the animations from the network or have the images saved to an SD Card. In that case, Lottie will defer the loading of the bitmap to this delegate.

        Be wary if you are using many images, however. Lottie is designed to work with vector shapes from After Effects. If your images look like they could be represented with vector shapes, see if it is possible to convert them to shape layers and re-export your animation. Check the documentation at http://airbnb.io/lottie for more information about importing shapes from Sketch or Illustrator to avoid this.

      • setFontAssetDelegate

        public void setFontAssetDelegate​(FontAssetDelegate assetDelegate)
        Use this to manually set fonts.
      • setTextDelegate

        public void setTextDelegate​(TextDelegate textDelegate)
        Set this to replace animation text with custom text at runtime
      • resolveKeyPath

        public List<KeyPath> resolveKeyPath​(KeyPath keyPath)
        Takes a KeyPath, potentially with wildcards or globstars and resolve it to a list of zero or more actual 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 KeyPath objects because they will be internally resolved to their content and won't trigger a tree walk of the animation contents when applied.

      • addValueCallback

        public <T> void addValueCallback​(KeyPath keyPath,
                                         T property,
                                         LottieValueCallback<T> callback)
        Add a property callback for the specified KeyPath. This 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 KeyPath has already been resolved with resolveKeyPath(KeyPath) and will resolve it if it hasn't.

      • cancelAnimation

        @MainThread
        public void cancelAnimation()
      • pauseAnimation

        @MainThread
        public void pauseAnimation()
      • setFrame

        public void setFrame​(int frame)
        Sets the progress to the specified frame. If the composition isn't set yet, the progress will be set to the frame when it is.
      • getFrame

        public int getFrame()
        Get the currently rendered frame.
      • setProgress

        public void setProgress​(@FloatRange(from=0.0,to=1.0)
                                float progress)
      • getProgress

        @FloatRange(from=0.0,
                    to=1.0)
        public float getProgress()
      • getDuration

        public long getDuration()
      • setPerformanceTrackingEnabled

        public void setPerformanceTrackingEnabled​(boolean enabled)
      • setSafeMode

        public void setSafeMode​(boolean safeMode)
        If you are experiencing a device specific crash that happens during drawing, you can set this to true for those devices. If set to true, draw will be wrapped with a try/catch which will cause Lottie to render an empty frame rather than crash your app.

        Ideally, you will never need this and the vast majority of apps and animations won't. However, you may use this for very specific cases if absolutely necessary.

        There is no XML attr for this because it should be set programmatically and only for specific devices that are known to be problematic.

      • setRenderMode

        public void setRenderMode​(RenderMode renderMode)
        Call this to set whether or not to render with hardware or software acceleration. Lottie defaults to Automatic which will use hardware acceleration unless: 1) There are dash paths and the device is pre-Pie. 2) There are more than 4 masks and mattes and the device is pre-Pie. Hardware acceleration is generally faster for those devices unless there are many large mattes and masks in which case there is a lot of GPU uploadTexture thrashing which makes it much slower.

        In most cases, hardware rendering will be faster, even if you have mattes and masks. However, if you have multiple mattes and masks (especially large ones), you should test both render modes. You should also test on pre-Pie and Pie+ devices because the underlying rendering engine changed significantly.

        See Also:
        Android Hardware Acceleration
      • setApplyingOpacityToLayersEnabled

        public void setApplyingOpacityToLayersEnabled​(boolean isApplyingOpacityToLayersEnabled)
        Sets whether to apply opacity to the each layer instead of shape.

        Opacity is normally applied directly to a shape. In cases where translucent shapes overlap, applying opacity to a layer will be more accurate at the expense of performance.

        The default value is false.

        Note: This process is very expensive. The performance impact will be reduced when hardware acceleration is enabled.

        See Also:
        setRenderMode(RenderMode)
      • disableExtraScaleModeInFitXY

        @Deprecated
        public void disableExtraScaleModeInFitXY()
        Deprecated.
        This API no longer has any effect.
      • addLottieOnCompositionLoadedListener

        public boolean addLottieOnCompositionLoadedListener​(@NonNull
                                                            LottieOnCompositionLoadedListener lottieOnCompositionLoadedListener)
      • removeLottieOnCompositionLoadedListener

        public boolean removeLottieOnCompositionLoadedListener​(@NonNull
                                                               LottieOnCompositionLoadedListener lottieOnCompositionLoadedListener)
      • removeAllLottieOnCompositionLoadedListener

        public void removeAllLottieOnCompositionLoadedListener()