This library brings easy-to-use composable which can fetch and display images from external sources, such as network, using the Picasso image loading library.
The primary API is via the PicassoImage()
functions. There are a 2 function versions available.
The simplest usage is like so:
PicassoImage(
data = "https://loremflickr.com/300/300"
)
This loads the data
passed in with Picasso, and then displays the resulting image using the standard Image
composable.
There is also a version of this function which accepts a Picasso ImageRequest
, allowing full customization of the request. This allows usage of things like (but not limited to) transformations:
PicassoImage(
data = "https://loremflickr.com/300/300",
requestBuilder = {
rotate(90f)
}
)
It also provides optional content 'slots', allowing you to provide custom content to be displayed when the request is loading, and/or if the image request failed:
PicassoImage(
data = "https://loremflickr.com/300/300",
loading = {
Box(Modifier.matchParentSize()) {
CircularProgressIndicator(Modifier.align(Alignment.Center))
}
},
error = {
Image(asset = imageResource(R.drawable.ic_error))
}
)
This library has built-in support for animating loaded images in, using a fade-in animation.
There are two ways to enable the animation:
A fadeIn: Boolean
parameter is available on PicassoImage
(default: false
). When enabled, a default fade-in animation will be used when the image is successfully loaded:
PicassoImage(
data = "https://picsum.photos/300/300",
fadeIn = true
)
If you need more control over the animation, you can use the content
composable version of PicassoImage
, to display the result in a MaterialLoadingImage
:
PicassoImage(
data = "https://random.image",
) { imageState ->
when (imageState) {
is ImageLoadState.Success -> {
MaterialLoadingImage(
result = imageState,
fadeInEnabled = true,
fadeInDurationMs = 600,
)
}
is ImageLoadState.Error -> /* TODO */
ImageLoadState.Loading -> /* TODO */
ImageLoadState.Empty -> /* TODO */
}
}
repositories {
mavenCentral()
}
dependencies {
implementation "dev.chrisbanes.accompanist:accompanist-picasso:<version>"
}
Snapshots of the development version are available in Sonatype's snapshots
repository. These are updated on every commit.