Code Monkey home page Code Monkey logo

dotlottieloader-android's Introduction

dotLottieLoader

Introducing dotLottie

dotLottie is an open-source file format that aggregates one or more Lottie files and their associated resources into a single file. They are ZIP archives compressed with the Deflate compression method and carry the file extension of ".lottie".

dotLottieLoader

dotLottieLoader is a library to help downloading and deflating a .lottie file, giving access to the animation, as well as the assets included in the bundle

View documentation, FAQ, help, examples, and more at dotlottie.io

Example

To run the example project, clone the repo, and the sample app

Installation

Install via Jitpack

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    ...
    implementation 'com.github.dotlottie:dotlottieloader-android:{version}'
}

Using dotLottie

loading from network

DotLottieLoader.with(context)
    .fromUrl(lottieUrl)
    .load(object: DotLottieResult {
        override fun onSuccess(result: DotLottie) {
            // handle success
        }

        override fun onError(throwable: Throwable) {
            // handle errors
        }
    })

loading from app assets

Load from raw resource

DotLottieLoader.with(context).fromRaw(rawResID).load(...)

Load from assets

DotLottieLoader.with(context).fromAsset(assetName).load(...)

Using the DotLottie

data class DotLottie(
    val manifest: Manifest?,
    val animations: Map<String, ByteArray>,
    val images: Map<String, ByteArray>
): Serializable

data class Manifest(
    val generator: String,
    val version: Float,
    val revision: Int?,
    val author: String,
    val animations: List<ManifestAnimation>,
    val custom: Map<String, Any>?
): Serializable

data class ManifestAnimation(
    val id: String,
    val speed: Float = 1.0f,
    val themeColor: String?,
    val loop: Boolean = false
): Serializable

for example, using it with lottie-android:

DotLottieLoader.with(this)
    .fromUrl(item)
    .load(object: DotLottieResult {
        override fun onSuccess(result: DotLottie) {

            //set image handler. Check example app for 
            // sample source
            animationView.setImageAssetDelegate(
                AppImageDelegate.getDelegate(
                    this@MainActivity, 
                    result.images
                )
            )
            
            // set the animation
            result.animations?.entries.first()?.let {
                val input = ByteArrayInputStream(it.value)
                
                // set animation from input stream. Pass non-null
                // cachekey to let LottieAnimationView cache the
                // animation internally as well
                animationView.setAnimation(input as InputStream, null)
                animationView.playAnimation()
            }
        }

        override fun onError(throwable: Throwable) {
            Toast.makeText(this@MainActivity, R.string.error_loading, Toast.LENGTH_LONG).show()
            throwable.printStackTrace()
        }
    })

Exporting dotLottie

We can export a DotLottie object to disk using the .compress() method. If the original animation provided a manifest, it will be retain, otherwise a new manifest with defaults (or provided defaults) will be generated

    val f = File(cacheDir, "example.lottie")
    val exported = myDotLottie.compress(loop = true, themeColor = "#dedede")
    f.writeBytes(exported)

    ...

Configurations

The library provides some configuration options to customize its behavior

Global configurations

       DotLottieLoader.setConfig(DotLottieConfig(
            cacheStrategy,  //optional
            cacheManager,   //optional
            converter       //optional
        ))
  • cacheStrategy - the caching strategy to use, DotLottieCacheStrategy.NONE, DotLottieCacheStrategy.MEMORY, DotLottieCacheStrategy.DISK. Defaults to DISK
  • cacheManager - Responsible for handling the cache. Provide your own subclass of DotLottieCache to override the default. The default cache manager uses LRUCache for in memory, and DiskLRUCache for disk caching.
  • DotLottieConverter - Responsible for parsing incoming files and converting them to DotLottie objects. Provide custom implementations if needed

Per request configurations

You may also pass a DotLottieConfig object per request by using

DotLottieLoader.with(context)
    .fromUrl(lottieUrl)
    .withConfig(DotLottieConfig(
        ...
    ))

When requesting from the network, an additional configuration is exposed to override the OkHttpClient used for the request

DotLottieLoader.with(this)
    .fromUrl(lottieUrl)
    .withClient(customOkHttpClient)
    .withConfig(
        DotLottieConfig(
        ...
    ))

Author

Naail Abdul Rahman | [email protected]

License

Copyright (c) 2020 LottieFiles

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, 
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

dotlottieloader-android's People

Contributors

kudanai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.