Brush APIs

رابط‌های برنامه‌نویسی کاربردی قلم‌مو (API) مربوط به قلم‌موهای جوهری (Ink Brush راهی برای ایجاد و سفارشی‌سازی قلم‌موها برای طراحی روی بوم ارائه می‌دهند.

یک قلم مو ایجاد کنید

برای ایجاد یک قلم‌مو، از متدهای کارخانه Brush با آرگومان‌های نامگذاری شده مانند Brush.createWithColorLong() استفاده کنید. این کلاس به شما امکان می‌دهد ویژگی‌های زیر را تنظیم کنید:

  • family : سبک قلم‌مو، مشابه با یک فونت یا فونت در متن. برای مقادیر موجود BrushFamily به StockBrushes مراجعه کنید.
  • color : رنگ قلم‌مو. می‌توانید رنگ را با استفاده از ColorLong تنظیم کنید.
  • size : ضخامت پایه خطوط ایجاد شده با قلم‌مو.
  • epsilon : کمترین فاصله‌ای که در آن دو نقطه در خط باید متمایز در نظر گرفته شوند، که سطح جزئیات یا دقت هندسه خط را کنترل می‌کند.
    • مقادیر بالاتر، حاشیه را ساده‌تر می‌کنند که باعث می‌شود حافظه کمتری مصرف شود و سریع‌تر رندر شود، اما ممکن است هنگام بزرگنمایی، منجر به مصنوعات قابل مشاهده‌ای مانند لبه‌های ناهموار شود.
    • مقادیر پایین‌تر، جزئیات بیشتری را برای بزرگنمایی با کیفیت بالا حفظ می‌کنند، اما استفاده از حافظه را افزایش می‌دهند.
    • برای نمونه‌سازی اولیه با مقیاس واحد ۱ پیکسل، ۰.۱ نقطه شروع خوبی است. برای برنامه‌های تولیدی که از تراکم صفحه نمایش‌های مختلف پشتیبانی می‌کنند، از واحدهای مستقل از تراکم (مانند 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
)