Skip to content

Instantly share code, notes, and snippets.

@anhvt52
Last active April 5, 2021 17:37
Show Gist options
  • Save anhvt52/bf9ee4a953f3421cbdbdd863650f10e8 to your computer and use it in GitHub Desktop.
Save anhvt52/bf9ee4a953f3421cbdbdd863650f10e8 to your computer and use it in GitHub Desktop.
Jetpack Compose Remote Image using Glide
fun RemoteImage(uri: String, modifier: Modifier = Modifier, default: @Composable () -> Unit) {
val bitmapState = remember { mutableStateOf<Bitmap?>(null) }
Glide.with(AmbientContext.current).asBitmap().load(uri).into(
object : CustomTarget<Bitmap>() {
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
bitmapState.value = resource
}
override fun onLoadCleared(placeholder: Drawable?) {
}
}
)
if (bitmapState.value != null) {
Image(
bitmap = bitmapState.value!!.asImageBitmap(),
modifier = modifier
)
} else {
default()
}
}
@luangs7
Copy link

luangs7 commented Apr 5, 2021

onResourceReady is not being called

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment