4
4
.
.
5
5
.
.
9
9
d
d
r
r
a
a
w
w
P
P
o
o
i
i
n
n
t
t
s
s
-
-
P
P
o
o
l
l
y
y
g
g
o
o
n
n
I
I
n
n
f
f
o
o
[
[
R
R
]
]
This tutorials shows how to use Canvas and drawPoints() to draw Polygon by setting PointMode.Polygon.
Points are defined in a List<Offset>.
Syntax
import androidx.compose.foundation.Canvas
Canvas(Modifier.fillMaxSize()) {
drawPoints(
points = listOf(
Offset(x=500f, y=400f),
Offset(x=700f, y=400f),
Offset(x=700f, y=600f),
Offset(x=500f, y=600f)
),
pointMode = PointMode.Polygon,
brush = SolidColor(Color.Red),
strokeWidth = 10f
)
}
E
E
x
x
a
a
m
m
p
p
l
l
e
e
In this example we use Canvas to draw multiple Lines.
MainActivity.kt
package com.example.testcompose
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.setContent
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Canvas(Modifier.fillMaxSize()) {
drawPoints(
points = listOf(
Offset(x=500f, y=400f),
Offset(x=700f, y=400f),
Offset(x=700f, y=600f),
Offset(x=500f, y=600f)
),
pointMode = PointMode.Polygon,
brush = SolidColor(Color.Red),
strokeWidth = 10f
)
}
}
}
}
Output