https://kotlinlang.org logo
#compose
Title
# compose
n

nglauber

11/06/2019, 1:01 AM
Any suggestion for async image loading? In other words: do we have Glide/Fresco/Picasso for Compose? 🙂
a

Adam Powell

11/06/2019, 1:02 AM
You can wire any of those in using an effect 🙂
use a custom image load target and have it set a field in an
@Model
, and return that from the effect
we were just chatting on the team earlier this afternoon about having some integration artifacts to provide some of these things
but they're doable using public API today
n

nglauber

11/06/2019, 1:23 AM
Thanks @Adam Powell! But how can I create an
Image
using a
Drawable
or
Bitmap
? 🤔
l

Leland Richardson [G]

11/06/2019, 1:53 AM
yeah i just did this myself. i had to duplicate the src for
AndroidImage
since it’s marked as internal. I think we should probably change that
n

nglauber

11/06/2019, 1:54 AM
Got it @Leland Richardson [G] 😉
l

Leland Richardson [G]

11/06/2019, 1:54 AM
Image
is an interface, so you can implement it yourself. the code for
AndroidImage
is basically just a wrapper around
Bitmap
so it’s not a lot of code to copy
there might be a few of these statements where we say “you can do that yourself with public API”, and the intention is there, but there’s some class we accidentally made internal and haven’t realized it yet
i think this is one of those cases, unless i’m missing something
Note: if you happen to write a really robust one, let me know! 🙂
n

nglauber

11/06/2019, 1:59 AM
I probably won’t 🙃
l

Leland Richardson [G]

11/06/2019, 1:59 AM
here’s a real rough start of one that I just did using Picasso….
Copy code
@Composable
fun Image(url: String, aspectRatio: Float) {
    var image by +state<Image?> { null }
    var drawable by +state<Drawable?> { null }
    +onCommit(url) {
        val picasso = Picasso.get()
        val target = object : Target {
            override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
                // TODO(lmr): we could use the drawable below
                drawable = placeHolderDrawable
            }

            override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
                drawable = errorDrawable
            }

            override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
                image = bitmap?.let { AndroidImage(it) }
            }
        }
        picasso
            .load(url)
            .into(target)

        onDispose {
            image = null
            drawable = null
            picasso.cancelRequest(target)
        }
    }
    // TODO(lmr): what's the best way to do aspect ratio here, and have the image fill the available
    // width?
    Container(height = 100.dp * aspectRatio, width = 100.dp) {
        val theImage = image
        val theDrawable = drawable
        if (theImage != null) {
            DrawImage(image = theImage)
        } else if (theDrawable != null) {
            Draw { canvas, parentSize -> theDrawable.draw(canvas.nativeCanvas) }
        }
    }
}
👍 2
n

nglauber

11/06/2019, 2:00 AM
awesome! I’m trying it now! 😛
l

Leland Richardson [G]

11/06/2019, 2:00 AM
real bare bones right now, but that might get ya started
hard coded to 100.dp right now 🙂
i’ll let ya know if i get something more complete. i might be doing that in next few days. we are doing a hackathon internally so i’ve been making a few odd things like this 😉
n

nglauber

11/06/2019, 2:26 AM
It worked 😉 thanks! Now fighting with the alignment on the second tab 😛
👍 2
@Leland Richardson [G] I wrote a post (in portuguese) describing how do I created this sample app. I gave you the credit for the
Image
component 😄 https://medium.com/@nglauber/jetpack-compose-part-ii-async-data-tabs-scroller-fab-state-efc8e267b914
👍 1
l

Leland Richardson [G]

11/06/2019, 7:33 PM
wow, super cool! going to take a look
n

nglauber

11/06/2019, 7:34 PM
awesome! I really appreciate your feedback 😉
g

ghedeon

11/09/2019, 2:00 PM
any plans to support image loading by widget out of the box, like flutter does?
🙏 3
i

itnoles

11/17/2019, 5:13 AM
AndroidImage is internal, hmm
l

Leland Richardson [G]

11/18/2019, 7:30 AM
yeah - i ended up copy/pasting it out in order to do what i did above. We will make it public I believe.
9 Views