API Brush memberi Anda alat untuk
menentukan gaya visual goresan Anda. Anda dapat membuat kuas dengan berbagai
warna, ukuran, dan jenis untuk mendapatkan berbagai tampilan.
Membuat kuas
Untuk membuat kuas, gunakan metode pendamping Brush Compose dengan argumen bernama seperti Brush.Companion.createWithComposeColor.
Dengan class ini, Anda dapat menetapkan properti berikut:
family: Gaya kuas, serupa dengan typeface atau font dalam teks. LihatStockBrushesuntuk mengetahui nilaiBrushFamilyyang tersedia.color: Warna kuas. Anda dapat menyetel warna menggunakanColorLong.size: Ketebalan keseluruhan goresan yang dibuat dengan kuas.epsilon: Jarak terkecil yang membuat dua titik harus dianggap berbeda secara visual untuk tujuan geometri pembuatan goresan. Rasio epsilon dan titik goresan mengontrol seberapa jauh goresan dapat diperbesar tanpa artefak, dengan biaya memori. Titik awal yang baik untuk unit stroke adalah 1 px, dan titik awal yang baik untuk epsilon adalah 0,1. Nilai epsilon yang lebih tinggi menggunakan lebih sedikit memori, tetapi memungkinkan lebih sedikit zoom sebelum artefak segitiga muncul. Lakukan eksperimen untuk menemukan nilai yang tepat untuk kasus penggunaan Anda.
val brush = Brush.createWithComposeColor(
family = StockBrushes.pressure(),
colorIntArgb = Color.Black,
size = 5F,
epsilon = 0.1F
)
Mengubah properti kuas
Anda dapat membuat salinan kuas yang ada menggunakan metode
copyWithComposeColor(), yang memungkinkan Anda
mengubah properti kuas.
val redBrush = Brush.createWithComposeColor(
family = StockBrushes.pressurePen(),
colorIntArgb = Color.RED,
size = 5F,
epsilon = 0.1F
)
val blueBrush = redBrush.copyWithComposeColor(color = Color.BLUE)
Kuas Kustom
While StockBrushes provides a versatile set of
common brushes, Ink API also offers an advanced path for creating entirely new
brush behaviors for unique artistic effects or to replicate specific existing
brushes for backward compatibility.
A custom BrushFamily is loaded from its serialized format. The required format
is the gzipped binary encoding of the
BrushFamily protocol buffer. This lets you load and use
custom brush files today. Once deserialized, the custom BrushFamily can be
used to create a new Brush with a specific color and size, just like any of
the StockBrushes families.
class CustomBrushes(val context: Context) {
private const val TAG = "CustomBrushes"
val brushes by lazy { loadCustomBrushes(context) }
@OptIn(ExperimentalInkCustomBrushApi::class)
private fun loadCustomBrushes(): List<CustomBrush> {
val brushFiles = mapOf(
"Calligraphy" to (R.raw.calligraphy to R.drawable.draw_24px),
"Flag Banner" to (R.raw.flag_banner to R.drawable.flag_24px),
"Graffiti" to (R.raw.graffiti to R.drawable.format_paint_24px),
// ...
)
val loadedBrushes = brushFiles.mapNotNull { (name, pair) ->
val (resourceId, icon) = pair
val brushFamily = context.resources.openRawResource(resourceId).use
{ inputStream ->
BrushFamily.decode(inputStream)
}
CustomBrush(name, icon, brushFamily.copy(clientBrushFamilyId = name))
}
return loadedBrushes
}
}
data class CustomBrush(
val name: String,
val icon: Int,
val brushFamily: BrushFamily
)