توفّر واجهات برمجة التطبيقات BrushInk طريقة لإنشاء وتخصيص فُرش للرسم على لوحة.
إنشاء فرشاة
لإنشاء فرشاة، استخدِم طرق المصنع Brush مع وسيطات مُسمّاة مثل Brush.createWithColorLong(). يتيح لك هذا الصف ضبط الخصائص التالية:
family: نمط الفرشاة، وهو مشابه لنمط الخط أو الخط في النص. راجِعStockBrushesللاطّلاع على قيمBrushFamilyالمتاحة.-
color: تمثّل هذه السمة لون الفرشاة. يمكنك ضبط اللون باستخدامColorLong. size: السُمك الأساسي للخطوط التي تم إنشاؤها باستخدام الفرشاةepsilon: هي أصغر مسافة يجب أن تكون بين نقطتَين في الخط ليتم اعتبارهما مختلفتَين، ما يتحكّم في مستوى التفاصيل أو دقة شكل الخط.- تؤدي القيم الأعلى إلى تبسيط الخط بشكل أكبر، ما يؤدي إلى استخدام مساحة أقل من الذاكرة وعرض أسرع، ولكن قد يؤدي ذلك إلى ظهور تشوّهات مرئية، مثل الحواف المسنّنة عند التكبير.
- تحتفظ القيم المنخفضة بتفاصيل أكثر عند التكبير بجودة عالية، ولكنها تزيد من استخدام الذاكرة.
- لإنشاء نماذج أوّلية باستخدام مقياس وحدة 1 بكسل، يمكنك البدء بالقيمة 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) { companion object { private const val TAG = "CustomBrushes" } val brushes by lazy { loadCustomBrushes() } @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) } return loadedBrushes } } data class CustomBrush( val name: String, val icon: Int, val brushFamily: BrushFamily )