⚠️ This repository is not longer maintained. Please use the latest dotlottie-android here.
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 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
To run the example project, clone the repo, and the sample app
Install via Jitpack
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
...
implementation 'com.github.dotlottie:dotlottieloader-android:{version}'
}
DotLottieLoader.with(context)
.fromUrl(lottieUrl)
.load(object: DotLottieResult {
override fun onSuccess(result: DotLottie) {
// handle success
}
override fun onError(throwable: Throwable) {
// handle errors
}
})
Load from raw resource
DotLottieLoader.with(context).fromRaw(rawResID).load(...)
Load from assets
DotLottieLoader.with(context).fromAsset(assetName).load(...)
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()
}
})
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)
...
The library provides some configuration options to customize its behavior
DotLottieLoader.setConfig(DotLottieConfig(
cacheStrategy, //optional
cacheManager, //optional
converter //optional
))
cacheStrategy
- the caching strategy to use,DotLottieCacheStrategy.NONE
,DotLottieCacheStrategy.MEMORY
,DotLottieCacheStrategy.DISK
. Defaults toDISK
cacheManager
- Responsible for handling the cache. Provide your own subclass ofDotLottieCache
to override the default. The default cache manager usesLRUCache
for in memory, andDiskLRUCache
for disk caching.DotLottieConverter
- Responsible for parsing incoming files and converting them toDotLottie
objects. Provide custom implementations if needed
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(
...
))
Naail Abdul Rahman | kudanai@gmail.com
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.