ब्रश एपीआई

Brush एपीआई की मदद से, आपको स्ट्रोक की विज़ुअल स्टाइल तय करने के लिए टूल मिलते हैं. अलग-अलग लुक पाने के लिए, अलग-अलग रंगों, साइज़, और फ़ैमिली के ब्रश बनाए जा सकते हैं.

ब्रश बनाना

ब्रश बनाने के लिए, Compose Brush कंपैनियन के तरीकों का इस्तेमाल करें. इनमें नाम वाले आर्ग्युमेंट शामिल होते हैं, जैसे कि Brush.Companion.createWithComposeColor. इस क्लास की मदद से, ये प्रॉपर्टी सेट की जा सकती हैं:

  • family: ब्रश की स्टाइल, जो टेक्स्ट में टाइपफ़ेस या फ़ॉन्ट के जैसी होती है. उपलब्ध BrushFamily वैल्यू के लिए, StockBrushes देखें.
  • color: ब्रश का रंग. ColorLong का इस्तेमाल करके, रंग सेट किया जा सकता है.
  • size: ब्रश से बनाए गए स्ट्रोक की कुल मोटाई.
  • epsilon: यह सबसे कम दूरी है. इस दूरी पर मौजूद दो पॉइंट को स्ट्रोक जनरेट करने की जियोमेट्री के हिसाब से, अलग-अलग माना जाना चाहिए. एपसिलॉन और स्ट्रोक पॉइंट का अनुपात यह तय करता है कि स्ट्रोक को कितना ज़ूम किया जा सकता है. इससे मेमोरी पर असर पड़ता है. स्ट्रोक यूनिट के लिए, 1 पिक्सल से शुरुआत करना सही होता है. वहीं, ऐब्सिलॉन के लिए 0.1 से शुरुआत करना सही होता है. एप्सिलॉन की ज़्यादा वैल्यू का इस्तेमाल करने पर, कम मेमोरी का इस्तेमाल होता है. हालांकि, इससे ट्रायंगल आर्टफ़ैक्ट दिखने से पहले कम ज़ूम किया जा सकता है. अपने इस्तेमाल के उदाहरण के लिए सही वैल्यू ढूंढने के लिए, एक्सपेरिमेंट करें.
val brush = Brush.createWithComposeColor(
  family = StockBrushes.pressure(),
  colorIntArgb = Color.Black,
  size = 5F,
  epsilon = 0.1F
)

ब्रश की प्रॉपर्टी में बदलाव करना

copyWithComposeColor() तरीके का इस्तेमाल करके, किसी मौजूदा ब्रश की कॉपी बनाई जा सकती है. इससे आपको ब्रश की किसी भी प्रॉपर्टी में बदलाव करने की सुविधा मिलती है.

val redBrush = Brush.createWithComposeColor(
  family = StockBrushes.pressurePen(),
  colorIntArgb = Color.RED,
  size = 5F,
  epsilon = 0.1F
)

val blueBrush = redBrush.copyWithComposeColor(color = Color.BLUE)

कस्टम ब्रश

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
)