Skip to content

Instantly share code, notes, and snippets.

@colinrtwhite
Last active October 15, 2020 04:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinrtwhite/2b5a1a7dc353c0cf6c5a9ebeba456458 to your computer and use it in GitHub Desktop.
Save colinrtwhite/2b5a1a7dc353c0cf6c5a9ebeba456458 to your computer and use it in GitHub Desktop.
// To load an image into an ImageView, use the load extension function.
imageView.load("https://www.example.com/image.jpg")
// Coil supports urls, uris, resources, drawables, bitmaps, files, and more.
imageView.load(R.drawable.image)
imageView.load(File("/path/to/image.jpg"))
imageView.load("content://com.android.externalstorage/image.jpg")
// Requests can be configured with an optional trailing lambda.
imageView.load("https://www.example.com/image.jpg") {
crossfade(true)
placeholder(R.drawable.image)
transformations(CircleCropTransformation())
}
// Internally, ImageView.load() is powered by the singleton ImageLoader.
val imageLoader = context.imageLoader
// Optionally, you can create and inject your own instance(s).
val imageLoader = ImageLoader(context)
// Enqueue an ImageRequest to load the image asynchronously into a custom target.
val request = ImageRequest.Builder(context)
.data("https://www.example.com/image.jpg")
.target { drawable ->
// Handle the successful result.
}
.build()
imageLoader.enqueue(request)
// Execute an ImageRequest to suspend and return the drawable.
val request = ImageRequest.Builder(context)
.data("https://www.example.com/image.jpg")
.build()
val drawable = imageLoader.execute(request).drawable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment