Ink Brush API की मदद से, कैनवस पर ड्रॉइंग करने के लिए ब्रशेस बनाए और पसंद के मुताबिक बनाए जा सकते हैं.
ब्रश बनाना
ब्रश बनाने के लिए, Brush फ़ैक्ट्री के तरीकों का इस्तेमाल करें. साथ ही, नाम वाले आर्ग्युमेंट का इस्तेमाल करें. जैसे, Brush.createWithColorLong(). इस क्लास की मदद से, ये प्रॉपर्टी सेट की जा सकती हैं:
family: ब्रश की स्टाइल, जो टेक्स्ट में टाइपफ़ेस या फ़ॉन्ट के जैसी होती है. उपलब्धBrushFamilyवैल्यू के लिए,StockBrushesदेखें.color: ब्रश का रंग.ColorLongका इस्तेमाल करके, रंग सेट किया जा सकता है.size: ब्रश से बनाए गए स्ट्रोक की शुरुआती मोटाई.epsilon: यह स्ट्रोक में मौजूद दो पॉइंट के बीच की सबसे कम दूरी होती है. इस दूरी पर मौजूद दो पॉइंट को अलग-अलग माना जाता है. इससे स्ट्रोक की ज्यामिति की बारीकी या फ़िडेलिटी को कंट्रोल किया जाता है.- ज़्यादा वैल्यू होने पर, स्ट्रोक ज़्यादा आसान हो जाता है. इससे कम मेमोरी इस्तेमाल होती है और रेंडरिंग तेज़ी से होती है. हालांकि, ज़ूम इन करने पर, आपको आर्टफ़ैक्ट दिख सकते हैं. जैसे, किनारे पर दांत जैसे निशान.
- कम वैल्यू का इस्तेमाल करने पर, ज़ूम करने पर इमेज की क्वालिटी अच्छी रहती है. हालांकि, इससे मेमोरी का इस्तेमाल बढ़ जाता है.
- एक पिक्सल यूनिट स्केल के साथ प्रोटोटाइपिंग के लिए, 0.1 एक अच्छा शुरुआती पॉइंट है. अलग-अलग स्क्रीन डेंसिटी के साथ काम करने वाले प्रोडक्शन ऐप्लिकेशन के लिए, डेंसिटी से जुड़ी यूनिट (जैसे, dp) का इस्तेमाल करें. साथ ही, इसके हिसाब से ऐब्सिलॉन को अडजस्ट करें.
val redBrush = Brush.createWithColorLong(
family = StockBrushes.pressurePen(),
colorLong = Color.RED.pack(),
size = 5F,
epsilon = 0.1F
)
ब्रश की प्रॉपर्टी में बदलाव करना
copy() तरीके का इस्तेमाल करके, किसी मौजूदा ब्रश की कॉपी बनाई जा सकती है. इस तरीके से, ब्रश की किसी भी प्रॉपर्टी में बदलाव किया जा सकता है.
val blueBrush = redBrush.copy(colorLong = Color.BLUE.pack())
कस्टम ब्रश
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
)