Brush API

API ของ Brush มีเครื่องมือให้คุณใช้ กำหนดสไตล์ภาพของลายเส้น คุณสร้างแปรงที่มีสี ขนาด และตระกูลต่างๆ ได้ เพื่อให้ได้ลุคที่หลากหลาย

สร้างแปรง

หากต้องการสร้างพู่กัน ให้ใช้เมธอด Compose Brush ร่วมกับอาร์กิวเมนต์ที่มีชื่อ เช่น Brush.Companion.createWithComposeColor คลาสนี้ช่วยให้คุณตั้งค่าพร็อพเพอร์ตี้ต่อไปนี้ได้

  • family: รูปแบบของแปรง ซึ่งคล้ายกับแบบตัวพิมพ์หรือแบบอักษรในข้อความ ดูค่า StockBrushes ที่ใช้ได้ของ BrushFamily
  • color: สีของแปรง คุณตั้งค่าสีได้โดยใช้ ColorLong
  • size: ความหนาโดยรวมของเส้นที่สร้างด้วยแปรง
  • epsilon: ระยะทางที่สั้นที่สุดที่ควรพิจารณาว่าจุด 2 จุดแตกต่างกันในเชิงภาพเพื่อวัตถุประสงค์ด้านรูปทรงเรขาคณิตในการสร้างเส้น อัตราส่วนของเอปซิลอน และจุดจังหวะจะควบคุมระดับการซูมเข้าของจังหวะโดยไม่มี อาร์ติแฟกต์ โดยมีค่าใช้จ่ายเป็นหน่วยความจำ จุดเริ่มต้นที่ดีสำหรับหน่วยจังหวะคือ 1 px และจุดเริ่มต้นที่ดีสำหรับค่าเอปซิลอนคือ 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
)