Last active
April 5, 2021 17:37
-
-
Save anhvt52/bf9ee4a953f3421cbdbdd863650f10e8 to your computer and use it in GitHub Desktop.
Jetpack Compose Remote Image using Glide
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
onResourceReady is not being called