Administrator
Administrator
Published on 2025-03-19 / 7 Visits
0
0

ImageVector、Painter、Bitmap

Painter

painterResource(R.drawable.ic_launcher_foreground) //从res/drawable目录读取资源ID读为Painter


    val customVector = ImageVector.Builder(
        name = "custom icon",
        defaultWidth = 10.dp,
        defaultHeight = 10.dp,
        viewportHeight = 100f,
        viewportWidth = 100f
    ).apply {
        //绘制了二个实心的矩形
        materialPath {
            moveTo(10f, 10f)
            lineTo(90f, 10f)
            lineTo(90f, 90f)
            lineTo(10f, 90f)
            close()
        }
        materialPath {
            moveTo(15f, 15f)
            lineTo(95f, 15f)
            lineTo(95f, 95f)
            lineTo(15f, 95f)
            close()
        }
    }.build()
//VectorPainter实现了Painter,向类型为Painter参数或变量赋值时允许VectorPainter
rememberVectorPainter(Icons.Default.Home) //将一个内置矢量图标转换为VectorPainter
rememberVectorPainter(customVector) //将一个用ImageVector.Build().build()构建的矢量图转换为VectorPainter

ImageVector

矢量图

val customVector = ImageVector.Builder(
    name = "custom icon",
    defaultWidth = 10.dp,
    defaultHeight = 10.dp,
    viewportHeight = 100f,
    viewportWidth = 100f
).apply {
    //绘制了二个实心的矩形
    materialPath {
        moveTo(10f, 10f)
        lineTo(90f,10f)
        lineTo(90f,90f)
        lineTo(10f,90f)
        close()
    }
    materialPath {
        moveTo(15f, 15f)
        lineTo(95f,15f)
        lineTo(95f,95f)
        lineTo(15f,95f)
        close()
    }
}.build()

val customVector2 = materialIcon(name = "test") {
    materialPath {
        moveTo(10f, 10f)
        lineTo(90f,10f)
        lineTo(90f,90f)
        lineTo(10f,90f)
        close()
    }
}
Icon(
    customVector,
    modifier = Modifier.size(150.dp).background(color = Color.Yellow),
    contentDescription = "",
    tint = Color.Red
)


Icons.Filled.Lock //内置ImageVector图标

Bitmap

位图

ImageBitmap.imageResource(id = R.drawable.my_img) //读取Resource为ImageBitmap


Comment