Skip to content

Commit 14589de

Browse files
committed
✨ Complete Library implementation
1 parent feec0b1 commit 14589de

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,132 @@
11
package com.emkaydauda.colorslider
22

3+
4+
import android.content.Context
5+
import android.graphics.Canvas
6+
import android.graphics.Color
7+
import android.graphics.Paint
8+
import android.graphics.drawable.Drawable
9+
import androidx.core.content.ContextCompat
10+
import android.util.AttributeSet
11+
import android.util.TypedValue
12+
import android.widget.SeekBar
13+
14+
class ColorSlider @JvmOverloads
15+
constructor(
16+
context: Context,
17+
attributeSet: AttributeSet? = null,
18+
styleAttr: Int = R.attr.seekBarStyle,
19+
styleRes: Int = 0
20+
) : SeekBar(context, attributeSet, styleAttr, styleRes) {
21+
22+
private var colors: ArrayList<Int> = arrayListOf(Color.RED, Color.GREEN, Color.DKGRAY)
23+
24+
private val w = 16f.toDip(context)
25+
private val h = 16f.toDip(context)
26+
27+
private val halfW = if (w >= 0) w / 2f else 1f
28+
private val halfH = if (h >= 0) h / 2f else 1f
29+
30+
private val paint = Paint()
31+
32+
private var noColorDrawable: Drawable? = null
33+
set(value) {
34+
w2 = value?.intrinsicWidth ?: 0
35+
h2 = value?.intrinsicHeight ?: 0
36+
halfW2 = if (w2 >= 0) w2 / 2 else 1
37+
halfH2 = if (h2 >= 0) h2 / 2 else 1
38+
39+
value?.setBounds(-halfW2, -halfH2, halfW2, halfH2)
40+
field = value
41+
}
42+
43+
var w2 = 0
44+
var h2 = 0
45+
var halfW2 = 1
46+
var halfH2 = 1
47+
48+
init {
49+
val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.ColorSlider)
50+
try {
51+
colors = typedArray.getTextArray(R.styleable.ColorSlider_colors).map {
52+
Color.parseColor(it.toString())
53+
} as ArrayList<Int>
54+
} finally {
55+
typedArray.recycle()
56+
}
57+
58+
noColorDrawable = context.getDrawable(R.drawable.ic_clear_red_24dp)
59+
60+
61+
colors.add(0, android.R.color.transparent)
62+
max = colors.size - 1
63+
progressTintList = ContextCompat.getColorStateList(context, android.R.color.transparent)
64+
progressBackgroundTintList = ContextCompat.getColorStateList(context, android.R.color.transparent)
65+
splitTrack = false
66+
67+
setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom + 16f.toDip(context).toInt())
68+
69+
thumb = context.getDrawable(R.drawable.ic_arrow_drop_down_black_24dp)
70+
71+
setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
72+
override fun onProgressChanged(p0: SeekBar?, position: Int, p2: Boolean) {
73+
listeners.forEach {
74+
it(colors[position])
75+
}
76+
}
77+
78+
override fun onStartTrackingTouch(p0: SeekBar?) {
79+
80+
}
81+
82+
override fun onStopTrackingTouch(p0: SeekBar?) {
83+
84+
}
85+
})
86+
87+
}
88+
89+
override fun onDraw(canvas: Canvas?) {
90+
super.onDraw(canvas)
91+
drawTickMarks(canvas)
92+
}
93+
94+
private var listeners: ArrayList<(Int) -> Unit> = arrayListOf()
95+
96+
var selectedColorValue: Int = android.R.color.transparent
97+
set(value) {
98+
var index = colors.indexOf(value)
99+
progress = if (index == -1) 0 else index
100+
}
101+
102+
fun addListener(function: (Int) -> Unit) {
103+
listeners.add(function)
104+
}
105+
106+
private fun drawTickMarks(canvas: Canvas?) {
107+
canvas?.let {
108+
val count = colors.size
109+
val saveCount = canvas.save()
110+
canvas.translate(paddingLeft.toFloat(), (height / 2f) + 8f.toDip(context))
111+
if (count > 1) {
112+
113+
for (i in 0 until count) {
114+
val spacing = (width - paddingLeft - paddingRight) / (count - 1).toFloat()
115+
116+
if (i == 0) {
117+
noColorDrawable?.draw(canvas)
118+
} else {
119+
paint.color = colors[i]
120+
canvas.drawRoundRect(-halfW, -halfH, halfW, halfH, 2f.toDip(context), 2f.toDip(context), paint)
121+
}
122+
canvas.translate(spacing, 0f)
123+
}
124+
canvas.restoreToCount(saveCount)
125+
}
126+
}
127+
}
128+
}
129+
130+
private fun Float.toDip(context: Context): Float {
131+
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this, context.resources.displayMetrics)
132+
}

0 commit comments

Comments
 (0)