Geert
09/24/2020, 5:32 PM// CircleShape does not work
Image(
asset = CircleShape,
modifier = Modifier
.preferredSize(16.dp)
.background(Color.Red)
)
// Show an Circle
Box(shape = CircleShape,
backgroundColor = Color.Red,
modifier = Modifier.preferredSize(16.dp)
) { }
// Material has no regular circle
Image(
asset = Icons.Default.AddCircle,
modifier = Modifier
.preferredSize(16.dp)
.background(Color.Red)
)
Nader Jawad
09/24/2020, 6:23 PMCircleShape
?Canvas(modifier = Modifier.preferredSize(16.dp)) { drawCircle(Color.Red) }
CircleShape
isn't an ImageAsset
or VectorAsset
so I don't think that would compile. The Box approach would work too. The last example would work if you had a vector asset that represented a circle.
For the most part though the quickest way to draw anything would be to use Modifier.drawBehind
with the drawing commands that you want and provide that on any composable modifier chain. The example, I provided using canvas is shorthand for creating a composable and automatically providing a drawBehind
modifier on it.mattinger
09/25/2020, 2:07 AM@Composable
fun drawableImageAsset(context: Context = ContextAmbient.current,
@DrawableRes id: Int): ImageAsset? {
val drawable = ContextCompat.getDrawable(context, id)
return drawable?.let { d ->
val bm = Bitmap.createBitmap(
d.intrinsicWidth,
d.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bm)
d.setBounds(0, 0, canvas.width, canvas.height)
d.draw(canvas)
bm.asImageAsset()
}
}
Nader Jawad
09/25/2020, 2:54 AMImageAsset
or VectorAsset
can consume a Painter
as well.ImagePainter
API as well as VectorPainter
which you can create via ImagePainter(imageAsset)
and VectorPainter(vectorAsset)
accordingly.Image
composable consumes Painter
instances directly but you can also draw any Painter
as part of any other composable using the Modifier.paint(painter)
modifier.DrawScope