vinaygaba / Learn-Jetpack-Compose-By-Example

🚀 This project contains various examples that show how you would do things the "Jetpack Compose" way

Home Page:https://jetpackcompose.app/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shape does not work with NetworkImageComponentGlide

dri94 opened this issue · comments

If you modify the shape for the Box in the below code, the image will display display as a square

@Composable
fun NetworkImageComponentGlide(url: String, modifier: Modifier = Modifier.fillMaxWidth() +
        Modifier.preferredHeightIn(maxHeight = 200.dp)) {
    var image by state<ImageAsset?> { null }
    var drawable by state<Drawable?> { null }
    val context = ContextAmbient.current
    onCommit(url) {
        val glide = Glide.with(context)
        val target = object : CustomTarget<Bitmap>() {
            override fun onLoadCleared(placeholder: Drawable?) {
                image = null
                drawable = placeholder
            }

            override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap>?) {
                image = bitmap.asImageAsset()
            }
        }
        glide
            .asBitmap()
            .load(url)
            .into(target)

        onDispose {
            image = null
            drawable = null
            glide.clear(target)
        }
    }

    val theImage = image
    val theDrawable = drawable
    if (theImage != null) {
        // Box is a predefined convenience composable that allows you to apply common draw & layout
        // logic. In addition we also pass a few modifiers to it.

        // You can think of Modifiers as implementations of the decorators pattern that are
        // used to modify the composable that its applied to. In this example, we configure the
        // Box composable to have a max height of 200dp and fill out the entire available
        // width.
        Box(modifier = modifier,
            gravity = ContentGravity.Center) {
            // Image is a pre-defined composable that lays out and draws a given [ImageAsset].
            Image(asset = theImage)
        }
    } else if (theDrawable != null) {
        Canvas(modifier = modifier) {
            drawCanvas { canvas, pxSize ->
                theDrawable.draw(canvas.nativeCanvas)
            }
        }
    }
}