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

julioromano

03/04/2021, 10:52 AM
Is there something similar to
android:tileMode="repeat"
we used to use in drawable XMLs ? (I have a drawable which I’d like to display tiled).
1
n

Nader Jawad

03/04/2021, 8:03 PM
Tilemode repeat only applied to BitmapDrawable IIRC. We do expose an ImageShader API that takes in an ImageBitmap and exposes the tilemode parameters
j

julioromano

03/04/2021, 8:41 PM
Yes it is a bitmap drawable. I’ve found the
ImageShader()
api but dunno how to use it. It’s not a @Composable function. What is the “entry point” ? Does it have to be used in a Canvas() composable using
Paint()
?
n

Nader Jawad

03/04/2021, 9:04 PM
You can configure it on a paint or obtain a ShaderBrush instance from it to draw with the DrawScope receiver scope like the following.
Copy code
val bitmapDrawable = LocalContext.current.getDrawable(R.drawable.android) as BitmapDrawable
    val imageBitmap = bitmapDrawable.bitmap.asImageBitmap()
    val imageBitmapShader = ImageShader(imageBitmap, TileMode.Repeated, TileMode.Repeated)
    val imageBrush = ShaderBrush(imageBitmapShader)
    Canvas(modifier = Modifier.fillMaxSize()) {
        drawRect(imageBrush)
    }
🙏 1
I filed a ticket internally to expose tilemode parameters to the Image composable overload that takes in an
ImageBitmap
as input. This isn't always feasible for every Painter instance similar to how the tilemode parameters are only exposed in BitmapDrawable in the framework
🙌 1
5 Views