StyleConfig - VerstSiu/kotlin_extension GitHub Wiki

API List

package com.ijoic.ktx.widget.attr

/*
 * Base
 */
abstract class StyleConfig {
  fun init(context: Context, set: AttributeSet? = null)
}

/*
 * Extend bind property
 */
fun StyleConfig.bindText(
  @AttrRes attr: Int
  [, defValue: String?]): ReadWriteProperty<StyleConfig, String?>

fun StyleConfig.bindString(
  @AttrRes attr: Int
  [, defValue: String?]): ReadWriteProperty<StyleConfig, String?>

fun StyleConfig.bindNonResourceString(
  @AttrRes attr: Int
  [, defValue: String?]): ReadWriteProperty<StyleConfig, String?>

fun StyleConfig.bindBoolean(
  @AttrRes attr: Int
  [, defValue: Boolean]): ReadWriteProperty<StyleConfig, Boolean>

fun StyleConfig.bindInt(
  @AttrRes attr: Int
  [, defValue: Int]): ReadWriteProperty<StyleConfig, Int>

fun StyleConfig.bindFloat(
  @AttrRes attr: Int
  [, defValue: Float]): ReadWriteProperty<StyleConfig, Float>

fun StyleConfig.bindColor(
  @AttrRes attr: Int
  [, defValue: Int]): ReadWriteProperty<StyleConfig, Int>

fun StyleConfig.bindColorStateList(
  @AttrRes attr: Int
  [, defValue: ColorStateList?]): ReadWriteProperty<StyleConfig, ColorStateList?>

fun StyleConfig.bindInteger(
  @AttrRes attr: Int
  [, defValue: Int]): ReadWriteProperty<StyleConfig, Int>

fun StyleConfig.bindDimen(
  @AttrRes attr: Int
  [, defValue: Float]): ReadWriteProperty<StyleConfig, Float>

fun StyleConfig.bindDimenPixelOffset(
  @AttrRes attr: Int
  [, defValue: Int]): ReadWriteProperty<StyleConfig, Int>

fun StyleConfig.bindDimenPixelSize(
  @AttrRes attr: Int
  [, defValue: Int]): ReadWriteProperty<StyleConfig, Int>

fun StyleConfig.bindFraction(
  @AttrRes attr: Int
  base: Int,
  pbase: Int,
  [, defValue: Float]): ReadWriteProperty<StyleConfig, Float>

fun StyleConfig.bindResourceId(
  @AttrRes attr: Int
  [, defValue: Int]): ReadWriteProperty<StyleConfig, Int>

fun StyleConfig.bindDrawable(
  @AttrRes attr: Int
  [, defValue: Drawable?]): ReadWriteProperty<StyleConfig, Drawable?>

fun StyleConfig.bindTextArray(
  @AttrRes attr: Int
  [, defValue: Array<CharSequence>?]): ReadWriteProperty<StyleConfig, Array<CharSequence>?>

Usage

  1. Declare attributes at xml:

    <resources>
    
      <declare-styleable name="RingView">
        <attr name="radius" format="dimension"/>
        <attr name="stroke_color" format="color"/>
        <attr name="stroke_width" format="dimension"/>
      </declare-styleable>
    
    </resources>
  2. Declare style class:

    /**
     * Attr config.
     */
    private class AttrConfig: StyleConfig() {
      /**
       * Radius.
       */
      val radius by bindDimen(R.attr.radius)
    
      /**
       * Stroke color.
       */
      val strokeColor by bindColor(R.attr.stroke_color)
    
      /**
       * Stroke width.
       */
      val strokeWidth by bindDimen(R.attr.stroke_width)
    }
  3. Initialize style class:

    class RingView(context: Context, set: AttributeSet?): View(context, set) {
    
      private val config = AttrConfig().apply { init(context, set) }
    
    }
  4. Use style class to draw view content:

    override fun onDraw(canvas: Canvas?) {
      canvas ?: return
    
      val centerX = width.toFloat() / 2F
      val centerY = height.toFloat() / 2F
    
      paint.strokeWidth = config.strokeWidth
      paint.color = config.strokeColor
    
      canvas.drawCircle(centerX, centerY, config.radius, paint)
    }
⚠️ **GitHub.com Fallback** ⚠️