Colton Idle
07/12/2023, 7:02 PMephemient
07/12/2023, 7:26 PMPainter
,
with(painter) { draw(size) }
inside your DrawScope
will draw it. if you have an ImageVector
, convert it to a Painter
with rememberVectorPainter()
MR3Y
07/12/2023, 7:26 PMpainterResource
works with vector drawables and you can draw it within a canvas using Painter#draw :
val vector = painterResource(id = R.drawable.example)
Canvas(modifier = Modifier.fillMaxSize(), onDraw = {
with(vector) {
draw(size)
}
})
MR3Y
07/12/2023, 7:26 PMephemient
07/12/2023, 7:27 PMephemient
07/12/2023, 7:27 PMColton Idle
07/12/2023, 7:37 PMephemient
07/12/2023, 7:39 PMDrawScope.() -> Unit
functionColton Idle
07/12/2023, 8:45 PMephemient
07/12/2023, 8:51 PMdrawContext.canvas.saveLayer(Rect(Offset.Zero, size), Paint().apply { blendMode = ... })
draw()
drawContext.canvas.restore()
Colton Idle
07/12/2023, 8:52 PMColton Idle
07/12/2023, 8:52 PMephemient
07/12/2023, 8:52 PMephemient
07/12/2023, 8:53 PMColton Idle
07/12/2023, 8:53 PMColton Idle
07/12/2023, 8:54 PMephemient
07/12/2023, 8:55 PMColton Idle
07/12/2023, 8:56 PMColton Idle
07/12/2023, 8:56 PMColton Idle
07/12/2023, 8:56 PMephemient
07/12/2023, 9:07 PMColton Idle
07/12/2023, 9:08 PMephemient
07/12/2023, 9:08 PMColton Idle
07/12/2023, 9:09 PMColton Idle
07/12/2023, 9:26 PMColton Idle
07/13/2023, 1:13 PMephemient
07/13/2023, 8:11 PMephemient
07/13/2023, 8:36 PM@Preview(showBackground = true)
@Composable
private fun Example() {
val painter = rememberVectorPainter(Icons.Default.Refresh)
Canvas(
modifier = Modifier
.size(96.dp)
.graphicsLayer(compositingStrategy = CompositingStrategy.Offscreen),
) {
drawCircle(Brush.linearGradient(listOf(Color.Red, Color.Blue)))
drawContext.canvas.saveLayer(Rect(Offset.Zero, size), Paint().apply { blendMode = BlendMode.DstOut })
with(painter) { draw(size) }
drawContext.canvas.restore()
}
}
the compositingStrategy
is to prevent it from punching a hole through all the parentsephemient
07/13/2023, 10:34 PM@Preview(showBackground = true)
@Composable
private fun Example() {
val icon = Icons.Default.Refresh
Canvas(modifier = Modifier.size(96.dp)) {
val parser = PathParser()
fun VectorNode.parseTo(path: Path) {
when (this) {
is VectorGroup -> {
// doesn't handle rotation/scale/translation/clip
for (child in this) child.parseTo(path)
}
is VectorPath -> {
// doesn't handle stroke/fill
parser.clear()
parser.addPathNodes(pathData).toPath(path)
}
}
}
val vectorPath = Path().also { icon.root.parseTo(it) }
scale(size.width / icon.viewportWidth, size.height / icon.viewportHeight, Offset.Zero) {
val mask = Path.combine(
PathOperation.Difference,
Path().apply { addRect(Rect(0f, 0f, icon.viewportWidth, icon.viewportHeight)) },
vectorPath,
)
clipPath(mask) {
scale(icon.viewportWidth / size.width, icon.viewportHeight / size.height, Offset.Zero) {
drawCircle(Brush.linearGradient(listOf(Color.Red, Color.Blue)))
}
}
}
}
}
Colton Idle
07/15/2023, 6:09 AMColton Idle
07/15/2023, 6:47 AMdrawContext.canvas.saveLayer(_Rect_(center, mySize), paint) but it keeps drawing in the top left
ephemient
07/15/2023, 9:49 AMtranslate(dx, dy) { ... }
or
inset(dx, dy) { ... }
depending on what you wantColton Idle
07/17/2023, 6:33 PM