Skip to content

Instantly share code, notes, and snippets.

View colinrtwhite's full-sized avatar
🙌
When Github stories?

Colin White colinrtwhite

🙌
When Github stories?
View GitHub Profile
// 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") {
imageView.load("https://www.example.com/image.jpg") {
listener(
onSuccess = { /** Handle success. */ },
onError = { /** Handle error. */ }
)
}
// Call this inside of your coroutine scope.
// This will throw an error inside of the scope if it fails.
val drawable = Coil.get("https://www.example.com/image.jpg")
@colinrtwhite
colinrtwhite / forwarding_painter.kt
Created March 8, 2022 01:11
A painter that wraps another painter to overwrite its color filter and/or alpha.
open class ForwardingPainter(
private val delegate: Painter,
private var alpha: Float = DefaultAlpha,
private var colorFilter: ColorFilter? = null,
) : Painter() {
override val intrinsicSize get() = delegate.intrinsicSize
override fun applyAlpha(alpha: Float): Boolean {
if (alpha == DefaultAlpha) {
@colinrtwhite
colinrtwhite / complex_forwarding_painter.kt
Last active November 13, 2023 13:41
A painter that wraps another painter to overwrite its color filter, alpha, and/or onDraw.
/**
* Create and return a new [Painter] that wraps [painter] with its [alpha], [colorFilter], or [onDraw] overwritten.
*/
fun forwardingPainter(
painter: Painter,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
onDraw: DrawScope.(ForwardingDrawInfo) -> Unit = DefaultOnDraw,
): Painter = ForwardingPainter(painter, alpha, colorFilter, onDraw)