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?Nader Jawad
09/24/2020, 6:26 PMCanvas(modifier = Modifier.preferredSize(16.dp)) { drawCircle(Color.Red) }Nader Jawad
09/24/2020, 6:29 PMCircleShape 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 AMmattinger
09/25/2020, 2:08 AMmattinger
09/25/2020, 2:08 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.Nader Jawad
09/25/2020, 2:55 AMImagePainter API as well as VectorPainter which you can create via ImagePainter(imageAsset) and VectorPainter(vectorAsset) accordingly.Nader Jawad
09/25/2020, 2:56 AMImage composable consumes Painter instances directly but you can also draw any Painter as part of any other composable using the Modifier.paint(painter) modifier.Nader Jawad
09/25/2020, 2:58 AMDrawScope