aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/ColoredRelativeLayout.kt
blob: 3e37411d629f92156481460cfc713a3caecc0f6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package net.lacolaco.smileessence.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.widget.RelativeLayout
import net.lacolaco.smileessence.R

// XXX: 4th argument defStyleRes: Int = 0 isn't available in API 19
class ColoredRelativeLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
        RelativeLayout(context, attrs, defStyleAttr) {
    private val paint = Paint()
    private val highlightColors = IntArray(4)
    private var showAccent = false

    init {
        setWillNotDraw(false)

        val ta = context.obtainStyledAttributes(attrs, R.styleable.ColoredRelativeLayout)
        highlightColors[0] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_none, -1)
        highlightColors[1] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_type1, -1)
        highlightColors[2] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_type2, -1)
        highlightColors[3] = ta.getColor(R.styleable.ColoredRelativeLayout_highlight_type3, -1)
        setHighlight(HIGHLIGHT_NONE)
        paint.color = ta.getColor(R.styleable.ColoredRelativeLayout_accent_color, -1)
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = 3f
        ta.recycle()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (showAccent)
            canvas.drawLine(1f, 0f, 1f, measuredHeight.toFloat(), paint)
    }

    fun setAccentVisibility(yes: Boolean) {
        showAccent = yes
    }

    fun setHighlight(type: Int) {
        setBackgroundColor(highlightColors[type])
    }

    companion object {
        val HIGHLIGHT_NONE = 0
    }
}